use log::{info, trace};
use pigdef::config::HardwareConfig;
use std::fs::File;
use std::io;
use std::io::BufReader;
use std::io::Write;
use std::path::Path;
pub const CONFIG_FILENAME: &str = ".piglet_config.json";
#[allow(dead_code)] pub fn get_config(config_file_path: &Path) -> HardwareConfig {
match load_cfg(config_file_path) {
Ok(config) => {
trace!(
"Config loaded from {}: {config}",
config_file_path.to_string_lossy()
);
config
}
Err(_) => {
info!("No config file could be loaded, using default config");
HardwareConfig::default()
}
}
}
pub fn load_cfg(file_path: &Path) -> io::Result<HardwareConfig> {
let file = File::open(file_path)?;
let reader = BufReader::new(file);
let config = serde_json::from_reader(reader)?;
Ok(config)
}
pub async fn store_config(
hardware_config: &HardwareConfig,
config_file_path: &Path,
) -> io::Result<()> {
let mut file = File::create(config_file_path)?;
let contents = serde_json::to_string(hardware_config)?;
file.write_all(contents.as_bytes())
}