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 <mpettersson@tutanota.com>
This commit is contained in:
Ryze 2023-11-28 22:28:48 +03:00
parent 392c7937b1
commit 3566df75e7
Signed by: ryze
GPG key ID: 9B296C5CEAEAAAC1

View file

@ -6,30 +6,26 @@ use serde::Deserialize;
use crate::error::FF2MpvError; use crate::error::FF2MpvError;
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(default)]
pub struct Config { pub struct Config {
#[serde(default = "default_player_command")]
pub player_command: String, pub player_command: String,
#[serde(default = "default_player_args")]
pub player_args: Vec<String>, pub player_args: Vec<String>,
} }
impl Default for Config { impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
player_command: default_player_command(), player_command: "mpv".to_owned(),
player_args: default_player_args(), player_args: vec![]
} }
} }
} }
impl Config { impl Config {
const CONFIG_FILENAME: &str = "ff2mpv-rust.json";
pub fn build() -> Self { pub fn build() -> Self {
if let Ok(config) = Config::parse_config_file() { Config::parse_config_file().unwrap_or_default()
config
} else {
Config::default()
}
} }
pub fn parse_config_file() -> Result<Self, FF2MpvError> { pub fn parse_config_file() -> Result<Self, FF2MpvError> {
@ -57,7 +53,7 @@ impl Config {
path.push("/etc"); path.push("/etc");
} }
path.push("ff2mpv-rust.json"); path.push(Self::CONFIG_FILENAME);
path path
} }
@ -67,15 +63,7 @@ impl Config {
let appdata = env::var("APPDATA").unwrap(); let appdata = env::var("APPDATA").unwrap();
path.push(appdata); path.push(appdata);
path.push("ff2mpv-rust.json"); path.push(Self::CONFIG_FILENAME);
path path
} }
} }
fn default_player_command() -> String {
"mpv".to_owned()
}
fn default_player_args() -> Vec<String> {
vec![]
}