From 60ddbc673c42dd9f52739b4b0cd1c535058a51e1 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Tue, 11 Nov 2025 11:41:07 -0700 Subject: [PATCH] wip --- pttodoest/src/main.rs | 55 +++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/pttodoest/src/main.rs b/pttodoest/src/main.rs index 5198289..1c28406 100755 --- a/pttodoest/src/main.rs +++ b/pttodoest/src/main.rs @@ -54,7 +54,7 @@ struct Flags { } impl Flags { - fn new() -> Result { + pub fn new() -> Result { let mut result = Flags::parse(); if result.path.get(..1) == Some("$") { @@ -69,7 +69,7 @@ impl Flags { Ok(result) } - fn files(&self) -> Result { + pub fn files(&self) -> Result { let metadata = match std::fs::metadata(self.path.clone()) { Ok(v) => Ok(v), Err(msg) => Err(format!("failed to load {}: {}", self.path.clone(), msg)), @@ -119,7 +119,7 @@ struct Files { } impl Files { - fn new(files: &Vec) -> Files { + pub fn new(files: &Vec) -> Files { let mut files = files.clone(); files.sort(); Files { @@ -134,15 +134,15 @@ struct File { } impl File { - fn new(file: &String) -> File { + pub fn new(file: &String) -> File { File { file: file.clone() } } - fn events(&self) -> Result { + pub fn events(&self) -> Result { Events::new(&self.file) } - fn stage_persisted(&self) -> Result<(), String> { + pub fn stage_persisted(&self) -> Result<(), String> { let stage = self.events()?.snapshot()?; let plaintext = serde_yaml::to_string(&stage).unwrap(); let mut f = std::fs::File::create(&self.file).expect("failed to open file for writing"); @@ -150,7 +150,7 @@ impl File { Ok(()) } - fn persist_stage(&self) -> Result<(), String> { + pub fn persist_stage(&self) -> Result<(), String> { let persisted = self.events()?.snapshot()?; let snapshot = serde_json::to_string(&persisted).unwrap(); let snapshot = snapshot.as_str(); @@ -228,6 +228,10 @@ mod test_file { #[test] fn test_file() { + let d = tempdir::TempDir::new("").unwrap(); + let mut f = std::fs::File::create(d.path().join("plain")).unwrap(); + writeln!(f, "hello world").unwrap(); + assert!(false, "not impl"); } } @@ -239,14 +243,14 @@ struct Delta { } impl Delta { - fn new(patch: json_patch::PatchOperation, ts: u64) -> Delta { + pub fn new(patch: json_patch::PatchOperation, ts: u64) -> Delta { Delta { patch: patch, ts: ts, } } - fn now(patch: json_patch::PatchOperation) -> Delta { + pub fn now(patch: json_patch::PatchOperation) -> Delta { Self::new( patch, std::time::SystemTime::now() @@ -266,7 +270,7 @@ struct Task(serde_yaml::Value); struct Events(Vec); impl Events { - fn new(file: &String) -> Result { + pub fn new(file: &String) -> Result { let logs = match std::fs::read_dir(Self::dir(&file)) { Ok(files) => Ok(files .filter(|x| x.is_ok()) @@ -284,11 +288,13 @@ impl Events { Ok(f) => { for line in std::io::BufReader::new(f).lines() { let line = line.unwrap(); - let delta = match serde_json::from_str(&line) { - Ok(v) => Ok(v), - Err(msg) => Err(format!("failed to parse line {}: {}", &line, msg)), - }?; - result.push(delta); + if line.len() > 0 { + let delta = match serde_json::from_str(&line) { + Ok(v) => Ok(v), + Err(msg) => Err(format!("failed to parse line {}: {}", &line, msg)), + }?; + result.push(delta); + } } Ok(()) } @@ -345,6 +351,25 @@ mod test_events { #[test] fn test_events() { + let d = tempdir::TempDir::new("").unwrap(); + test_file(&d, "plain", "- persisted\n- stage only"); + test_file( + &d, + ".plain.log", + r#" + {"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}} + "#, + ); + + let events = Events::new(&d.path().join("plain").to_str().unwrap().to_string()).unwrap(); + assert_eq!(1, events.0.len(), "events: {:?}", events); + assert!(false, "not impl"); } } + +fn test_file(d: &tempdir::TempDir, fname: &str, content: &str) { + let mut f = std::fs::File::create(d.path().join("plain")).unwrap(); + writeln!(f, "- persisted\n- stage only").unwrap(); + f.sync_all().unwrap(); +}