DS made DsFile a trait

This commit is contained in:
Jacob Weaver
2024-12-13 14:32:54 -06:00
parent 808c218646
commit 19a0cc1a8e
7 changed files with 107 additions and 114 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ edition = "2021"
path = "src/ds.rs"
[features]
default = ["std"]
default = []
std = []
[dependencies]
+9 -15
View File
@@ -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<F: DsFile> {
hk: DsHk,
out_data: DsOutData,
file_list: HashMap<TlmSetId, DsFile>,
file_list: HashMap<TlmSetId, F>,
enabled: bool,
}
@@ -33,13 +27,13 @@ pub struct DsFileSettings {
pub enabled: bool,
}
pub struct Ds {
data: DsData,
pub struct Ds<F: DsFile> {
data: DsData<F>,
tlm_sets: HashMap<TlmSetId, DsTlmSet>,
start_enabled: bool,
}
impl Ds {
impl<F: DsFile> Ds<F> {
pub fn new(tlm_sets: HashMap<TlmSetId, DsTlmSet>, start_enabled: bool) -> Self {
Self {
data: Default::default(),
@@ -61,7 +55,7 @@ impl Ds {
}
}
impl App for Ds {
impl<F: DsFile> App for Ds<F> {
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()
};
-72
View File
@@ -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<File>,
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<usize> {
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(());
}
}
}
-25
View File
@@ -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<usize> {
return Ok(0);
}
pub fn flush(&mut self) -> Result<()> {
return Ok(());
}
}
+95
View File
@@ -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<usize>;
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<File>,
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<usize> {
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::*;