From fea4e8d35eab450cbc2cc43b79d30c7316fa1f12 Mon Sep 17 00:00:00 2001 From: fromost Date: Fri, 17 Oct 2025 16:29:36 +0800 Subject: [PATCH] Change to use json for config cleanup unused codes --- Cargo.lock | 67 +----------------------------- Cargo.toml | 6 ++- src/cli.rs | 5 +++ src/config/mod.rs | 43 ++++--------------- src/config/types.rs | 8 ++-- src/constants.rs | 2 +- src/helpers/mod.rs | 1 - src/main.rs | 4 +- src/{types => models}/game.rs | 0 src/{types => models}/mod.rs | 0 src/widgets/components/textarea.rs | 16 +++++-- src/widgets/popups/folder.rs | 4 +- src/widgets/views/mod.rs | 3 +- 13 files changed, 42 insertions(+), 117 deletions(-) rename src/{types => models}/game.rs (100%) rename src/{types => models}/mod.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 5a98b12..d92f77b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -276,26 +276,6 @@ dependencies = [ "static_assertions", ] -[[package]] -name = "const-random" -version = "0.1.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359" -dependencies = [ - "const-random-macro", -] - -[[package]] -name = "const-random-macro" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e" -dependencies = [ - "getrandom 0.2.16", - "once_cell", - "tiny-keccak", -] - [[package]] name = "convert_case" version = "0.7.1" @@ -380,12 +360,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "crunchy" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" - [[package]] name = "cssparser" version = "0.35.0" @@ -589,15 +563,6 @@ dependencies = [ "syn", ] -[[package]] -name = "dlv-list" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "442039f5147480ba31067cb00ada1adae6892028e40e45fc5de7b7df6dcc1b5f" -dependencies = [ - "const-random", -] - [[package]] name = "document-features" version = "0.2.11" @@ -1522,16 +1487,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" -[[package]] -name = "ordered-multimap" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49203cdcae0030493bad186b28da2fa25645fa276a51b6fec8010d281e02ef79" -dependencies = [ - "dlv-list", - "hashbrown 0.14.5", -] - [[package]] name = "owo-colors" version = "4.2.3" @@ -1837,16 +1792,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbc52377db80e3fec3a2c748ca603b8b6cacdd34ff89ff4b742a635361d4b4a7" -[[package]] -name = "rust-ini" -version = "0.21.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "796e8d2b6696392a43bea58116b667fb4c29727dc5abd27d6acf338bb4f688c7" -dependencies = [ - "cfg-if", - "ordered-multimap", -] - [[package]] name = "rustc-demangle" version = "0.1.26" @@ -2244,8 +2189,9 @@ dependencies = [ "ratatui", "reqwest", "robotstxt", - "rust-ini", "scraper", + "serde", + "serde_json", "tokio", "tokio-util", "tokio-utils", @@ -2391,15 +2337,6 @@ dependencies = [ "time-core", ] -[[package]] -name = "tiny-keccak" -version = "2.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" -dependencies = [ - "crunchy", -] - [[package]] name = "tinystr" version = "0.8.1" diff --git a/Cargo.toml b/Cargo.toml index fc4c8e0..fa985f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,10 +13,14 @@ tokio-util = "0.7.9" tokio-utils = "0.1.2" directories = "6.0.0" lazy_static = "1.5.0" -rust-ini = "0.21.3" robotstxt = "0.3.0" scraper = "0.24.0" rat-cursor = "1.2.1" +serde_json = "1.0.145" + +[dependencies.serde] +version = "1.0.228" +features = ["derive"] [dependencies.tui-input] version = "0.14.0" diff --git a/src/cli.rs b/src/cli.rs index 2311410..d6922cd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -5,6 +5,7 @@ use clap::{command, Args, Command, Parser, Subcommand}; use color_eyre::Result; use ratatui::crossterm; use std::path::PathBuf; +use color_eyre::eyre::eyre; #[derive(Parser, Debug)] struct FolderAddCommand { @@ -111,11 +112,15 @@ impl FolderAddCommand { let mut config = ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH.to_path_buf())?; let path = PathBuf::from(&self.path); let abs_path = path.canonicalize()?; + if !abs_path.is_dir() { + return Err(eyre!("{:?} is not a directory", abs_path)); + } config .path_config .dlsite_paths .push(abs_path.to_str().unwrap().to_string()); config.save()?; + println!("Added {:?} to path config", abs_path); Ok(()) } } diff --git a/src/config/mod.rs b/src/config/mod.rs index 2dbe1da..a3e3c5d 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -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 { - 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::>() - .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<()> { diff --git a/src/config/types.rs b/src/config/types.rs index dc97b81..f5cfe59 100644 --- a/src/config/types.rs +++ b/src/config/types.rs @@ -1,16 +1,18 @@ -#[derive(Clone)] +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Serialize, Deserialize)] pub(crate) struct ApplicationConfig { pub(crate) basic_config: BasicConfig, pub(crate) path_config: PathConfig, } -#[derive(Clone)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub(crate) struct BasicConfig { pub(crate) db_path: String, pub(crate) tick_rate: u64, } -#[derive(Clone)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub(crate) struct PathConfig { pub(crate) dlsite_paths: Vec, } diff --git a/src/constants.rs b/src/constants.rs index a9287a6..286bc98 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -9,5 +9,5 @@ lazy_static! { BASE_DIRS.config_dir().to_path_buf().join(APP_DIR_NAME); pub static ref APP_DATA_DIR: PathBuf = BASE_DIRS.data_dir().to_path_buf().join(APP_DIR_NAME); pub static ref APP_CACHE_PATH: PathBuf = BASE_DIRS.cache_dir().to_path_buf().join(APP_DIR_NAME); - pub static ref APP_CONIFG_FILE_PATH: PathBuf = APP_CONFIG_DIR.clone().join("config.ini"); + pub static ref APP_CONIFG_FILE_PATH: PathBuf = APP_CONFIG_DIR.clone().join("config.json"); } \ No newline at end of file diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 8b13789..e69de29 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -1 +0,0 @@ - diff --git a/src/main.rs b/src/main.rs index 934e534..a252ffb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ mod crawler; mod event; mod helpers; mod schema; -mod types; +mod models; mod widgets; use crate::cli::Cli; @@ -19,4 +19,4 @@ async fn main() -> Result<()> { color_eyre::install()?; let cli = Cli::parse(); cli.run().await -} +} \ No newline at end of file diff --git a/src/types/game.rs b/src/models/game.rs similarity index 100% rename from src/types/game.rs rename to src/models/game.rs diff --git a/src/types/mod.rs b/src/models/mod.rs similarity index 100% rename from src/types/mod.rs rename to src/models/mod.rs diff --git a/src/widgets/components/textarea.rs b/src/widgets/components/textarea.rs index 5d6e2b4..2a8bf63 100644 --- a/src/widgets/components/textarea.rs +++ b/src/widgets/components/textarea.rs @@ -91,16 +91,14 @@ impl TextArea { pub fn new(title: &str, placeholder_text: &str, validate_fn: fn(&str) -> bool, - style: Option, - auto_scroll: Option, ) -> Self { let func = Arc::new(validate_fn); Self { title: title.to_string(), - style: style.unwrap_or(TextAreaStyle::SingleLine), - auto_scroll: auto_scroll.unwrap_or(true), + style: TextAreaStyle::SingleLine, + auto_scroll: true, validate_fn: func, state: TextAreaState { input: Input::new(placeholder_text.to_string()), @@ -112,6 +110,16 @@ impl TextArea { } } + pub fn display_style(mut self, style: TextAreaStyle) -> Self { + self.style = style; + self + } + + pub fn set_auto_scroll(mut self, auto_scroll: bool) -> Self { + self.auto_scroll = auto_scroll; + self + } + pub fn handle_input(&mut self, event: &Event) -> Result<()> { let _ = self.state.input.handle_event(event); self.state.is_valid = (self.validate_fn)(self.state.input.value()); diff --git a/src/widgets/popups/folder.rs b/src/widgets/popups/folder.rs index 1374123..f60427c 100644 --- a/src/widgets/popups/folder.rs +++ b/src/widgets/popups/folder.rs @@ -18,9 +18,7 @@ impl AddFolderPopup { |x| { let path = Path::new(x); path.exists() && path.is_dir() - }, - None, - None + } ); textarea.state.is_active = true; Self { textarea } diff --git a/src/widgets/views/mod.rs b/src/widgets/views/mod.rs index 826a6ba..2640f77 100644 --- a/src/widgets/views/mod.rs +++ b/src/widgets/views/mod.rs @@ -2,9 +2,8 @@ mod main_view; use crossterm::event::{Event, KeyEvent}; pub use main_view::MainView; -use std::any::Any; -pub trait View: Any + 'static { +pub trait View { fn handle_input(&mut self, event: &Event) -> color_eyre::Result<()>; fn handle_key_input(&mut self, key: &KeyEvent) -> color_eyre::Result<()>; fn is_running(&self) -> bool;