use serde::{Serialize, Deserialize};
use crate::io::{file_to_string, file_exists, write_to_file};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ConfigOptions
{
pub zettelkasten: String,
pub template: String,
db_file: String,
}
impl ::std::default::Default for ConfigOptions
{
fn default() -> ConfigOptions
{
let zettelkasten_path = format!("{}/zettelkasten", env!("HOME"));
ConfigOptions {
zettelkasten: zettelkasten_path,
db_file: String::from("metadata.sql"),
template: String::from(""),
}
}
}
impl ConfigOptions
{
pub fn db_file(&self) -> String
{
format!("{}/{}", &self.zettelkasten, &self.db_file)
}
}
fn expand_path(path: &str) -> String
{
let result = match path.chars().next().unwrap() {
'/' => path.to_string(),
'~' => shellexpand::tilde(path).to_string(),
'$' => path.to_string(),
_ => format!("$HOME/{}", path),
};
shellexpand::env(&result).unwrap().to_string()
}
impl ConfigOptions
{
pub fn load() -> ConfigOptions
{
let xdg_cfg_dir = option_env!("XDG_CONFIG_HOME");
let config_path = if xdg_cfg_dir.is_none() {
format!( "{}/.config/settle/settle.yaml", env!("HOME"))
} else {
format!( "{}/settle/settle.yaml", xdg_cfg_dir.unwrap())
};
if !file_exists(&config_path) {
let data = serde_yaml::to_string(&ConfigOptions::default()).unwrap();
write_to_file(&config_path, &data);
}
let tmp: ConfigOptions = serde_yaml::from_str(&file_to_string(&config_path)).unwrap();
ConfigOptions {
zettelkasten: expand_path(&tmp.zettelkasten),
template: expand_path(&tmp.template),
db_file: tmp.db_file,
}
}
}