Changing how to use textdifference thing

I think it'd be better to use text difference if a substring isn't found.

I realised afterwards that I think this is how Scryfall does it anyway.
This commit is contained in:
2025-08-17 00:34:03 +01:00
parent 6558a31619
commit 9a9f42bc1e
3 changed files with 38 additions and 21 deletions

View File

@@ -14,5 +14,6 @@ rusqlite = "0.37.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.138"
tempfile = "3.20.0"
textdistance = "1.1.1"
ureq = { version = "3.0.12", features = ["json"] }
uuid = { version = "1.12.1", features = ["v4", "serde"] }

View File

@@ -1,6 +1,7 @@
use rusqlite;
use std::fs;
use std::path::PathBuf;
use std::str::SplitWhitespace;
use super::deser::ScryfallCard;
use super::utils::get_local_cache_folder;
@@ -12,21 +13,6 @@ fn get_local_data_sqlite_file() -> PathBuf {
folder
}
fn create_db_sql() -> String {
"
CREATE TABLE cards (
name TEXT NOT NULL UNIQUE,
lowercase_name TEXT NOT NULL UNIQUE,
type_line TEXT,
oracle_text TEXT,
power_toughness TEXT,
loyalty INTEGER,
mana_cost TEXT,
scryfall_uri TEXT NOT NULL UNIQUE
);"
.to_string()
}
pub fn get_all_card_names() -> Vec<String> {
let sqlite_file = get_local_data_sqlite_file();
let conn = rusqlite::Connection::open(sqlite_file).unwrap();
@@ -58,6 +44,13 @@ pub fn update_db_with_file(file: PathBuf) -> bool {
let mut conn = rusqlite::Connection::open(sqlite_file).unwrap();
let tx = conn.transaction().unwrap();
for card in ac {
for word in card.name.split_whitespace() {
let res = tx.execute(
"INSERT INTO magic_words (word) VALUES (?1)
ON CONFLICT (word) DO NOTHING;",
[word.replace(",", "")],
);
}
let lowercase_name = card.name.to_lowercase();
let power_toughness = match card.power {
Some(p) => format!("{}/{}", p, card.toughness.unwrap()),
@@ -84,6 +77,23 @@ pub fn update_db_with_file(file: PathBuf) -> bool {
true
}
const CREATE_CARDS_TABLE_SQL: &str = "
CREATE TABLE cards (
name TEXT NOT NULL UNIQUE,
lowercase_name TEXT NOT NULL UNIQUE,
type_line TEXT,
oracle_text TEXT,
power_toughness TEXT,
loyalty INTEGER,
mana_cost TEXT,
scryfall_uri TEXT NOT NULL UNIQUE
)";
const CREATE_MAGIC_WORDS_TABLE_SQL: &str = "
CREATE TABLE magic_words (
word TEXT NOT NULL UNIQUE
)";
// Will delete your current db
pub fn init_db() -> bool {
create_cache_folder();
@@ -94,7 +104,9 @@ pub fn init_db() -> bool {
let _res = fs::remove_file(&sqlite_file);
// TODO actually check result for whether it was a permissions thing or something
let connection = rusqlite::Connection::open(sqlite_file).unwrap();
let init_query = create_db_sql();
connection.execute(&init_query, ()).unwrap();
connection.execute(&CREATE_CARDS_TABLE_SQL, ()).unwrap();
connection
.execute(&CREATE_MAGIC_WORDS_TABLE_SQL, ())
.unwrap();
true
}

View File

@@ -3,6 +3,7 @@ use scryfall_deser::get_all_lowercase_card_names;
use scryfall_deser::get_local_cache_folder;
use scryfall_deser::init_db;
use scryfall_deser::update_db_with_file;
use textdistance::str::damerau_levenshtein;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
@@ -27,14 +28,17 @@ fn main() {
panic!("You need to put some card text to search");
}
let search_string = card_name.join(" ");
dbg!(&search_string);
//dbg!(&search_string);
let cards = get_all_lowercase_card_names();
dbg!(&cards);
//dbg!(&cards);
let mut matching_cards = Vec::new();
for card in cards {
if card.contains(&search_string) {
matching_cards.push(card);
matching_cards.push(card.clone());
}
}
dbg!(matching_cards);
if matching_cards.is_empty() {
// Do some distance checking stuff
}
//dbg!(matching_cards);
}