A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
1
use serde::Deserialize;2
use std::fmt::Write;3
use std::fs;4
use std::path::Path;5
use std::path::PathBuf;7
use crate::error::Error;8
use crate::Result;
74
impl Config {75
pub fn acquire() -> Result<Self> {74
let mut config_path = match dirs::config_dir() {75
Some(config_path) => config_path,76
None => return Err(Error::new("unable to determine config path!")),76
let mut config_file = Self::get_global();77
let local_file = Self::get_local();78
if local_file.is_some() {79
config_file = local_file;80
}81
match config_file {82
Some(file) => {83
let raw = fs::read_to_string(&file)?;84
let config: Self = toml::from_str(&raw)?;85
Ok(config)86
}87
None => Err(Error::new("unable to determine config file!")),88
}89
}90
fn get_local() -> Option<PathBuf> {91
let local_file = Path::new("config.toml").to_path_buf();92
if !local_file.exists() {93
return None;94
}95
Some(local_file)96
}97
fn get_global() -> Option<PathBuf> {98
let mut global_file = match dirs::config_dir() {99
Some(file) => file,100
None => return None,101
};78
config_path.push(APP_NAME);79
config_path.push("config.toml");80
let raw = fs::read_to_string(&config_path)?;81
let config: Self = toml::from_str(&raw)?;82
Ok(config)102
global_file.push(APP_NAME);103
global_file.push("config.toml");104
if !global_file.exists() {105
return None;106
}107
Some(global_file)108
}109
}