From e4a060552c859a1c3cf71e97a0dc6071d3c3a2f2 Mon Sep 17 00:00:00 2001 From: lew Date: Fri, 10 Apr 2026 18:08:31 +0100 Subject: [PATCH] entries: add find_entry, refactor set_status to use it --- src/entries.rs | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/entries.rs b/src/entries.rs index d7fc6c5..f6c70cb 100644 --- a/src/entries.rs +++ b/src/entries.rs @@ -98,8 +98,8 @@ pub fn read_approved(dir: &Path) -> Vec { read_by_status(dir, Status::Approved) } -/// Find an entry file by short ID prefix and update its status. -pub fn set_status(dir: &Path, short_id: &str, status: Status) -> Result { +/// Find a single entry by short ID (the UUID portion after the underscore). +pub fn find_entry(dir: &Path, short_id: &str) -> Result { let read_dir = std::fs::read_dir(dir).map_err(|e| e.to_string())?; for item in read_dir { let Ok(item) = item else { continue }; @@ -107,20 +107,22 @@ pub fn set_status(dir: &Path, short_id: &str, status: Status) -> Result Result { + let mut entry = find_entry(dir, short_id)?; + entry.meta.status = status; + let path = dir.join(format!("{}.txt", entry.id)); + std::fs::write(&path, entry.to_file_contents()).map_err(|e| e.to_string())?; + Ok(entry.meta.name.clone()) +} + #[cfg(test)] mod tests { use super::*; @@ -288,4 +290,16 @@ Hello!"#; let reparsed = Entry::parse("test", &serialized).unwrap(); assert_eq!(reparsed.meta.voice_note, "1744300800_abcd1234.webm"); } + + #[test] + fn test_find_entry() { + let dir = tempfile::tempdir().unwrap(); + let contents = "+++\nname = \"alice\"\ndate = \"2026-04-10\"\nstatus = \"pending\"\n+++\nhello"; + std::fs::write(dir.path().join("1744300800_abcd1234.txt"), contents).unwrap(); + + let entry = find_entry(dir.path(), "abcd1234").unwrap(); + assert_eq!(entry.meta.name, "alice"); + + assert!(find_entry(dir.path(), "nonexistent").is_err()); + } }