J.A.R.V.I.S-py/crates/jarvis-core/src/db.rs

59 lines
1.4 KiB
Rust
Raw Normal View History

pub mod structs;
use crate::{config, APP_CONFIG_DIR};
2025-12-11 23:43:50 +05:00
use log::info;
use std::fs::File;
use std::io::{BufReader, Read};
2025-12-11 23:43:50 +05:00
use std::path::PathBuf;
use serde_json;
fn get_db_file_path() -> PathBuf {
2025-12-11 23:43:50 +05:00
PathBuf::from(format!(
"{}/{}",
APP_CONFIG_DIR.get().unwrap().display(),
config::DB_FILE_NAME
))
}
pub fn init_settings() -> structs::Settings {
let mut db = None;
let db_file_path = get_db_file_path();
2025-12-11 23:43:50 +05:00
info!(
"Loading settings db file located at: {}",
db_file_path.display()
);
if db_file_path.exists() {
// try load existing settings
if let Ok(mut db_file) = File::open(db_file_path) {
let reader = BufReader::new(db_file);
if let Ok(parsed_json) = serde_json::from_reader(reader) {
info!("Settings loaded.");
db = Some(parsed_json);
}
}
}
if db.is_none() {
// create default settings db file
warn!("No settings file found or there was an error parsing it. Creating default struct.");
db = Some(structs::Settings::default());
}
db.unwrap()
}
pub fn save_settings(settings: &structs::Settings) -> Result<(), std::io::Error> {
let db_file_path = get_db_file_path();
std::fs::write(
2026-01-04 09:00:51 +05:00
&db_file_path,
2025-12-11 23:43:50 +05:00
serde_json::to_string_pretty(&settings).unwrap(),
)?;
2026-01-04 09:00:51 +05:00
info!("Settings saved to: {:#}", db_file_path.display());
Ok(())
2025-12-11 23:43:50 +05:00
}