vcs-controller/src/config.rs

34 lines
947 B
Rust
Raw Normal View History

2024-03-25 16:28:13 -07:00
use config::{Config, FileFormat};
2024-04-06 20:53:36 -07:00
use err_derive::Error;
2024-04-06 11:05:29 -07:00
use log::{error, info};
2024-03-25 16:28:13 -07:00
use std::fs::File;
use std::io::Write;
use crate::ui::AppState;
2024-03-25 16:28:13 -07:00
pub fn load_config() -> AppState {
let settings = Config::builder()
.add_source(config::File::new("./settings.toml", FileFormat::Toml))
.build();
2024-04-06 20:53:36 -07:00
settings
.and_then(|val| val.try_deserialize())
.unwrap_or_default()
2024-03-25 16:28:13 -07:00
}
2024-04-06 20:53:36 -07:00
#[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),
2024-03-25 16:28:13 -07:00
}
2024-04-06 20:53:36 -07:00
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(())
2024-04-06 21:28:27 -07:00
}