vcs-controller/src/config.rs

48 lines
1.3 KiB
Rust
Raw Normal View History

2024-03-25 16:28:13 -07:00
use config::{Config, FileFormat};
use std::fs::File;
use std::io::Write;
2024-04-06 10:49:19 -07:00
use log::{error, info};
2024-03-25 16:28:13 -07:00
use crate::ui_code::AppState;
pub fn load_config() -> AppState {
let settings = Config::builder()
.add_source(config::File::new("./settings.toml", FileFormat::Toml))
.build();
if let Ok(val) = settings {
if let Ok(state) = val.try_deserialize() {
state
} else {
AppState::default()
}
} else {
AppState::default()
}
}
pub fn save_config(config: &AppState) {
2024-04-06 10:49:19 -07:00
match toml::to_string(&config) {
Ok(toml_str) => {
match File::create("./settings.toml") {
Ok(mut file) => {
match file.write_all(toml_str.as_bytes()) {
Ok(_) => {
info!("Config file saved succesfully");
}
Err(e) => {
error!("Couldn't write config file contents to open file: {e}");
}
}
}
Err(e) => {
error!("Couldn't open settings file: {e}");
}
2024-03-25 16:28:13 -07:00
}
}
2024-04-06 10:49:19 -07:00
Err(e) => {
error!("Could not serialize app state: {e}");
}
}
2024-03-25 16:28:13 -07:00
}