diff options
author | FxQnLr <[email protected]> | 2024-04-10 20:15:39 +0200 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-04-10 20:15:39 +0200 |
commit | 69d3e0e6564b416637978a69f0a035066aea4759 (patch) | |
tree | 90c5b80a808f9c71cd3440add52b023973ae5b3b /src/storage.rs | |
parent | 07740f3d985b4cc921b69cd45ec9191a2daecd67 (diff) | |
download | webol-69d3e0e6564b416637978a69f0a035066aea4759.tar webol-69d3e0e6564b416637978a69f0a035066aea4759.tar.gz webol-69d3e0e6564b416637978a69f0a035066aea4759.zip |
Closes #30 and #27. At least a little
Diffstat (limited to 'src/storage.rs')
-rw-r--r-- | src/storage.rs | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/src/storage.rs b/src/storage.rs index 6ba5ee1..0da245b 100644 --- a/src/storage.rs +++ b/src/storage.rs | |||
@@ -8,7 +8,7 @@ use ipnetwork::IpNetwork; | |||
8 | use mac_address::MacAddress; | 8 | use mac_address::MacAddress; |
9 | use serde::{Deserialize, Serialize}; | 9 | use serde::{Deserialize, Serialize}; |
10 | use serde_json::json; | 10 | use serde_json::json; |
11 | use tracing::{debug, warn}; | 11 | use tracing::{debug, trace, warn}; |
12 | use utoipa::ToSchema; | 12 | use utoipa::ToSchema; |
13 | 13 | ||
14 | use crate::error::Error; | 14 | use crate::error::Error; |
@@ -26,6 +26,7 @@ impl Device { | |||
26 | const STORAGE_PATH: &'static str = "devices"; | 26 | const STORAGE_PATH: &'static str = "devices"; |
27 | 27 | ||
28 | pub fn setup() -> Result<String, Error> { | 28 | pub fn setup() -> Result<String, Error> { |
29 | trace!("check for storage at {}", Self::STORAGE_PATH); | ||
29 | let sp = Path::new(Self::STORAGE_PATH); | 30 | let sp = Path::new(Self::STORAGE_PATH); |
30 | if !sp.exists() { | 31 | if !sp.exists() { |
31 | warn!("device storage path doesn't exist, creating it"); | 32 | warn!("device storage path doesn't exist, creating it"); |
@@ -38,17 +39,21 @@ impl Device { | |||
38 | } | 39 | } |
39 | 40 | ||
40 | pub fn read(id: &str) -> Result<Self, Error> { | 41 | pub fn read(id: &str) -> Result<Self, Error> { |
42 | trace!(?id, "attempt to read file"); | ||
41 | let mut file = File::open(format!("{}/{id}.json", Self::STORAGE_PATH))?; | 43 | let mut file = File::open(format!("{}/{id}.json", Self::STORAGE_PATH))?; |
42 | let mut buf = String::new(); | 44 | let mut buf = String::new(); |
43 | file.read_to_string(&mut buf)?; | 45 | file.read_to_string(&mut buf)?; |
46 | trace!(?id, ?buf, "read successfully from file"); | ||
44 | 47 | ||
45 | let dev = serde_json::from_str(&buf)?; | 48 | let dev = serde_json::from_str(&buf)?; |
46 | Ok(dev) | 49 | Ok(dev) |
47 | } | 50 | } |
48 | 51 | ||
49 | pub fn write(&self) -> Result<(), Error> { | 52 | pub fn write(&self) -> Result<(), Error> { |
53 | trace!(?self.id, ?self, "attempt to write to file"); | ||
50 | let mut file = File::create(format!("{}/{}.json", Self::STORAGE_PATH, self.id))?; | 54 | let mut file = File::create(format!("{}/{}.json", Self::STORAGE_PATH, self.id))?; |
51 | file.write_all(json!(self).to_string().as_bytes())?; | 55 | file.write_all(json!(self).to_string().as_bytes())?; |
56 | trace!(?self.id, "wrote successfully to file"); | ||
52 | 57 | ||
53 | Ok(()) | 58 | Ok(()) |
54 | } | 59 | } |