Skip to main content

front_matter_value

Function front_matter_value 

Source
pub fn front_matter_value(text: &str, key: &str) -> Option<String>
Expand description

One scalar of a record’s front matter, read the way lib/recover.sh reads it: the first line that begins <key>: , anywhere in the file.

The shell spelling is sed -n 's/^session_id: //p' | head -n 1, which does not stop at the closing ---, and this matches it deliberately. A reader that was stricter than the writer would classify a temp the shell calls a record as foreign and leave it behind for ever.

use majordomus_cli::capability::builtin::recover::front_matter_value;
let rec = "---\nkind: session\nsession_id: s-0001\n---\n\nbody\n";
assert_eq!(front_matter_value(rec, "session_id").as_deref(), Some("s-0001"));
// absent is absent, and an empty value is not a value
assert!(front_matter_value(rec, "task_id").is_none());
assert!(front_matter_value("session_id: \n", "session_id").is_none());
// a key that only appears as a prefix of another is not a match
assert!(front_matter_value("session_id_old: x\n", "session_id").is_none());