vcs-controller/src/config.rs

55 lines
1.6 KiB
Rust
Raw Normal View History

2024-03-25 16:28:13 -07:00
use config::{Config, FileFormat};
2024-05-20 21:20:30 -07:00
use gtk::cairo::IoError;
use snafu::prelude::*;
2024-04-06 11:05:29 -07:00
use log::{error, info};
2024-05-18 13:05:22 -07:00
use serde::{Deserialize, Serialize};
2024-03-25 16:28:13 -07:00
use std::fs::File;
use std::io::Write;
2024-05-18 13:05:22 -07:00
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AppConfig {
pub camera_ip: String,
pub camera_port: u32,
2024-03-25 16:28:13 -07:00
2024-05-18 13:05:22 -07:00
pub tracker_ip: String,
pub tracker_port: u32,
pub tracker_refresh_rate_millis: u32,
}
impl Default for AppConfig {
fn default() -> Self {
AppConfig {
camera_ip: "10.0.0.33".to_string(),
camera_port: 8765,
tracker_ip: "10.0.0.210".to_string(),
tracker_port: 6543,
2024-05-18 13:05:22 -07:00
tracker_refresh_rate_millis: 100,
}
}
}
pub fn load_config() -> AppConfig {
2024-05-20 21:20:30 -07:00
Config::builder()
2024-03-25 16:28:13 -07:00
.add_source(config::File::new("./settings.toml", FileFormat::Toml))
2024-05-20 21:20:30 -07:00
.build()
2024-04-06 20:53:36 -07:00
.and_then(|val| val.try_deserialize())
.unwrap_or_default()
2024-03-25 16:28:13 -07:00
}
2024-05-20 21:20:30 -07:00
#[derive(Debug, Snafu)]
2024-04-06 20:53:36 -07:00
pub enum SaveConfigError {
2024-05-20 21:20:30 -07:00
#[snafu(display("Could not serialize app state: {source}"))]
SerdeError {source: toml::ser::Error },
#[snafu(display("Could not write app state to file: {path}"))]
IoError {source: std::io::Error, path: String },
2024-03-25 16:28:13 -07:00
}
2024-04-06 20:53:36 -07:00
2024-05-18 13:05:22 -07:00
pub fn save_config(config: &AppConfig) -> Result<(), SaveConfigError> {
2024-05-20 21:20:30 -07:00
let toml_str = toml::to_string(&config).context(SerdeSnafu)?;
let mut file = File::create("./settings.toml").context(IoSnafu {path: "./settings.toml" })?;
file.write_all(toml_str.as_bytes()).context(IoSnafu {path: "./settings.toml" })?;
2024-04-06 20:53:36 -07:00
info!("Config file saved successfully");
Ok(())
2024-04-06 21:28:27 -07:00
}