summaryrefslogtreecommitdiff
path: root/src/storage.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/storage.rs')
-rw-r--r--src/storage.rs7
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;
8use mac_address::MacAddress; 8use mac_address::MacAddress;
9use serde::{Deserialize, Serialize}; 9use serde::{Deserialize, Serialize};
10use serde_json::json; 10use serde_json::json;
11use tracing::{debug, warn}; 11use tracing::{debug, trace, warn};
12use utoipa::ToSchema; 12use utoipa::ToSchema;
13 13
14use crate::error::Error; 14use 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 }