Added the start of the db create

Need to do insert (obv), recall, search by name (get the whole name column), and
then display nicely.

Also renamed / used more correctly the "data" folder. I should use cache for
the downloaded .json file from scryfall though
This commit is contained in:
2025-08-15 01:47:13 +01:00
parent 4f095c55ec
commit 5dfdff17c1
2 changed files with 60 additions and 16 deletions

View File

@@ -1,34 +1,75 @@
use dir_spec::Dir; use dir_spec::Dir;
use sqlite; use sqlite;
use std::fs; use std::fs;
use std::path::PathBuf;
const CACHE_FOLDER: &str = "scryfall_cache"; const DATA_FOLDER: &str = "scryfall";
const SQLITE_FILENAME: &str = "scryfall_db.sqlite3";
// NOTE: this should be idempotent fn get_local_data_folder() -> PathBuf {
pub fn create_cache_folder() { let cache_folder = Dir::data_home();
let cache_folder = Dir::cache_home();
match cache_folder { match cache_folder {
None => { None => {
panic!("Can't find a cache folder - really don't know what the problem is sorry"); panic!("Can't find a cache folder - really don't know what the problem is sorry");
} }
Some(mut f) => { Some(mut f) => {
f.push(CACHE_FOLDER); f.push(DATA_FOLDER);
let ret = fs::create_dir(&f); f
match ret {
Ok(_) => (),
Err(_e) => {
let err_string = format!(
"Couldn't create folder within your cache folder: {}",
f.display()
);
panic!("{}", err_string);
}
}
} }
} }
} }
fn get_local_data_sqlite_file() -> PathBuf {
let mut folder = get_local_data_folder();
folder.push(SQLITE_FILENAME);
folder
}
// NOTE: this should be idempotent - creating a dir always is... right?
pub fn create_cache_folder() {
let f = get_local_data_folder();
let ret = fs::create_dir(&f);
match ret {
Ok(_) => (),
Err(e) => {
if e.raw_os_error().unwrap() == 17 {
// This is folder already exists - which is fine for us
// TODO probably should use e.kind() for better readability
return;
}
panic!(
"Couldn't create folder within your cache folder: {}. Error is {}",
f.display(),
e
);
}
}
}
fn create_db_sql() -> String {
"
CREATE TABLE cards (
name TEXT NOT NULL UNIQUE,
type_line TEXT,
oracle_text TEXT,
power_toughness TEXT,
loyalty INTEGER,
legalities TEXT,
mana_cost TEXT,
scryfall_uri TEXT NOT NULL UNIQUE
);"
.to_string()
}
// Will delete your current db
pub fn init_db() -> bool { pub fn init_db() -> bool {
create_cache_folder(); create_cache_folder();
let sqlite_file = get_local_data_sqlite_file();
println!("sqlite file location: {}", sqlite_file.display());
let _res = fs::remove_file(&sqlite_file);
// TODO actually check result for whether it was a permissions thing or something
let connection = sqlite::open(sqlite_file).unwrap();
let init_query = create_db_sql();
connection.execute(init_query).unwrap();
true true
} }

View File

@@ -1,4 +1,5 @@
use clap::Parser; use clap::Parser;
use scryfall_deser::init_db;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
@@ -19,4 +20,6 @@ fn main() {
} }
let search_string = card_name.join(" "); let search_string = card_name.join(" ");
dbg!(search_string); dbg!(search_string);
init_db();
} }