+use serde::{Deserialize, Serialize};
+use serde_json::{from_str, to_string};
use std::env;
use std::fs::{create_dir_all, write, File};
use std::io::{Read, Result};
use std::path::PathBuf;
-use serde::{Deserialize, Serialize};
-use serde_json;
const CONFIG_ENV_VARIABLE: &str = "LYRICLI_CONFIG_DIRECTORY";
const CONFIG_DEFAULT_LOCATION: &str = "XDG_CONFIG_HOME";
#[derive(Serialize, Deserialize, Debug)]
pub struct Configuration {
- enabled_sources: Vec<String>
+ enabled_sources: Vec<String>,
}
impl Configuration {
-
- pub fn new() -> Self {
-
+ pub fn new() -> Self {
if let Some(configuration) = Configuration::read() {
return configuration;
}
enabled_sources: vec![
"apple_music".to_string(),
"spotify".to_string(),
- "strawberry".to_string()
- ]
+ "strawberry".to_string(),
+ ],
}
}
let mut config_file = File::open(&config_file_path).ok()?;
let mut config_contents = String::new();
config_file.read_to_string(&mut config_contents).ok()?;
- serde_json::from_str(&config_contents).ok()?
+ from_str(&config_contents).ok()?
}
fn write(configuration: &Configuration) -> Result<()> {
let config_file_path = Configuration::file_path();
- if let Ok(serialized_configuration) = serde_json::to_string(&configuration) {
+ if let Ok(serialized_configuration) = to_string(&configuration) {
write(&config_file_path, serialized_configuration)?;
}
Ok(())
Ok(directory) => PathBuf::from(directory),
Err(_) => match env::var("HOME") {
Ok(directory) => PathBuf::from(directory).join(CONFIG_FALLBACK_LOCATION),
- Err(_) => panic!("Could not find required directory, {} or {} should be set and readable.", CONFIG_ENV_VARIABLE, CONFIG_DEFAULT_LOCATION),
+ Err(_) => panic!(
+ "Could not find required directory, {} or {} should be set and readable.",
+ CONFIG_ENV_VARIABLE, CONFIG_DEFAULT_LOCATION
+ ),
},
},
- }.join(CONFIG_SUBDIRECTORY)
+ }
+ .join(CONFIG_SUBDIRECTORY)
}
}