Change to use json for config

cleanup unused codes
This commit is contained in:
2025-10-17 16:29:36 +08:00
parent 71122e87ae
commit fea4e8d35e
13 changed files with 42 additions and 117 deletions

67
Cargo.lock generated
View File

@@ -276,26 +276,6 @@ dependencies = [
"static_assertions", "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]] [[package]]
name = "convert_case" name = "convert_case"
version = "0.7.1" version = "0.7.1"
@@ -380,12 +360,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "crunchy"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
[[package]] [[package]]
name = "cssparser" name = "cssparser"
version = "0.35.0" version = "0.35.0"
@@ -589,15 +563,6 @@ dependencies = [
"syn", "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]] [[package]]
name = "document-features" name = "document-features"
version = "0.2.11" version = "0.2.11"
@@ -1522,16 +1487,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" 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]] [[package]]
name = "owo-colors" name = "owo-colors"
version = "4.2.3" version = "4.2.3"
@@ -1837,16 +1792,6 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbc52377db80e3fec3a2c748ca603b8b6cacdd34ff89ff4b742a635361d4b4a7" 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]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.26" version = "0.1.26"
@@ -2244,8 +2189,9 @@ dependencies = [
"ratatui", "ratatui",
"reqwest", "reqwest",
"robotstxt", "robotstxt",
"rust-ini",
"scraper", "scraper",
"serde",
"serde_json",
"tokio", "tokio",
"tokio-util", "tokio-util",
"tokio-utils", "tokio-utils",
@@ -2391,15 +2337,6 @@ dependencies = [
"time-core", "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]] [[package]]
name = "tinystr" name = "tinystr"
version = "0.8.1" version = "0.8.1"

View File

@@ -13,10 +13,14 @@ tokio-util = "0.7.9"
tokio-utils = "0.1.2" tokio-utils = "0.1.2"
directories = "6.0.0" directories = "6.0.0"
lazy_static = "1.5.0" lazy_static = "1.5.0"
rust-ini = "0.21.3"
robotstxt = "0.3.0" robotstxt = "0.3.0"
scraper = "0.24.0" scraper = "0.24.0"
rat-cursor = "1.2.1" rat-cursor = "1.2.1"
serde_json = "1.0.145"
[dependencies.serde]
version = "1.0.228"
features = ["derive"]
[dependencies.tui-input] [dependencies.tui-input]
version = "0.14.0" version = "0.14.0"

View File

@@ -5,6 +5,7 @@ use clap::{command, Args, Command, Parser, Subcommand};
use color_eyre::Result; use color_eyre::Result;
use ratatui::crossterm; use ratatui::crossterm;
use std::path::PathBuf; use std::path::PathBuf;
use color_eyre::eyre::eyre;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
struct FolderAddCommand { struct FolderAddCommand {
@@ -111,11 +112,15 @@ impl FolderAddCommand {
let mut config = ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH.to_path_buf())?; let mut config = ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH.to_path_buf())?;
let path = PathBuf::from(&self.path); let path = PathBuf::from(&self.path);
let abs_path = path.canonicalize()?; let abs_path = path.canonicalize()?;
if !abs_path.is_dir() {
return Err(eyre!("{:?} is not a directory", abs_path));
}
config config
.path_config .path_config
.dlsite_paths .dlsite_paths
.push(abs_path.to_str().unwrap().to_string()); .push(abs_path.to_str().unwrap().to_string());
config.save()?; config.save()?;
println!("Added {:?} to path config", abs_path);
Ok(()) Ok(())
} }
} }

View File

@@ -1,32 +1,16 @@
use crate::config::types::{ApplicationConfig, BasicConfig, PathConfig}; use crate::config::types::{ApplicationConfig, BasicConfig, PathConfig};
use crate::constants::{APP_CONIFG_FILE_PATH, APP_DATA_DIR}; use crate::constants::{APP_CONIFG_FILE_PATH, APP_DATA_DIR};
use color_eyre::Result; use color_eyre::Result;
use ini::Ini;
use std::path::PathBuf; use std::path::PathBuf;
use serde_json;
pub mod types; pub mod types;
impl ApplicationConfig { impl ApplicationConfig {
pub fn from_file(path: &PathBuf) -> Result<Self> { pub fn from_file(path: &PathBuf) -> Result<Self> {
let conf = Ini::load_from_file(path)?; let reader = std::fs::File::open(path)?;
let basic_conf_section = conf.section(Some("Basic")).unwrap(); let result = serde_json::from_reader(reader)?;
let basic_conf = BasicConfig { Ok(result)
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,
})
} }
pub fn new() -> Self { pub fn new() -> Self {
@@ -51,21 +35,10 @@ impl ApplicationConfig {
} }
fn write_to_file(self, path: &PathBuf) -> Result<()> { fn write_to_file(self, path: &PathBuf) -> Result<()> {
let mut conf = Ini::new(); let writer = std::fs::File::create(path)?;
conf.with_section(Some("Basic")) let result = serde_json::to_writer_pretty(writer, &self)?;
.set("DBPath", self.basic_config.db_path) Ok(result)
.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(())
} }
pub fn save(&self) -> Result<()> { pub fn save(&self) -> Result<()> {

View File

@@ -1,16 +1,18 @@
#[derive(Clone)] use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct ApplicationConfig { pub(crate) struct ApplicationConfig {
pub(crate) basic_config: BasicConfig, pub(crate) basic_config: BasicConfig,
pub(crate) path_config: PathConfig, pub(crate) path_config: PathConfig,
} }
#[derive(Clone)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct BasicConfig { pub(crate) struct BasicConfig {
pub(crate) db_path: String, pub(crate) db_path: String,
pub(crate) tick_rate: u64, pub(crate) tick_rate: u64,
} }
#[derive(Clone)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct PathConfig { pub(crate) struct PathConfig {
pub(crate) dlsite_paths: Vec<String>, pub(crate) dlsite_paths: Vec<String>,
} }

View File

@@ -9,5 +9,5 @@ lazy_static! {
BASE_DIRS.config_dir().to_path_buf().join(APP_DIR_NAME); 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_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_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");
} }

View File

@@ -1 +0,0 @@

View File

@@ -6,7 +6,7 @@ mod crawler;
mod event; mod event;
mod helpers; mod helpers;
mod schema; mod schema;
mod types; mod models;
mod widgets; mod widgets;
use crate::cli::Cli; use crate::cli::Cli;

View File

@@ -91,16 +91,14 @@ impl TextArea {
pub fn new(title: &str, pub fn new(title: &str,
placeholder_text: &str, placeholder_text: &str,
validate_fn: fn(&str) -> bool, validate_fn: fn(&str) -> bool,
style: Option<TextAreaStyle>,
auto_scroll: Option<bool>,
) )
-> Self -> Self
{ {
let func = Arc::new(validate_fn); let func = Arc::new(validate_fn);
Self { Self {
title: title.to_string(), title: title.to_string(),
style: style.unwrap_or(TextAreaStyle::SingleLine), style: TextAreaStyle::SingleLine,
auto_scroll: auto_scroll.unwrap_or(true), auto_scroll: true,
validate_fn: func, validate_fn: func,
state: TextAreaState { state: TextAreaState {
input: Input::new(placeholder_text.to_string()), 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<()> { pub fn handle_input(&mut self, event: &Event) -> Result<()> {
let _ = self.state.input.handle_event(event); let _ = self.state.input.handle_event(event);
self.state.is_valid = (self.validate_fn)(self.state.input.value()); self.state.is_valid = (self.validate_fn)(self.state.input.value());

View File

@@ -18,9 +18,7 @@ impl AddFolderPopup {
|x| { |x| {
let path = Path::new(x); let path = Path::new(x);
path.exists() && path.is_dir() path.exists() && path.is_dir()
}, }
None,
None
); );
textarea.state.is_active = true; textarea.state.is_active = true;
Self { textarea } Self { textarea }

View File

@@ -2,9 +2,8 @@ mod main_view;
use crossterm::event::{Event, KeyEvent}; use crossterm::event::{Event, KeyEvent};
pub use main_view::MainView; 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_input(&mut self, event: &Event) -> color_eyre::Result<()>;
fn handle_key_input(&mut self, key: &KeyEvent) -> color_eyre::Result<()>; fn handle_key_input(&mut self, key: &KeyEvent) -> color_eyre::Result<()>;
fn is_running(&self) -> bool; fn is_running(&self) -> bool;