Matching substrings works

Want to try also using something like this for finding spelling mistakes etc.
https://github.com/life4/textdistance.rs

Going to have to try to do some combination though to ensure exact substring
matches, even when missing the latter half, still work well. Maybe... I dunno
will have to try.
This commit is contained in:
2025-08-16 21:38:34 +01:00
parent 72fa35d41a
commit 6558a31619
3 changed files with 30 additions and 8 deletions

View File

@@ -16,6 +16,7 @@ fn create_db_sql() -> String {
" "
CREATE TABLE cards ( CREATE TABLE cards (
name TEXT NOT NULL UNIQUE, name TEXT NOT NULL UNIQUE,
lowercase_name TEXT NOT NULL UNIQUE,
type_line TEXT, type_line TEXT,
oracle_text TEXT, oracle_text TEXT,
power_toughness TEXT, power_toughness TEXT,
@@ -38,6 +39,18 @@ pub fn get_all_card_names() -> Vec<String> {
card_names card_names
} }
pub fn get_all_lowercase_card_names() -> Vec<String> {
let sqlite_file = get_local_data_sqlite_file();
let conn = rusqlite::Connection::open(sqlite_file).unwrap();
let mut stmt = conn.prepare("SELECT lowercase_name FROM cards;").unwrap();
let mut rows = stmt.query([]).unwrap();
let mut card_names = Vec::new();
while let Some(row) = rows.next().unwrap() {
card_names.push(row.get(0).unwrap());
}
card_names
}
pub fn update_db_with_file(file: PathBuf) -> bool { pub fn update_db_with_file(file: PathBuf) -> bool {
let ac = fs::read_to_string(file).unwrap(); let ac = fs::read_to_string(file).unwrap();
let ac: Vec<ScryfallCard> = serde_json::from_str(&ac).unwrap(); let ac: Vec<ScryfallCard> = serde_json::from_str(&ac).unwrap();
@@ -45,6 +58,7 @@ pub fn update_db_with_file(file: PathBuf) -> bool {
let mut conn = rusqlite::Connection::open(sqlite_file).unwrap(); let mut conn = rusqlite::Connection::open(sqlite_file).unwrap();
let tx = conn.transaction().unwrap(); let tx = conn.transaction().unwrap();
for card in ac { for card in ac {
let lowercase_name = card.name.to_lowercase();
let power_toughness = match card.power { let power_toughness = match card.power {
Some(p) => format!("{}/{}", p, card.toughness.unwrap()), Some(p) => format!("{}/{}", p, card.toughness.unwrap()),
None => "".to_string(), None => "".to_string(),
@@ -61,9 +75,9 @@ pub fn update_db_with_file(file: PathBuf) -> bool {
Some(mc) => mc, Some(mc) => mc,
None => "".to_string(), None => "".to_string(),
}; };
tx.execute( let res = tx.execute(
"INSERT INTO cards (name, type_line, oracle_text, power_toughness, loyalty, mana_cost, scryfall_uri) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)", "INSERT INTO cards (name, lowercase_name, type_line, oracle_text, power_toughness, loyalty, mana_cost, scryfall_uri) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
[card.name, card.type_line, oracle_text, power_toughness, loyalty, mana_cost, card.scryfall_uri], [card.name, lowercase_name, card.type_line, oracle_text, power_toughness, loyalty, mana_cost, card.scryfall_uri],
); );
} }
tx.commit(); tx.commit();

View File

@@ -5,7 +5,7 @@ mod deser;
pub use crate::deser::ScryfallCard; pub use crate::deser::ScryfallCard;
mod db; mod db;
pub use db::{get_all_card_names, init_db, update_db_with_file}; pub use db::{get_all_lowercase_card_names, init_db, update_db_with_file};
mod utils; mod utils;
pub use utils::get_local_cache_folder; pub use utils::get_local_cache_folder;

View File

@@ -1,5 +1,5 @@
use clap::Parser; use clap::Parser;
use scryfall_deser::get_all_card_names; use scryfall_deser::get_all_lowercase_card_names;
use scryfall_deser::get_local_cache_folder; use scryfall_deser::get_local_cache_folder;
use scryfall_deser::init_db; use scryfall_deser::init_db;
use scryfall_deser::update_db_with_file; use scryfall_deser::update_db_with_file;
@@ -20,13 +20,21 @@ fn main() {
// TODO - actually download and update // TODO - actually download and update
path.push("oracle-cards-20250814210711.json"); path.push("oracle-cards-20250814210711.json");
update_db_with_file(path); update_db_with_file(path);
return;
} }
let card_name = args.remainder; let card_name = args.remainder;
if card_name.is_empty() { if card_name.is_empty() {
panic!("You need to put some card text to search"); panic!("You need to put some card text to search");
} }
let search_string = card_name.join(" "); let search_string = card_name.join(" ");
dbg!(search_string); dbg!(&search_string);
let cards = get_all_card_names(); 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);
}
}
dbg!(matching_cards);
} }