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:
@@ -16,6 +16,7 @@ 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,
|
||||
@@ -38,6 +39,18 @@ pub fn get_all_card_names() -> Vec<String> {
|
||||
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 {
|
||||
let ac = fs::read_to_string(file).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 tx = conn.transaction().unwrap();
|
||||
for card in ac {
|
||||
let lowercase_name = card.name.to_lowercase();
|
||||
let power_toughness = match card.power {
|
||||
Some(p) => format!("{}/{}", p, card.toughness.unwrap()),
|
||||
None => "".to_string(),
|
||||
@@ -61,9 +75,9 @@ pub fn update_db_with_file(file: PathBuf) -> bool {
|
||||
Some(mc) => mc,
|
||||
None => "".to_string(),
|
||||
};
|
||||
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)",
|
||||
[card.name, card.type_line, oracle_text, power_toughness, loyalty, mana_cost, card.scryfall_uri],
|
||||
let res = tx.execute(
|
||||
"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, lowercase_name, card.type_line, oracle_text, power_toughness, loyalty, mana_cost, card.scryfall_uri],
|
||||
);
|
||||
}
|
||||
tx.commit();
|
||||
|
||||
@@ -5,7 +5,7 @@ mod deser;
|
||||
pub use crate::deser::ScryfallCard;
|
||||
|
||||
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;
|
||||
pub use utils::get_local_cache_folder;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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::init_db;
|
||||
use scryfall_deser::update_db_with_file;
|
||||
@@ -20,13 +20,21 @@ fn main() {
|
||||
// TODO - actually download and update
|
||||
path.push("oracle-cards-20250814210711.json");
|
||||
update_db_with_file(path);
|
||||
return;
|
||||
}
|
||||
let card_name = args.remainder;
|
||||
if card_name.is_empty() {
|
||||
panic!("You need to put some card text to search");
|
||||
}
|
||||
let search_string = card_name.join(" ");
|
||||
dbg!(search_string);
|
||||
let cards = get_all_card_names();
|
||||
dbg!(cards);
|
||||
dbg!(&search_string);
|
||||
let cards = get_all_lowercase_card_names();
|
||||
dbg!(&cards);
|
||||
let mut matching_cards = Vec::new();
|
||||
for card in cards {
|
||||
if card.contains(&search_string) {
|
||||
matching_cards.push(card);
|
||||
}
|
||||
}
|
||||
dbg!(matching_cards);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user