closerrrrrr to actual code woo

main
bel 2023-11-26 23:25:53 -07:00
parent 0f6f4edf88
commit 6955b7857c
3 changed files with 59 additions and 19 deletions

View File

@ -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<Add>,
xs: Vec<X>,
}
#[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<Storage, String> {
}
#[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);
}
}

View File

@ -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

View File

@ -1,7 +1,7 @@
adds:
xs:
- t: 1
add: def
x: def
tag: abc
- t: 2
add: ghi
x: ghi
tag: ""