MacroBoard-rs/src/shared/config.rs

30 lines
701 B
Rust

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::{fs::File, io::Write, path::Path};
pub const COMMAND_COUNT: usize = 8;
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Config {
pub commands: [Option<String>; COMMAND_COUNT],
}
impl Config {
pub fn open(path: &str) -> Result<Config> {
if Path::new(path).exists() {
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
} else {
Ok(Config::default())
}
}
pub fn save(&self, path: &str) -> Result<()> {
let mut file = File::create(path)?;
let s = serde_json::to_string(self)?;
write!(file, "{}", s)?;
Ok(())
}
}