Added a utils file

Wanted to separate the higher level config folder stuff from just db module

Because the download module will also want to find cache
This commit is contained in:
2025-08-15 02:00:06 +01:00
parent 5dfdff17c1
commit 1462401787
3 changed files with 62 additions and 37 deletions

View File

@@ -1,23 +1,8 @@
use dir_spec::Dir;
use sqlite;
use std::fs;
use std::path::PathBuf;
const DATA_FOLDER: &str = "scryfall";
const SQLITE_FILENAME: &str = "scryfall_db.sqlite3";
fn get_local_data_folder() -> PathBuf {
let cache_folder = Dir::data_home();
match cache_folder {
None => {
panic!("Can't find a cache folder - really don't know what the problem is sorry");
}
Some(mut f) => {
f.push(DATA_FOLDER);
f
}
}
}
use super::utils::{create_cache_folder, get_local_data_folder, SQLITE_FILENAME};
fn get_local_data_sqlite_file() -> PathBuf {
let mut folder = get_local_data_folder();
@@ -25,27 +10,6 @@ fn get_local_data_sqlite_file() -> PathBuf {
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 (