diff options
author | FxQnLr <[email protected]> | 2024-04-11 09:20:04 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2024-04-11 09:20:04 +0200 |
commit | 6b05d1a437a49db98056de7b029923e8aedf1a5a (patch) | |
tree | bc70f14cae1760e91369705273904c0de1bfbf75 /src/storage.rs | |
parent | 907e5cb5bc48899b444f7fedd85af7b5974d9a2e (diff) | |
parent | 2476e182f61d209768635e8eca6e75b4acfbd007 (diff) | |
download | webol-6b05d1a437a49db98056de7b029923e8aedf1a5a.tar webol-6b05d1a437a49db98056de7b029923e8aedf1a5a.tar.gz webol-6b05d1a437a49db98056de7b029923e8aedf1a5a.zip |
Merge pull request #32 from FxQnLr/0.4.0
0.4.0
Diffstat (limited to 'src/storage.rs')
-rw-r--r-- | src/storage.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/storage.rs b/src/storage.rs new file mode 100644 index 0000000..0da245b --- /dev/null +++ b/src/storage.rs | |||
@@ -0,0 +1,70 @@ | |||
1 | use std::{ | ||
2 | fs::{create_dir_all, File}, | ||
3 | io::{Read, Write}, | ||
4 | path::Path, | ||
5 | }; | ||
6 | |||
7 | use ipnetwork::IpNetwork; | ||
8 | use mac_address::MacAddress; | ||
9 | use serde::{Deserialize, Serialize}; | ||
10 | use serde_json::json; | ||
11 | use tracing::{debug, trace, warn}; | ||
12 | use utoipa::ToSchema; | ||
13 | |||
14 | use crate::error::Error; | ||
15 | |||
16 | #[derive(Serialize, Deserialize, Clone, Debug)] | ||
17 | pub struct Device { | ||
18 | pub id: String, | ||
19 | pub mac: MacAddress, | ||
20 | pub broadcast_addr: String, | ||
21 | pub ip: IpNetwork, | ||
22 | pub times: Option<Vec<i64>>, | ||
23 | } | ||
24 | |||
25 | impl Device { | ||
26 | const STORAGE_PATH: &'static str = "devices"; | ||
27 | |||
28 | pub fn setup() -> Result<String, Error> { | ||
29 | trace!("check for storage at {}", Self::STORAGE_PATH); | ||
30 | let sp = Path::new(Self::STORAGE_PATH); | ||
31 | if !sp.exists() { | ||
32 | warn!("device storage path doesn't exist, creating it"); | ||
33 | create_dir_all(Self::STORAGE_PATH)?; | ||
34 | }; | ||
35 | |||
36 | debug!("device storage at '{}'", Self::STORAGE_PATH); | ||
37 | |||
38 | Ok(Self::STORAGE_PATH.to_string()) | ||
39 | } | ||
40 | |||
41 | pub fn read(id: &str) -> Result<Self, Error> { | ||
42 | trace!(?id, "attempt to read file"); | ||
43 | let mut file = File::open(format!("{}/{id}.json", Self::STORAGE_PATH))?; | ||
44 | let mut buf = String::new(); | ||
45 | file.read_to_string(&mut buf)?; | ||
46 | trace!(?id, ?buf, "read successfully from file"); | ||
47 | |||
48 | let dev = serde_json::from_str(&buf)?; | ||
49 | Ok(dev) | ||
50 | } | ||
51 | |||
52 | pub fn write(&self) -> Result<(), Error> { | ||
53 | trace!(?self.id, ?self, "attempt to write to file"); | ||
54 | let mut file = File::create(format!("{}/{}.json", Self::STORAGE_PATH, self.id))?; | ||
55 | file.write_all(json!(self).to_string().as_bytes())?; | ||
56 | trace!(?self.id, "wrote successfully to file"); | ||
57 | |||
58 | Ok(()) | ||
59 | } | ||
60 | } | ||
61 | |||
62 | #[derive(ToSchema)] | ||
63 | #[schema(as = Device)] | ||
64 | pub struct DeviceSchema { | ||
65 | pub id: String, | ||
66 | pub mac: String, | ||
67 | pub broadcast_addr: String, | ||
68 | pub ip: String, | ||
69 | pub times: Option<Vec<i64>>, | ||
70 | } | ||