HomeServer/src/devices.rs

38 lines
770 B
Rust
Raw Normal View History

2023-09-19 08:52:12 +00:00
use std::fs;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::{from_str, to_string_pretty};
#[derive(Clone, PartialEq, Eq, Deserialize, Serialize, Debug)]
pub struct Devices {
pub plugs: Vec<String>,
}
impl Devices {
pub async fn read(file: &str) -> Result<Self> {
Ok(from_str(&fs::read_to_string(file)?)?)
}
pub fn save(&self, file: &str) -> Result<()> {
fs::write("devices.conf", to_string_pretty(self)?)?;
Ok(())
}
}
#[cfg(test)]
mod test {
use super::Devices;
use anyhow::Result;
#[test]
fn create_conf() -> Result<()> {
let devices = Devices {
plugs: vec!["Dev1".to_string(), "Dev2".to_string()],
};
devices.save("devices.conf")
}
}