40 lines
1.1 KiB
Rust
40 lines
1.1 KiB
Rust
use anyhow::Result;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::{
|
|
fs::File,
|
|
io::{Read, Write},
|
|
};
|
|
use xdg::*;
|
|
|
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
|
pub struct Config {
|
|
pub(crate) front_url: String,
|
|
pub(crate) weather_url: String,
|
|
}
|
|
|
|
pub 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")?;
|
|
match File::open(&config_path) {
|
|
Ok(mut fin) => {
|
|
let mut datni = String::new();
|
|
fin.read_to_string(&mut datni)?;
|
|
let cfg: Config = toml::from_str(datni.as_str())?;
|
|
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)
|
|
}
|
|
}
|
|
}
|