Change to use json for config
cleanup unused codes
This commit is contained in:
@@ -1,32 +1,16 @@
|
||||
use crate::config::types::{ApplicationConfig, BasicConfig, PathConfig};
|
||||
use crate::constants::{APP_CONIFG_FILE_PATH, APP_DATA_DIR};
|
||||
use color_eyre::Result;
|
||||
use ini::Ini;
|
||||
use std::path::PathBuf;
|
||||
use serde_json;
|
||||
|
||||
pub mod types;
|
||||
|
||||
impl ApplicationConfig {
|
||||
pub fn from_file(path: &PathBuf) -> Result<Self> {
|
||||
let conf = Ini::load_from_file(path)?;
|
||||
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()?,
|
||||
};
|
||||
let path_conf_section = conf.section(Some("Path")).unwrap();
|
||||
let path_conf = PathConfig {
|
||||
dlsite_paths: path_conf_section
|
||||
.get("DLSite")
|
||||
.unwrap()
|
||||
.split(":")
|
||||
.map(|s| s.to_string())
|
||||
.collect(),
|
||||
};
|
||||
Ok(Self {
|
||||
basic_config: basic_conf,
|
||||
path_config: path_conf,
|
||||
})
|
||||
let reader = std::fs::File::open(path)?;
|
||||
let result = serde_json::from_reader(reader)?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
@@ -51,21 +35,10 @@ impl ApplicationConfig {
|
||||
}
|
||||
|
||||
fn write_to_file(self, path: &PathBuf) -> Result<()> {
|
||||
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.with_section(Some("Path")).set(
|
||||
"DLSite",
|
||||
self.path_config
|
||||
.dlsite_paths
|
||||
.into_iter()
|
||||
.filter(|x| !x.is_empty())
|
||||
.collect::<Vec<String>>()
|
||||
.join(":"),
|
||||
);
|
||||
conf.write_to_file(path)?;
|
||||
Ok(())
|
||||
let writer = std::fs::File::create(path)?;
|
||||
let result = serde_json::to_writer_pretty(writer, &self)?;
|
||||
Ok(result)
|
||||
|
||||
}
|
||||
|
||||
pub fn save(&self) -> Result<()> {
|
||||
|
||||
Reference in New Issue
Block a user