diff options
| author | lukeflo | 2025-02-22 00:48:41 +0100 |
|---|---|---|
| committer | lukeflo | 2025-02-22 00:48:41 +0100 |
| commit | 63d4410fdfe7712faec287aee2f5c0ca288dc996 (patch) | |
| tree | cdc1c3a2fa012c9c194766c256572129ae069a92 /src/config.rs | |
| parent | 9cfed78e434a165d4cc01a869dfa7066b8b29819 (diff) | |
| download | bibiman-63d4410fdfe7712faec287aee2f5c0ca288dc996.tar.gz bibiman-63d4410fdfe7712faec287aee2f5c0ca288dc996.zip | |
replace Config crate with Figment -> better merging of default and config values
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 164 |
1 files changed, 56 insertions, 108 deletions
diff --git a/src/config.rs b/src/config.rs index faba5d8..e2c34b9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,147 +18,95 @@ use std::path::PathBuf; use color_eyre::eyre::Result; -use config::{ConfigError, FileFormat}; +use figment::{ + providers::{Format, Serialized, Toml}, + Figment, +}; use ratatui::style::Color; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use crate::cliargs::CLIArgs; /// Main struct of the config file. Contains substructs/headings in toml -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct BibiConfig { - pub general: Option<General>, - pub colors: Option<Colors>, + pub general: General, + pub colors: Colors, } /// Substruct [general] in config.toml -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct General { pub bibfiles: Option<Vec<PathBuf>>, pub editor: Option<String>, - pub pdf_opener: Option<String>, - pub url_opener: Option<String>, + pub pdf_opener: String, + pub url_opener: String, } /// Substruct [colors] in config.toml -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct Colors { - pub main_text_color: Option<Color>, - pub highlight_text_color: Option<Color>, - pub entry_color: Option<Color>, - pub keyword_color: Option<Color>, - pub info_color: Option<Color>, - pub confirm_color: Option<Color>, - pub warn_color: Option<Color>, - pub bar_bg_color: Option<Color>, - pub popup_bg_color: Option<Color>, - pub selected_row_bg_color: Option<Color>, + pub main_text_color: Color, + pub highlight_text_color: Color, + pub entry_color: Color, + pub keyword_color: Color, + pub info_color: Color, + pub confirm_color: Color, + pub warn_color: Color, + pub bar_bg_color: Color, + pub popup_bg_color: Color, + pub selected_row_bg_color: Color, } impl Default for BibiConfig { fn default() -> Self { Self { - general: Some(General { + general: General { bibfiles: None, editor: None, - pdf_opener: Some(select_opener()), - url_opener: Some(select_opener()), - }), - colors: Some(Colors { - main_text_color: Some(Color::Indexed(250)), - highlight_text_color: Some(Color::Indexed(254)), - entry_color: Some(Color::Indexed(36)), - keyword_color: Some(Color::Indexed(101)), - info_color: Some(Color::Indexed(99)), - confirm_color: Some(Color::Indexed(47)), - warn_color: Some(Color::Indexed(124)), - bar_bg_color: Some(Color::Indexed(235)), - popup_bg_color: Some(Color::Indexed(234)), - selected_row_bg_color: Some(Color::Indexed(237)), - }), + pdf_opener: select_opener(), + url_opener: select_opener(), + }, + colors: Colors { + main_text_color: Color::Indexed(250), + highlight_text_color: Color::Indexed(254), + entry_color: Color::Indexed(36), + keyword_color: Color::Indexed(101), + info_color: Color::Indexed(99), + confirm_color: Color::Indexed(47), + warn_color: Color::Indexed(124), + bar_bg_color: Color::Indexed(235), + popup_bg_color: Color::Indexed(234), + selected_row_bg_color: Color::Indexed(237), + }, } } } impl BibiConfig { - pub fn parse_config(&mut self, args: &CLIArgs) -> Result<()> { - if args.cfg_path.is_file() { - let cfg_file = Self::parse_cfg_file(args)?; + pub fn parse_config(args: &CLIArgs) -> Result<BibiConfig> { + let cfg_file: BibiConfig = if args.cfg_path.is_file() { + Figment::from(Serialized::defaults(BibiConfig::default())) + .merge(Toml::file(&args.cfg_path)) + .extract()? + } else { + BibiConfig::default() + }; - if let Some(general) = cfg_file.general { - if let Some(bibfiles) = general.bibfiles { - self.general.as_mut().unwrap().bibfiles = Some(bibfiles) - } - if let Some(editor) = general.editor { - self.general.as_mut().unwrap().editor = Some(editor) - } - if let Some(pdf_opener) = general.pdf_opener { - self.general.as_mut().unwrap().pdf_opener = Some(pdf_opener) - } - if let Some(url_opener) = general.url_opener { - self.general.as_mut().unwrap().url_opener = Some(url_opener) - } - } - - if let Some(colors) = cfg_file.colors { - if let Some(main_text_color) = colors.main_text_color { - self.colors.as_mut().unwrap().main_text_color = Some(main_text_color) - } - if let Some(highlight_text_color) = colors.highlight_text_color { - self.colors.as_mut().unwrap().highlight_text_color = Some(highlight_text_color) - } - if let Some(entry_color) = colors.entry_color { - self.colors.as_mut().unwrap().entry_color = Some(entry_color) - } - if let Some(keyword_color) = colors.keyword_color { - self.colors.as_mut().unwrap().keyword_color = Some(keyword_color) - } - if let Some(info_color) = colors.info_color { - self.colors.as_mut().unwrap().info_color = Some(info_color) - } - if let Some(confirm_color) = colors.confirm_color { - self.colors.as_mut().unwrap().confirm_color = Some(confirm_color) - } - if let Some(warn_color) = colors.warn_color { - self.colors.as_mut().unwrap().warn_color = Some(warn_color) - } - if let Some(bar_bg_color) = colors.bar_bg_color { - self.colors.as_mut().unwrap().bar_bg_color = Some(bar_bg_color) - } - if let Some(popup_bg_color) = colors.popup_bg_color { - self.colors.as_mut().unwrap().popup_bg_color = Some(popup_bg_color) - } - if let Some(selected_row_bg_color) = colors.selected_row_bg_color { - self.colors.as_mut().unwrap().selected_row_bg_color = - Some(selected_row_bg_color) - } - } - } - - Ok(()) - } - - fn parse_cfg_file(args: &CLIArgs) -> Result<Self, ConfigError> { - let mut cfg = config::Config::builder(); - cfg = cfg.add_source( - config::File::from(args.cfg_path.clone()) - .format(FileFormat::Toml) - .required(false), - ); - cfg.build()?.try_deserialize() + Ok(cfg_file) } /// Activates the default color scheme for light background terminals pub fn light_colors(&mut self) { - self.colors.as_mut().unwrap().main_text_color = Some(Color::Indexed(235)); - self.colors.as_mut().unwrap().highlight_text_color = Some(Color::Indexed(232)); - self.colors.as_mut().unwrap().entry_color = Some(Color::Indexed(23)); - self.colors.as_mut().unwrap().keyword_color = Some(Color::Indexed(58)); - self.colors.as_mut().unwrap().info_color = Some(Color::Indexed(57)); - self.colors.as_mut().unwrap().bar_bg_color = Some(Color::Indexed(144)); - self.colors.as_mut().unwrap().popup_bg_color = Some(Color::Indexed(187)); - self.colors.as_mut().unwrap().confirm_color = Some(Color::Indexed(22)); - self.colors.as_mut().unwrap().selected_row_bg_color = Some(Color::Indexed(107)); + self.colors.main_text_color = Color::Indexed(235); + self.colors.highlight_text_color = Color::Indexed(232); + self.colors.entry_color = Color::Indexed(23); + self.colors.keyword_color = Color::Indexed(58); + self.colors.info_color = Color::Indexed(57); + self.colors.bar_bg_color = Color::Indexed(144); + self.colors.popup_bg_color = Color::Indexed(187); + self.colors.confirm_color = Color::Indexed(22); + self.colors.selected_row_bg_color = Color::Indexed(107); } } |
