MacroBoard-rs/src/shared/config.rs

35 lines
710 B
Rust
Raw Normal View History

2022-07-15 17:08:22 +00:00
use anyhow::Result;
use serde::{Deserialize, Serialize};
2019-11-22 18:02:27 +00:00
2022-07-15 17:08:22 +00:00
use std::{fs::File, io::Write};
2019-11-22 18:02:27 +00:00
2022-07-15 17:08:22 +00:00
pub const COMMAND_COUNT: usize = 8;
2019-11-22 18:02:27 +00:00
2022-07-15 17:08:22 +00:00
#[derive(Debug, Serialize, Deserialize)]
2019-11-22 18:02:27 +00:00
pub struct Config {
pub commands: [Option<String>; COMMAND_COUNT],
}
impl Config {
2022-07-15 17:08:22 +00:00
pub fn open(path: &str) -> Result<Config> {
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
2019-11-22 18:02:27 +00:00
}
2022-07-15 17:08:22 +00:00
pub fn save(&self, path: &str) -> Result<()> {
let mut file = File::create(path)?;
let s = serde_json::to_string(self)?;
2019-11-22 18:02:27 +00:00
2022-07-15 17:08:22 +00:00
write!(file, "{}", s)?;
2019-11-22 18:02:27 +00:00
2022-07-15 17:08:22 +00:00
Ok(())
2019-11-22 18:02:27 +00:00
}
}
impl Default for Config {
fn default() -> Config {
Config {
commands: Default::default(),
}
}
}