Clean up config.rs #8

Closed
MarkusPettersson98 wants to merge 1 commit from typesafe-get-config-location into main

View file

@ -1,81 +1,67 @@
use serde::Deserialize;
use std::env;
use std::fs;
use std::path::PathBuf;
ryze312 commented 2023-10-30 22:48:27 +01:00 (Migrated from github.com)
Review

I am not sure if the moving of Deserialize import was intentional.
Use declarations should be ordered as following:

  1. std;
  2. Public crates;
  3. Local crates
I am not sure if the moving of `Deserialize` import was intentional. Use declarations should be ordered as following: 1. `std`; 2. Public crates; 3. Local crates
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<String>,
}
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 {
pub const 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<Self, FF2MpvError> {
let config_path = Config::get_config_location();
if !config_path.exists() {
return Err(FF2MpvError::NoConfig);
}
let config_path = Config::get_config_location()?;
let string = fs::read_to_string(config_path)?;
let config = serde_json::from_str(&string)?;
Ok(config)
}
#[cfg(target_family = "unix")]
fn get_config_location() -> PathBuf {
let mut path = PathBuf::new();
/// Returns a *valid* config path, i.e. it exists and is readable by the
/// current user. If the config path is not valid, an [`FF2MpvError`] is
/// returned.
fn get_config_location() -> Result<PathBuf, FF2MpvError> {
let config_dir = if cfg!(target_family = "unix") {
if let Ok(home) = env::var("XDG_CONFIG_HOME") {
path.push(home);
PathBuf::from(home)
} else if let Ok(home) = env::var("HOME") {
path.push(home);
path.push(".config");
PathBuf::from(home).join(".config")
} else {
path.push("/etc");
PathBuf::from("/etc")
}
} else if cfg!(target_family = "windows") {
env::var("APPDATA")
.map_err(|_| FF2MpvError::NoConfig)?
.into()
} else {
unimplemented!("This platform is not supported")
};
path.push("ff2mpv-rust.json");
path
let path = config_dir.join(Config::FILENAME);
match path.try_exists() {
Ok(true) => Ok(path),
// Broken symbolic link to config file.
Ok(false) => Err(FF2MpvError::NoConfig),
Err(err) => Err(FF2MpvError::IOError(err)),
}
#[cfg(target_family = "windows")]
fn get_config_location() -> PathBuf {
let mut path = PathBuf::new();
let appdata = env::var("APPDATA").unwrap();
path.push(appdata);
path.push("ff2mpv-rust.json");
path
}
}
fn default_player_command() -> String {
"mpv".to_owned()
}
fn default_player_args() -> Vec<String> {
vec![]
}