rename Storage to TSheet

main
Bel LaPointe 2023-11-27 08:41:24 -07:00
parent 0722a6fc3b
commit 3f83364f8f
1 changed files with 27 additions and 27 deletions

View File

@ -9,7 +9,7 @@ fn main() {
} }
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Storage { struct TSheet {
xs: Vec<X>, xs: Vec<X>,
} }
@ -20,32 +20,32 @@ struct X {
tag: String, tag: String,
} }
fn save_storage(path: String, storage: Storage) -> Result<(), String> { fn save(path: String, tsheet: TSheet) -> Result<(), String> {
match File::create(path.clone()) { match File::create(path.clone()) {
Ok(mut writer) => _save_storage(&mut writer, storage), Ok(mut writer) => _save(&mut writer, tsheet),
Err(reason) => Err(format!("failed to open {} to save storage: {}", path, reason)), Err(reason) => Err(format!("failed to open {} to save tsheet: {}", path, reason)),
} }
} }
fn _save_storage(writer: &mut dyn Write, storage: Storage) -> Result<(), String> { fn _save(writer: &mut dyn Write, tsheet: TSheet) -> Result<(), String> {
let mut w = serde_yaml::Serializer::new(writer); let mut w = serde_yaml::Serializer::new(writer);
match storage.serialize(&mut w) { match tsheet.serialize(&mut w) {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(reason) => Err(format!("failed to serialize storage: {}", reason)), Err(reason) => Err(format!("failed to serialize tsheet: {}", reason)),
} }
} }
fn load_storage(path: String) -> Result<Storage, String> { fn load(path: String) -> Result<TSheet, String> {
match File::open(path.clone()) { match File::open(path.clone()) {
Ok(mut reader) => _load_storage(&mut reader), Ok(mut reader) => _load(&mut reader),
Err(reason) => Err(format!("failed to read storage {}: {}", path, reason)), Err(reason) => Err(format!("failed to read tsheet {}: {}", path, reason)),
} }
} }
fn _load_storage(reader: &mut dyn Read) -> Result<Storage, String> { fn _load(reader: &mut dyn Read) -> Result<TSheet, String> {
match serde_yaml::from_reader::<&mut dyn Read, Storage>(reader) { match serde_yaml::from_reader::<&mut dyn Read, TSheet>(reader) {
Ok(storage) => Ok(storage), Ok(tsheet) => Ok(tsheet),
Err(err) => Err(format!("failed to parse storage: {}", err)), Err(err) => Err(format!("failed to parse tsheet: {}", err)),
} }
} }
@ -55,34 +55,34 @@ mod test_save_load {
#[test] #[test]
fn test_save_load_empty() { fn test_save_load_empty() {
let got = _load_storage(&mut "xs: []".as_bytes()).expect("failed to parse 'xs: []' storage"); let got = _load(&mut "xs: []".as_bytes()).expect("failed to parse 'xs: []' tsheet");
assert_eq!(got, Storage{xs: vec![]}); assert_eq!(got, TSheet{xs: vec![]});
let mut w = vec![]; let mut w = vec![];
_save_storage(&mut w, got).expect("failed saving storage to writer"); _save(&mut w, got).expect("failed saving tsheet to writer");
assert_eq!(String::from_utf8(w).unwrap(), "xs: []\n".to_string()); assert_eq!(String::from_utf8(w).unwrap(), "xs: []\n".to_string());
} }
#[test] #[test]
fn test_testdata_standalone_yaml() { fn test_testdata_standalone_yaml() {
let want = Storage{xs: vec![ let want = TSheet{xs: vec![
X{t: 1, x: "def".to_string(), tag: "abc".to_string()}, X{t: 1, x: "def".to_string(), tag: "abc".to_string()},
X{t: 2, x: "ghi".to_string(), tag: "".to_string()}, X{t: 2, x: "ghi".to_string(), tag: "".to_string()},
]}; ]};
assert_eq!( assert_eq!(
load_storage("./src/testdata/standalone.yaml".to_string()).expect("cant load standalone.yaml"), load("./src/testdata/standalone.yaml".to_string()).expect("cant load standalone.yaml"),
want, want,
); );
let mut w = vec![]; let mut w = vec![];
_save_storage(&mut w, want).expect("failed saving storage to writer"); _save(&mut w, want).expect("failed saving tsheet to writer");
assert_eq!(String::from_utf8(w).unwrap(), "xs:\n- t: 1\n x: def\n tag: abc\n- t: 2\n x: ghi\n tag: ''\n".to_string()); assert_eq!(String::from_utf8(w).unwrap(), "xs:\n- t: 1\n x: def\n tag: abc\n- t: 2\n x: ghi\n tag: ''\n".to_string());
} }
} }
impl Storage { impl TSheet {
fn from(&self, t: SystemTime) -> Storage { fn from(&self, t: SystemTime) -> TSheet {
let mut result = Storage{xs: vec![]}; let mut result = TSheet{xs: vec![]};
self.xs.iter() self.xs.iter()
.filter(|x| x.ts() >= t) .filter(|x| x.ts() >= t)
.for_each(|x| result.xs.push(x.clone())); .for_each(|x| result.xs.push(x.clone()));
@ -97,17 +97,17 @@ impl X {
} }
#[cfg(test)] #[cfg(test)]
mod test_storage { mod test_tsheet {
use super::*; use super::*;
#[test] #[test]
fn test_storage_from_date() { fn test_tsheet_from_date() {
let given = Storage{xs: vec![ let given = TSheet{xs: vec![
X{t: 1, x: "def".to_string(), tag: "abc".to_string()}, X{t: 1, x: "def".to_string(), tag: "abc".to_string()},
X{t: 2, x: "ghi".to_string(), tag: "".to_string()}, X{t: 2, x: "ghi".to_string(), tag: "".to_string()},
X{t: 3, x: "jkl".to_string(), tag: "".to_string()}, X{t: 3, x: "jkl".to_string(), tag: "".to_string()},
]}; ]};
let want = Storage{xs: vec![ let want = TSheet{xs: vec![
X{t: 2, x: "ghi".to_string(), tag: "".to_string()}, X{t: 2, x: "ghi".to_string(), tag: "".to_string()},
X{t: 3, x: "jkl".to_string(), tag: "".to_string()}, X{t: 3, x: "jkl".to_string(), tag: "".to_string()},
]}; ]};