43 lines
1.3 KiB
Rust
43 lines
1.3 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use std::{
|
|
fs::File,
|
|
io::{Read, Result, Write},
|
|
};
|
|
use xdg::*;
|
|
|
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
|
pub(crate) struct Config {
|
|
pub(crate) front_url: String,
|
|
pub(crate) weather_url: String,
|
|
}
|
|
|
|
pub(crate) fn load() -> Result<Config> {
|
|
let xdg_dirs =
|
|
BaseDirectories::with_prefix("cabytcini").expect("pu djica lo nu finti lei datnyveiste");
|
|
let config_path = xdg_dirs
|
|
.place_config_file("gaftercu'a.toml")
|
|
.expect("pu djica lo nu le datnyveiste be lo gaftercu'a zvati");
|
|
match File::open(&config_path) {
|
|
Ok(mut fin) => {
|
|
let mut datni = String::new();
|
|
fin.read_to_string(&mut datni)
|
|
.expect("pu djica lo nu tcidu le sfaile");
|
|
let cfg: Config =
|
|
toml::from_str(datni.as_str()).expect("pu djica lo nu jimpe lo sfaile");
|
|
Ok(cfg)
|
|
}
|
|
|
|
Err(why) => {
|
|
log::info!("pu na kargau le sfaile: {:?}", why);
|
|
let mut fout = File::create(config_path).unwrap();
|
|
let cfg = Config {
|
|
front_url: "".to_string(),
|
|
weather_url: "".to_string(),
|
|
};
|
|
|
|
write!(fout, "{}", toml::to_string_pretty(&cfg).unwrap()).unwrap();
|
|
Ok(cfg)
|
|
}
|
|
}
|
|
}
|