47 lines
1.3 KiB
Rust
47 lines
1.3 KiB
Rust
use config::{Config, FileFormat};
|
|
use std::fs::File;
|
|
use std::io::Write;
|
|
use log::{error, info};
|
|
|
|
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) {
|
|
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}");
|
|
}
|
|
}
|
|
}
|
|
Err(e) => {
|
|
error!("Could not serialize app state: {e}");
|
|
}
|
|
}
|
|
}
|