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",
]
[[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"

View File

@@ -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"

View File

@@ -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(())
}
}

View File

@@ -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<()> {

View File

@@ -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<String>,
}

View File

@@ -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");
}

View File

@@ -1 +0,0 @@

View File

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

View File

@@ -91,16 +91,14 @@ impl TextArea {
pub fn new(title: &str,
placeholder_text: &str,
validate_fn: fn(&str) -> bool,
style: Option<TextAreaStyle>,
auto_scroll: Option<bool>,
)
-> 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());

View File

@@ -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 }

View File

@@ -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;