From 76c89b47fd74e069f9db73503a5131a5a60b8516 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sat, 10 Aug 2024 21:21:34 +0200 Subject: add 'devices' path to request all available devices --- src/storage.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src/storage.rs') diff --git a/src/storage.rs b/src/storage.rs index 90ff1b4..e069875 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,5 +1,6 @@ use std::{ - fs::{create_dir_all, File}, + ffi::OsStr, + fs::{create_dir_all, File, read_dir}, io::{Read, Write}, path::Path, }; @@ -26,14 +27,14 @@ impl Device { const STORAGE_PATH: &'static str = "devices"; pub fn setup() -> Result { - trace!("check for storage at {}", Self::STORAGE_PATH); + trace!("check for storage STORAGE_PATH=\"{}\"", Self::STORAGE_PATH); let sp = Path::new(Self::STORAGE_PATH); if !sp.exists() { warn!("device storage path doesn't exist, creating it"); create_dir_all(Self::STORAGE_PATH)?; }; - debug!("device storage at '{}'", Self::STORAGE_PATH); + debug!("device storage STORAGE_PATH=\"{}\"", Self::STORAGE_PATH); Ok(Self::STORAGE_PATH.to_string()) } @@ -49,6 +50,24 @@ impl Device { Ok(dev) } + pub fn read_all() -> Result, Error> { + trace!("attempt to read all files"); + let st_path = read_dir(Self::STORAGE_PATH)?; + + let mut devices = vec![]; + for file_path in st_path { + let file_path = file_path?; + if file_path.path().extension() != Some(OsStr::new("json")) { continue; }; + let mut file = File::open(file_path.path())?; + let mut buf = String::new(); + file.read_to_string(&mut buf)?; + trace!(?file, ?buf, "read successfully from file"); + devices.push(serde_json::from_str(&buf)?); + } + + Ok(devices) + } + pub fn write(&self) -> Result<(), Error> { trace!(?self.id, ?self, "attempt to write to file"); let mut file = File::create(format!("{}/{}.json", Self::STORAGE_PATH, self.id))?; -- cgit v1.2.3