60 lines
1.7 KiB
Rust
60 lines
1.7 KiB
Rust
use crate::config::types::{ApplicationConfig, BasicConfig, PathConfig};
|
|
use crate::constants::{APP_CONIFG_FILE_PATH, APP_DB_DATA_DIR};
|
|
use color_eyre::Result;
|
|
use std::path::PathBuf;
|
|
use language_tags::LanguageTag;
|
|
use ratatui::widgets::ListState;
|
|
use serde_json;
|
|
|
|
pub mod types;
|
|
|
|
pub(crate) struct GameList<T> {
|
|
games: Vec<T>,
|
|
state: ListState,
|
|
}
|
|
|
|
impl ApplicationConfig {
|
|
pub fn get_config() -> Result<Self> {
|
|
if APP_CONIFG_FILE_PATH.exists() {
|
|
ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH)
|
|
} else {
|
|
Ok(ApplicationConfig::new())
|
|
}
|
|
}
|
|
|
|
fn from_file(path: &PathBuf) -> Result<Self> {
|
|
let reader = std::fs::File::open(path)?;
|
|
let result = serde_json::from_reader(reader)?;
|
|
Ok(result)
|
|
}
|
|
|
|
fn new() -> Self {
|
|
let default_locale = sys_locale::get_locale().unwrap_or(String::from("ja-JP"));
|
|
let conf = Self {
|
|
basic_config: BasicConfig {
|
|
db_path: APP_DB_DATA_DIR.to_str().unwrap().to_string(),
|
|
tick_rate: 250,
|
|
locale: LanguageTag::parse(&default_locale).unwrap(),
|
|
},
|
|
path_config: PathConfig {
|
|
dlsite_paths: vec![],
|
|
},
|
|
};
|
|
conf.clone()
|
|
.write_to_file(&APP_CONIFG_FILE_PATH.to_path_buf())
|
|
.unwrap();
|
|
conf
|
|
}
|
|
|
|
fn write_to_file(self, path: &PathBuf) -> Result<()> {
|
|
let writer = std::fs::File::create(path)?;
|
|
let result = serde_json::to_writer_pretty(writer, &self)?;
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
pub fn save(&self) -> Result<()> {
|
|
self.clone().write_to_file(&APP_CONIFG_FILE_PATH.to_path_buf())
|
|
}
|
|
}
|