diff --git a/pttodoest/src/main.rs b/pttodoest/src/main.rs index aac166b..e8fc34b 100755 --- a/pttodoest/src/main.rs +++ b/pttodoest/src/main.rs @@ -9,6 +9,8 @@ fn main() { if !flags.dry_run { for file in files.files.iter() { + file.stage_new_persisted() + .expect("failed to stage new log files"); file.persist_stage() .expect("failed to persist staged changes to log file"); file.stage_persisted().expect("failed to stage log files"); @@ -157,6 +159,25 @@ impl File { Events::new(&self.file) } + pub fn stage_new_persisted(&self) -> Result<(), String> { + let events = self.events()?; + let stage_mod_time = std::fs::metadata(&self.file) + .unwrap() + .modified() + .unwrap() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_secs(); + let new_persisted: Vec = events + .0 + .iter() + .filter(|x| x.ts > stage_mod_time) + .map(|x| x.clone()) + .collect(); + panic!("not impl: apply filtered deltas to stage"); + Ok(()) + } + pub fn stage_persisted(&self) -> Result<(), String> { let stage = self.events()?.snapshot()?; let plaintext = serde_yaml::to_string(&stage).unwrap(); @@ -368,6 +389,48 @@ mod test_file { tests::file_contains(&d, "plain", "- initial\n- 1"); }); } + + #[test] + fn test_stage_new_persisted() { + tests::with_dir(|d| { + tests::write_file(&d, "plain", "- old\n- new"); + tests::write_file( + &d, + ".plain.host", + format!( + r#" + {{"ts":{}, "patch":{{"op":"replace", "path":"/0", "value": "enqueued for persistence"}}}} + "#, + 2147483647, + ).as_str(), + ); + + let f = File::new(&d.path().join("plain").to_str().unwrap().to_string()); + + assert_eq!(1, f.events().unwrap().0.len()); + assert_eq!(2, f.stage().unwrap().len()); + tests::file_contains(&d, "plain", "old"); + tests::file_contains(&d, "plain", "new"); + + f.stage_new_persisted().unwrap(); + tests::file_contains(&d, "plain", "enqueued"); + tests::file_contains(&d, "plain", "new"); + assert_eq!(1, f.events().unwrap().0.len()); + assert_eq!(2, f.stage().unwrap().len()); + + f.persist_stage().unwrap(); + assert_eq!(3, f.events().unwrap().0.len()); + assert_eq!(2, f.stage().unwrap().len()); + tests::file_contains(&d, "plain", "enqueued"); + tests::file_contains(&d, "plain", "new"); + + f.stage_persisted().unwrap(); + assert_eq!(3, f.events().unwrap().0.len()); + assert_eq!(2, f.stage().unwrap().len()); + tests::file_contains(&d, "plain", "enqueued"); + tests::file_contains(&d, "plain", "new"); + }); + } } #[derive(Debug, Clone, Serialize, Deserialize)]