vcs-controller/src/config.rs

33 lines
947 B
Rust

use config::{Config, FileFormat};
use err_derive::Error;
use log::{error, info};
use std::fs::File;
use std::io::Write;
use crate::ui::AppState;
pub fn load_config() -> AppState {
let settings = Config::builder()
.add_source(config::File::new("./settings.toml", FileFormat::Toml))
.build();
settings
.and_then(|val| val.try_deserialize())
.unwrap_or_default()
}
#[derive(Error, Debug)]
pub enum SaveConfigError {
#[error(display = "Could not serialize app state: {:?}", _0)]
SerdeError(#[cause] toml::ser::Error),
#[error(display = "Could not write app state to file: {:?}", _0)]
IoError(#[cause] std::io::Error),
}
pub fn save_config(config: &AppState) -> Result<(), SaveConfigError> {
let toml_str = toml::to_string(&config)?;
let mut file = File::create("./settings.toml")?;
file.write_all(toml_str.as_bytes())?;
info!("Config file saved successfully");
Ok(())
}