Add sqlite and config support

This commit is contained in:
2025-10-09 11:33:06 +08:00
parent 5053d19d73
commit 10b89aee17
14 changed files with 758 additions and 33 deletions

36
src/config/mod.rs Normal file
View File

@@ -0,0 +1,36 @@
use std::path::{PathBuf};
use ini::Ini;
use crate::config::types::{ApplicationConfig, BasicConfig};
pub mod types;
impl ApplicationConfig {
pub fn from_file(path: &PathBuf) -> Self {
let conf = Ini::load_from_file(path).unwrap();
let basic_conf_section = conf.section(Some("Basic")).unwrap();
let basic_conf = BasicConfig {
db_path: basic_conf_section.get("DBPath").unwrap().to_string(),
tick_rate: basic_conf_section.get("TickRate").unwrap().parse().unwrap(),
};
Self {
basic_config: basic_conf
}
}
pub fn new() -> Self {
Self {
basic_config: BasicConfig {
db_path: "games.db".to_string(),
tick_rate: 250
}
}
}
pub fn write_to_file(self, path: PathBuf) {
let mut conf = Ini::new();
conf.with_section(Some("Basic"))
.set("DBPath", self.basic_config.db_path)
.set("TickRate", self.basic_config.tick_rate.to_string());
conf.write_to_file(path).unwrap();
}
}