From 6955b7857cc186b67fbc012a1b37c3fbade78195 Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 26 Nov 2023 23:25:53 -0700 Subject: [PATCH] closerrrrrr to actual code woo --- src/main.rs | 64 +++++++++++++++++++++++++++++------- src/testdata/standalone.txt | 8 ++--- src/testdata/standalone.yaml | 6 ++-- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index db014b3..81c42ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ use serde::{Serialize, Deserialize}; use std::io::{Read, Write}; use std::fs::File; +use std::time::{SystemTime, UNIX_EPOCH, Duration}; +use std::ops::Add; fn main() { println!("Hello, world!"); @@ -8,13 +10,13 @@ fn main() { #[derive(Debug, PartialEq, Serialize, Deserialize)] struct Storage { - adds: Vec, + xs: Vec, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] -struct Add { +#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] +struct X { t: i64, - add: String, + x: String, tag: String, } @@ -48,24 +50,24 @@ fn _load_storage(reader: &mut dyn Read) -> Result { } #[cfg(test)] -mod tests { +mod test_save_load { use super::*; #[test] fn test_save_load_empty() { - let got = _load_storage(&mut "adds: []".as_bytes()).expect("failed to parse 'adds: []' storage"); - assert_eq!(got, Storage{adds: vec![]}); + let got = _load_storage(&mut "xs: []".as_bytes()).expect("failed to parse 'xs: []' storage"); + assert_eq!(got, Storage{xs: vec![]}); let mut w = vec![]; _save_storage(&mut w, got).expect("failed saving storage to writer"); - assert_eq!(String::from_utf8(w).unwrap(), "adds: []\n".to_string()); + assert_eq!(String::from_utf8(w).unwrap(), "xs: []\n".to_string()); } #[test] fn test_testdata_standalone_yaml() { - let want = Storage{adds: vec![ - Add{t: 1, add: "def".to_string(), tag: "abc".to_string()}, - Add{t: 2, add: "ghi".to_string(), tag: "".to_string()}, + let want = Storage{xs: vec![ + X{t: 1, x: "def".to_string(), tag: "abc".to_string()}, + X{t: 2, x: "ghi".to_string(), tag: "".to_string()}, ]}; assert_eq!( load_storage("./src/testdata/standalone.yaml".to_string()).expect("cant load standalone.yaml"), @@ -74,6 +76,44 @@ mod tests { let mut w = vec![]; _save_storage(&mut w, want).expect("failed saving storage to writer"); - assert_eq!(String::from_utf8(w).unwrap(), "adds:\n- t: 1\n add: def\n tag: abc\n- t: 2\n add: 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 { + fn from(&self, t: SystemTime) -> Storage { + let mut result = Storage{xs: vec![]}; + self.xs.iter() + .filter(|x| !x.clone().clone().before(t)) + .for_each(|x| result.xs.push(x.clone())); + result + } +} + +impl X { + fn before(self, t: SystemTime) -> bool { + self.ts() < t + } + + fn ts(self) -> SystemTime { + UNIX_EPOCH.add(Duration::from_secs(self.t.try_into().unwrap())) + } +} + +#[cfg(test)] +mod test_storage { + use super::*; + + #[test] + fn test_storage_from_date() { + let given = Storage{xs: vec![ + X{t: 1, x: "def".to_string(), tag: "abc".to_string()}, + X{t: 2, x: "ghi".to_string(), tag: "".to_string()}, + ]}; + let want = Storage{xs: vec![ + X{t: 2, x: "ghi".to_string(), tag: "".to_string()}, + ]}; + let got = given.from(UNIX_EPOCH.add(Duration::from_secs(1))); + assert_eq!(got, want); } } diff --git a/src/testdata/standalone.txt b/src/testdata/standalone.txt index e420b73..164c4ff 100644 --- a/src/testdata/standalone.txt +++ b/src/testdata/standalone.txt @@ -1,9 +1,9 @@ - t: 1969-12-31 d: - adds: + xs: - d: 1 - add: abc - adds: + x: abc + xs: - def - d: 0 - add: ghi + x: ghi diff --git a/src/testdata/standalone.yaml b/src/testdata/standalone.yaml index 196d27a..c49b303 100644 --- a/src/testdata/standalone.yaml +++ b/src/testdata/standalone.yaml @@ -1,7 +1,7 @@ -adds: +xs: - t: 1 - add: def + x: def tag: abc - t: 2 - add: ghi + x: ghi tag: ""