rename Storage to TSheet
parent
0722a6fc3b
commit
3f83364f8f
54
src/main.rs
54
src/main.rs
|
|
@ -9,7 +9,7 @@ fn main() {
|
|||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct Storage {
|
||||
struct TSheet {
|
||||
xs: Vec<X>,
|
||||
}
|
||||
|
||||
|
|
@ -20,32 +20,32 @@ struct X {
|
|||
tag: String,
|
||||
}
|
||||
|
||||
fn save_storage(path: String, storage: Storage) -> Result<(), String> {
|
||||
fn save(path: String, tsheet: TSheet) -> Result<(), String> {
|
||||
match File::create(path.clone()) {
|
||||
Ok(mut writer) => _save_storage(&mut writer, storage),
|
||||
Err(reason) => Err(format!("failed to open {} to save storage: {}", path, reason)),
|
||||
Ok(mut writer) => _save(&mut writer, tsheet),
|
||||
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);
|
||||
match storage.serialize(&mut w) {
|
||||
match tsheet.serialize(&mut w) {
|
||||
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()) {
|
||||
Ok(mut reader) => _load_storage(&mut reader),
|
||||
Err(reason) => Err(format!("failed to read storage {}: {}", path, reason)),
|
||||
Ok(mut reader) => _load(&mut reader),
|
||||
Err(reason) => Err(format!("failed to read tsheet {}: {}", path, reason)),
|
||||
}
|
||||
}
|
||||
|
||||
fn _load_storage(reader: &mut dyn Read) -> Result<Storage, String> {
|
||||
match serde_yaml::from_reader::<&mut dyn Read, Storage>(reader) {
|
||||
Ok(storage) => Ok(storage),
|
||||
Err(err) => Err(format!("failed to parse storage: {}", err)),
|
||||
fn _load(reader: &mut dyn Read) -> Result<TSheet, String> {
|
||||
match serde_yaml::from_reader::<&mut dyn Read, TSheet>(reader) {
|
||||
Ok(tsheet) => Ok(tsheet),
|
||||
Err(err) => Err(format!("failed to parse tsheet: {}", err)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -55,34 +55,34 @@ mod test_save_load {
|
|||
|
||||
#[test]
|
||||
fn test_save_load_empty() {
|
||||
let got = _load_storage(&mut "xs: []".as_bytes()).expect("failed to parse 'xs: []' storage");
|
||||
assert_eq!(got, Storage{xs: vec![]});
|
||||
let got = _load(&mut "xs: []".as_bytes()).expect("failed to parse 'xs: []' tsheet");
|
||||
assert_eq!(got, TSheet{xs: 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());
|
||||
}
|
||||
|
||||
#[test]
|
||||
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: 2, x: "ghi".to_string(), tag: "".to_string()},
|
||||
]};
|
||||
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,
|
||||
);
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
impl Storage {
|
||||
fn from(&self, t: SystemTime) -> Storage {
|
||||
let mut result = Storage{xs: vec![]};
|
||||
impl TSheet {
|
||||
fn from(&self, t: SystemTime) -> TSheet {
|
||||
let mut result = TSheet{xs: vec![]};
|
||||
self.xs.iter()
|
||||
.filter(|x| x.ts() >= t)
|
||||
.for_each(|x| result.xs.push(x.clone()));
|
||||
|
|
@ -97,17 +97,17 @@ impl X {
|
|||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_storage {
|
||||
mod test_tsheet {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_storage_from_date() {
|
||||
let given = Storage{xs: vec![
|
||||
fn test_tsheet_from_date() {
|
||||
let given = TSheet{xs: vec![
|
||||
X{t: 1, x: "def".to_string(), tag: "abc".to_string()},
|
||||
X{t: 2, x: "ghi".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: 3, x: "jkl".to_string(), tag: "".to_string()},
|
||||
]};
|
||||
|
|
|
|||
Loading…
Reference in New Issue