2023-09-19 08:52:12 +00:00
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2023-09-21 05:30:39 +00:00
|
|
|
use serde_json::{from_str, to_string, to_string_pretty};
|
2023-09-19 08:52:12 +00:00
|
|
|
|
|
|
|
#[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)?)?)
|
|
|
|
}
|
|
|
|
|
2023-09-21 05:30:39 +00:00
|
|
|
pub fn to_json(&self) -> Result<String> {
|
|
|
|
Ok(to_string(self)?)
|
|
|
|
}
|
|
|
|
|
2023-09-19 09:18:22 +00:00
|
|
|
#[allow(unused)]
|
2023-09-19 08:52:12 +00:00
|
|
|
pub fn save(&self, file: &str) -> Result<()> {
|
2023-09-19 09:18:22 +00:00
|
|
|
fs::write(file, to_string_pretty(self)?)?;
|
2023-09-19 08:52:12 +00:00
|
|
|
|
|
|
|
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()],
|
|
|
|
};
|
|
|
|
|
2023-09-19 09:18:22 +00:00
|
|
|
devices.save("test_devices.conf")
|
2023-09-19 08:52:12 +00:00
|
|
|
}
|
|
|
|
}
|