36 lines
987 B
Rust
36 lines
987 B
Rust
use std::fs::{self, File};
|
|
|
|
use crate::display::theme::Theme;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
|
pub struct Config {
|
|
pub auth: Auth,
|
|
pub theme: Theme,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
|
pub struct Auth {
|
|
hostname: Option<String>,
|
|
token: Option<String>,
|
|
}
|
|
|
|
impl Config {
|
|
pub fn load_or_create() -> Result<Self, anyhow::Error> {
|
|
const CONFIG_FILENAME: &str = "kk.toml";
|
|
let xdg_dirs = xdg::BaseDirectories::with_prefix("kk")?;
|
|
let cfg_filename =
|
|
xdg_dirs.place_config_file(CONFIG_FILENAME)?;
|
|
Ok(match fs::read(&cfg_filename) {
|
|
Ok(f) => toml::from_slice(&f)?,
|
|
Err(_) => {
|
|
// Save default file
|
|
let cfg = Self::default();
|
|
let fmt_cfg = toml::to_string_pretty(&cfg)?;
|
|
fs::write(cfg_filename, fmt_cfg)?;
|
|
cfg
|
|
}
|
|
})
|
|
}
|
|
}
|