From c7bacec1196d2580c679252db9f99c42ffe40460 Mon Sep 17 00:00:00 2001 From: honhimW Date: Thu, 12 Dec 2024 19:03:44 +0800 Subject: [PATCH] feat: #[serde(default = "xx")] --- examples/theme.rs | 16 ++++------------ src/configuration.rs | 30 +++++++++++++++++++++++------- src/main.rs | 2 +- src/tabs/explorer.rs | 8 ++------ 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/examples/theme.rs b/examples/theme.rs index 4d4d127..f017999 100644 --- a/examples/theme.rs +++ b/examples/theme.rs @@ -1,21 +1,13 @@ -use anyhow::Result; -use ratatui::style::palette::tailwind; -use ron::ser::PrettyConfig; -use ratisui::theme::{get_color, Color, Tab, TailwindColor, TailwindPalette, Theme}; -use ratisui::theme::TailwindPalette::C100; +use anyhow::{Context, Result}; +use ratisui::theme::{Color, Tab, Theme}; fn main() -> Result<()> { let mut theme = Theme::default(); let mut tab = Tab::default(); theme.tab = tab; - let result = ron::ser::to_string_pretty(&theme, PrettyConfig::default())?; - println!("{}", result); - - let color = get_color(|t| &t.tab.explorer.accent); - println!("{:?}", color); - - println!("{:?}", Color::hex("ffffff").to_color()); + let r_color = Color::hex("ffffff").to_color().context("")?; + assert!(matches!(r_color, ratatui::style::Color::Rgb(255,255,255))); Ok(()) } \ No newline at end of file diff --git a/src/configuration.rs b/src/configuration.rs index a561601..8ca2610 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -112,24 +112,40 @@ fn get_file_path>(file_name: T) -> Result { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct Configuration { - pub fps: Option, - pub scan_size: Option, - pub try_format: Option, + #[serde(default = "fps")] + pub fps: u8, + #[serde(default = "scan_size")] + pub scan_size: u16, + #[serde(default = "try_format")] + pub try_format: bool, pub theme: Option, } +fn fps() -> u8 { + 30 +} + +fn scan_size() -> u16 { + 2_000 +} + +fn try_format() -> bool { + false +} + + #[derive(Serialize, Deserialize)] pub struct Databases { pub default_database: Option, pub databases: HashMap, } -impl Configuration { +impl Default for Configuration { fn default() -> Self { Self { - fps: Some(30), - scan_size: Some(2_000), - try_format: Some(false), + fps: fps(), + scan_size: scan_size(), + try_format: try_format(), theme: None, } } diff --git a/src/main.rs b/src/main.rs index a77322b..1682aae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -113,7 +113,7 @@ async fn main() -> Result<()> { } async fn run(mut app: App, mut terminal: TerminalBackEnd, config: Configuration) -> Result<()> { - let fps = cmp::min(config.fps.clone().unwrap_or(30) as usize, 60); + let fps = cmp::min(config.fps as usize, 60); let delay_millis = 1000 / fps; let delay_duration = Duration::from_millis(delay_millis as u64); let mut fps_calculator = FpsCalculator::default(); diff --git a/src/tabs/explorer.rs b/src/tabs/explorer.rs index 2f0bf11..e38a30f 100644 --- a/src/tabs/explorer.rs +++ b/src/tabs/explorer.rs @@ -1215,12 +1215,8 @@ impl Listenable for ExplorerTab { fn on_app_event(&mut self, _app_event: AppEvent) -> Result<()> { match _app_event { AppEvent::InitConfig(configuration) => { - if let Some(scan_size) = configuration.scan_size { - self.scan_size = scan_size; - } - if let Some(try_format) = configuration.try_format { - self.try_format = try_format; - } + self.scan_size = configuration.scan_size; + self.try_format = configuration.try_format; } AppEvent::Reset => { self.show_delete_popup = false;