2024-03-25 16:28:13 -07:00
|
|
|
use config::{Config, FileFormat};
|
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_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) {
|
2024-04-06 11:05:29 -07:00
|
|
|
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");
|
2024-04-06 10:49:19 -07:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2024-04-06 11:05:29 -07:00
|
|
|
error!("Couldn't write config file contents to open file: {e}");
|
2024-04-06 10:49:19 -07:00
|
|
|
}
|
2024-04-06 11:05:29 -07:00
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
error!("Couldn't open settings file: {e}");
|
2024-03-25 16:28:13 -07:00
|
|
|
}
|
2024-04-06 11:05:29 -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
|
|
|
}
|