From 19a0cc1a8e315ead65977fb14818a75564566b56 Mon Sep 17 00:00:00 2001 From: Jacob Weaver Date: Fri, 13 Dec 2024 14:32:54 -0600 Subject: [PATCH] DS made DsFile a trait --- apps/ds/Cargo.toml | 2 +- apps/ds/src/ds.rs | 24 +++----- apps/ds/src/ds_std.rs | 72 ------------------------ apps/ds/src/ds_stub.rs | 25 --------- apps/ds/src/file.rs | 95 ++++++++++++++++++++++++++++++++ builds/example_build/Cargo.toml | 1 + builds/example_build/src/main.rs | 2 +- 7 files changed, 107 insertions(+), 114 deletions(-) delete mode 100644 apps/ds/src/ds_std.rs delete mode 100644 apps/ds/src/ds_stub.rs create mode 100644 apps/ds/src/file.rs diff --git a/apps/ds/Cargo.toml b/apps/ds/Cargo.toml index e937e1f..e2ccc94 100644 --- a/apps/ds/Cargo.toml +++ b/apps/ds/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" path = "src/ds.rs" [features] -default = ["std"] +default = [] std = [] [dependencies] diff --git a/apps/ds/src/ds.rs b/apps/ds/src/ds.rs index 0037005..a3d23bb 100644 --- a/apps/ds/src/ds.rs +++ b/apps/ds/src/ds.rs @@ -1,15 +1,9 @@ #![no_std] -#[cfg(feature = "std")] -mod ds_std; use bincode::encode_to_vec; -#[cfg(feature = "std")] -pub use ds_std::DsFile; -#[cfg(not(feature = "std"))] -mod ds_stub; -#[cfg(not(feature = "std"))] -pub use ds_stub::DsFile; +mod file; +pub use file::*; extern crate alloc; @@ -20,10 +14,10 @@ use msg::{DsCmd, DsHk, DsOutData, DsTlmSet, Instance, Msg, TlmSetId}; use rfe::*; #[derive(Debug, Default)] -pub struct DsData { +pub struct DsData { hk: DsHk, out_data: DsOutData, - file_list: HashMap, + file_list: HashMap, enabled: bool, } @@ -33,13 +27,13 @@ pub struct DsFileSettings { pub enabled: bool, } -pub struct Ds { - data: DsData, +pub struct Ds { + data: DsData, tlm_sets: HashMap, start_enabled: bool, } -impl Ds { +impl Ds { pub fn new(tlm_sets: HashMap, start_enabled: bool) -> Self { Self { data: Default::default(), @@ -61,7 +55,7 @@ impl Ds { } } -impl App for Ds { +impl App for Ds { fn init(&mut self, rfe: &mut rfe::Rfe) -> Result<()> { self.data = Default::default(); self.data.enabled = self.start_enabled; @@ -152,7 +146,7 @@ impl App for Ds { if let Some(f) = self.data.file_list.get_mut(&tlm_set.id) { f } else { - let f = DsFile::new(tlm_set.path.clone()); + let f = F::new(tlm_set.path.clone()); self.data.file_list.insert(tlm_set.id, f); self.data.file_list.get_mut(&tlm_set.id).unwrap() }; diff --git a/apps/ds/src/ds_std.rs b/apps/ds/src/ds_std.rs deleted file mode 100644 index a6b1069..0000000 --- a/apps/ds/src/ds_std.rs +++ /dev/null @@ -1,72 +0,0 @@ -extern crate std; - -use alloc::{ - format, - string::{String, ToString}, -}; -use chrono::Utc; -use log::*; -use std::{fs::File, io::Write, path::Path}; - -#[derive(Debug)] -pub struct DsFile { - pub dir: String, - pub file: Option, - prefix: String, -} - -impl DsFile { - pub fn new(dir: String) -> Self { - Self { - file: None, - prefix: dir - .clone() - .split("/") - .last() - .unwrap_or("unnamed") - .to_string(), - dir, - } - } - - pub fn close(&mut self) { - self.file = None; - } - - pub fn open(&mut self) { - let time = Self::get_time(&self.prefix); - let file_path = Path::new(&self.dir).join(time); - self.file = match File::create(&file_path) { - Ok(f) => Some(f), - Err(e) => { - error!("failed to create file at {:?} {}", file_path, e); - None - } - }; - } - - fn get_time(prefix: &str) -> String { - let date = Utc::now(); - format!("{}_{}", prefix, date.format("%Y-%m-%d_%H-%M-%S.dat")) - } - - pub fn write(&mut self, buf: &[u8]) -> std::io::Result { - if self.file.is_none() { - self.open(); - } - - if let Some(f) = &mut self.file { - return f.write(buf); - } else { - return Ok(0); - } - } - - pub fn flush(&mut self) -> std::io::Result<()> { - if let Some(f) = &mut self.file { - return f.flush(); - } else { - return Ok(()); - } - } -} diff --git a/apps/ds/src/ds_stub.rs b/apps/ds/src/ds_stub.rs deleted file mode 100644 index ef2a6ec..0000000 --- a/apps/ds/src/ds_stub.rs +++ /dev/null @@ -1,25 +0,0 @@ -use alloc::string::String; -use anyhow::Result; - -#[derive(Debug)] -pub struct DsFile { - pub dir: String, -} - -impl DsFile { - pub fn new(dir: String) -> Self { - Self { dir } - } - - pub fn close(&mut self) {} - - pub fn open(&mut self) {} - - pub fn write(&mut self, _buf: &[u8]) -> Result { - return Ok(0); - } - - pub fn flush(&mut self) -> Result<()> { - return Ok(()); - } -} diff --git a/apps/ds/src/file.rs b/apps/ds/src/file.rs new file mode 100644 index 0000000..32b2f99 --- /dev/null +++ b/apps/ds/src/file.rs @@ -0,0 +1,95 @@ +extern crate alloc; +use alloc::string::String; +use anyhow::Result; + +pub trait DsFile: Default { + fn new(dir: String) -> Self; + fn close(&mut self); + fn open(&mut self); + fn write(&mut self, buf: &[u8]) -> Result; + fn flush(&mut self) -> Result<()>; +} + +#[cfg(feature = "std")] +mod file_std { + extern crate std; + + use alloc::{ + format, + string::{String, ToString}, + }; + use anyhow::Result; + use chrono::Utc; + use log::*; + use std::{fs::File, io::Write, path::Path}; + + use super::DsFile; + + #[derive(Debug, Default)] + pub struct StdDsFile { + pub dir: String, + pub file: Option, + prefix: String, + } + + impl StdDsFile { + fn get_time(prefix: &str) -> String { + let date = Utc::now(); + format!("{}_{}", prefix, date.format("%Y-%m-%d_%H-%M-%S.dat")) + } + } + + impl DsFile for StdDsFile { + fn new(dir: String) -> Self { + Self { + file: None, + prefix: dir + .clone() + .split("/") + .last() + .unwrap_or("unnamed") + .to_string(), + dir, + } + } + + fn close(&mut self) { + self.file = None; + } + + fn open(&mut self) { + let time = Self::get_time(&self.prefix); + let file_path = Path::new(&self.dir).join(time); + self.file = match File::create(&file_path) { + Ok(f) => Some(f), + Err(e) => { + error!("failed to create file at {:?} {}", file_path, e); + None + } + }; + } + + fn write(&mut self, buf: &[u8]) -> Result { + if self.file.is_none() { + self.open(); + } + + if let Some(f) = &mut self.file { + return Ok(f.write(buf)?); + } else { + return Ok(0); + } + } + + fn flush(&mut self) -> Result<()> { + if let Some(f) = &mut self.file { + return Ok(f.flush()?); + } else { + return Ok(()); + } + } + } +} + +#[cfg(feature = "std")] +pub use file_std::*; diff --git a/builds/example_build/Cargo.toml b/builds/example_build/Cargo.toml index ddd9e5a..1056f27 100644 --- a/builds/example_build/Cargo.toml +++ b/builds/example_build/Cargo.toml @@ -9,6 +9,7 @@ rfe.path = "../../rfe" rfe.features = ["std"] example.path = "../../apps/example" ds.path = "../../apps/ds" +ds.features = ["std"] hs.path = "../../apps/hs" hs.features = ["std"] to.path = "../../apps/to" diff --git a/builds/example_build/src/main.rs b/builds/example_build/src/main.rs index a5a02d9..ff50455 100644 --- a/builds/example_build/src/main.rs +++ b/builds/example_build/src/main.rs @@ -95,7 +95,7 @@ fn main() -> Result<()> { ); let mut example = Example::new(); - let mut ds = Ds::new(record, false); + let mut ds = Ds::::new(record, false); // let mut wd = LinuxWatchdog::new().unwrap(); let mut grabber = StdSystemInfoGrabber::new(); let mut hs = Hs::new(