entries: add read_by_status, refactor read_approved
This commit is contained in:
parent
ae293c7724
commit
6e6571d5f6
1 changed files with 24 additions and 3 deletions
|
|
@ -85,14 +85,19 @@ pub fn read_entries(dir: &Path) -> Vec<Entry> {
|
||||||
entries
|
entries
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read approved entries only.
|
/// Read entries filtered by status.
|
||||||
pub fn read_approved(dir: &Path) -> Vec<Entry> {
|
pub fn read_by_status(dir: &Path, status: Status) -> Vec<Entry> {
|
||||||
read_entries(dir)
|
read_entries(dir)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|e| e.meta.status == Status::Approved)
|
.filter(|e| e.meta.status == status)
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Read approved entries only.
|
||||||
|
pub fn read_approved(dir: &Path) -> Vec<Entry> {
|
||||||
|
read_by_status(dir, Status::Approved)
|
||||||
|
}
|
||||||
|
|
||||||
/// Find an entry file by short ID prefix and update its status.
|
/// Find an entry file by short ID prefix and update its status.
|
||||||
pub fn set_status(dir: &Path, short_id: &str, status: Status) -> Result<String, String> {
|
pub fn set_status(dir: &Path, short_id: &str, status: Status) -> Result<String, String> {
|
||||||
let read_dir = std::fs::read_dir(dir).map_err(|e| e.to_string())?;
|
let read_dir = std::fs::read_dir(dir).map_err(|e| e.to_string())?;
|
||||||
|
|
@ -253,6 +258,22 @@ Hi!"#;
|
||||||
assert_eq!(entry.meta.voice_note, "");
|
assert_eq!(entry.meta.voice_note, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_read_by_status() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let approved = "+++\nname = \"a\"\ndate = \"2026-04-10\"\nstatus = \"approved\"\n+++\nhi";
|
||||||
|
let pending = "+++\nname = \"b\"\ndate = \"2026-04-10\"\nstatus = \"pending\"\n+++\nhi";
|
||||||
|
let denied = "+++\nname = \"c\"\ndate = \"2026-04-10\"\nstatus = \"denied\"\n+++\nhi";
|
||||||
|
std::fs::write(dir.path().join("1_aaa.txt"), approved).unwrap();
|
||||||
|
std::fs::write(dir.path().join("2_bbb.txt"), pending).unwrap();
|
||||||
|
std::fs::write(dir.path().join("3_ccc.txt"), denied).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(read_by_status(dir.path(), Status::Approved).len(), 1);
|
||||||
|
assert_eq!(read_by_status(dir.path(), Status::Pending).len(), 1);
|
||||||
|
assert_eq!(read_by_status(dir.path(), Status::Denied).len(), 1);
|
||||||
|
assert_eq!(read_by_status(dir.path(), Status::Approved)[0].meta.name, "a");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_roundtrip_with_voice_note() {
|
fn test_roundtrip_with_voice_note() {
|
||||||
let contents = r#"+++
|
let contents = r#"+++
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue