From 3566df75e71aa31b4080b1c5d19a4fce25038db8 Mon Sep 17 00:00:00 2001 From: Ryze <50497128+ryze312@users.noreply.github.com> Date: Tue, 28 Nov 2023 22:28:48 +0300 Subject: [PATCH] Config improvements from #8 - Use #[serde(default)] on the whole struct instead of defaults for separate fields - Use unwrap_or_default() instead of matching - Wrap config filename in constant Co-authored-by: Markus Pettersson --- src/config.rs | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/config.rs b/src/config.rs index 35331ba..6dc679c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,30 +6,26 @@ use serde::Deserialize; use crate::error::FF2MpvError; #[derive(Deserialize)] +#[serde(default)] pub struct Config { - #[serde(default = "default_player_command")] pub player_command: String, - - #[serde(default = "default_player_args")] pub player_args: Vec, } impl Default for Config { fn default() -> Self { Self { - player_command: default_player_command(), - player_args: default_player_args(), + player_command: "mpv".to_owned(), + player_args: vec![] } } } impl Config { + const CONFIG_FILENAME: &str = "ff2mpv-rust.json"; + pub fn build() -> Self { - if let Ok(config) = Config::parse_config_file() { - config - } else { - Config::default() - } + Config::parse_config_file().unwrap_or_default() } pub fn parse_config_file() -> Result { @@ -57,7 +53,7 @@ impl Config { path.push("/etc"); } - path.push("ff2mpv-rust.json"); + path.push(Self::CONFIG_FILENAME); path } @@ -67,15 +63,7 @@ impl Config { let appdata = env::var("APPDATA").unwrap(); path.push(appdata); - path.push("ff2mpv-rust.json"); + path.push(Self::CONFIG_FILENAME); path } } - -fn default_player_command() -> String { - "mpv".to_owned() -} - -fn default_player_args() -> Vec { - vec![] -}