Remove unused types
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
use crate::config::types::{ApplicationConfig, BasicConfig, PathConfig};
|
||||
use crate::constants::{APP_CONIFG_FILE_PATH, CACHE_MAP};
|
||||
use color_eyre::Result;
|
||||
use std::path::PathBuf;
|
||||
use language_tags::LanguageTag;
|
||||
use ratatui::widgets::ListState;
|
||||
use serde::Deserialize;
|
||||
use serde_json;
|
||||
|
||||
pub mod types;
|
||||
|
||||
const CONFIG_KEY: &str = "app_conf";
|
||||
|
||||
impl ApplicationConfig {
|
||||
pub fn get_config() -> Result<Self> {
|
||||
if CACHE_MAP.contains_key(CONFIG_KEY) &&
|
||||
let Some(cached_config) = CACHE_MAP.get(CONFIG_KEY)
|
||||
{
|
||||
Ok(serde_json::from_value(cached_config.clone())?)
|
||||
} else if APP_CONIFG_FILE_PATH.exists() {
|
||||
ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH)
|
||||
} else {
|
||||
ApplicationConfig::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn from_file(path: &PathBuf) -> Result<Self> {
|
||||
let reader = std::fs::File::open(path)?;
|
||||
let result: serde_json::Value = serde_json::from_reader(reader)?;
|
||||
CACHE_MAP.insert(CONFIG_KEY.to_string(), result.clone());
|
||||
Ok(serde_json::from_value(result)?)
|
||||
}
|
||||
|
||||
fn new() -> Result<Self> {
|
||||
let default_locale = sys_locale::get_locale().unwrap_or(String::from("ja-JP"));
|
||||
let conf = Self {
|
||||
basic_config: BasicConfig {
|
||||
tick_rate: 250,
|
||||
locale: LanguageTag::parse(&default_locale)?,
|
||||
},
|
||||
path_config: PathConfig {
|
||||
dlsite_paths: vec![],
|
||||
},
|
||||
};
|
||||
conf.clone().write_to_file(APP_CONIFG_FILE_PATH.to_path_buf())?;
|
||||
Ok(conf)
|
||||
}
|
||||
|
||||
fn write_to_file(self, path: PathBuf) -> Result<()> {
|
||||
let writer = std::fs::File::create(path)?;
|
||||
serde_json::to_writer_pretty(writer, &self)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn save(self) -> Result<()> {
|
||||
let current_value = serde_json::to_value(&self)?;
|
||||
CACHE_MAP.alter(CONFIG_KEY, |_, _| current_value);
|
||||
self.write_to_file(APP_CONIFG_FILE_PATH.to_path_buf())
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
use language_tags::LanguageTag;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct ApplicationConfig {
|
||||
pub basic_config: BasicConfig,
|
||||
pub path_config: PathConfig,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub(crate) struct BasicConfig {
|
||||
pub locale: LanguageTag,
|
||||
pub tick_rate: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct PathConfig {
|
||||
pub dlsite_paths: Vec<String>
|
||||
}
|
||||
Reference in New Issue
Block a user