pub const RECORD: &str = "/boot/gunnar-last-stop.txt";
pub const SAID: &str = "the previous stop (kept on the ESP):";
pub fn render(power_off_epoch_ms: u128, line: &str) -> String {
format!("power_off_epoch_ms={power_off_epoch_ms} {}\n", line.replace('\n', " "))
}
pub fn power_off_epoch_ms(text: &str) -> Option<u128> {
text.split("power_off_epoch_ms=").nth(1)?.split_whitespace().next()?.parse().ok()
}
pub fn keep(line: &str) -> bool {
let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis()).unwrap_or(0);
crate::narrate::write_esp_file(std::path::Path::new(RECORD), render(now, line).as_bytes())
}
pub fn say_previous() {
let path = std::path::Path::new(RECORD);
if let Ok(text) = std::fs::read_to_string(path) {
tracing::info!("{SAID} {}", text.trim());
crate::narrate::remove_esp_file(path);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_stop_record_is_one_line_whose_power_off_time_reads_back() {
let r = render(1_790_850_910_705, "front: stopped cleanly on ACPI power button — power-off: sync(2) done in 3 ms; 2 writer(s) released;\n/data remounted read-only in 41 ms");
assert_eq!(r.matches('\n').count(), 1, "{r}");
assert!(r.starts_with("power_off_epoch_ms=1790850910705 front: stopped cleanly"), "{r}");
assert_eq!(power_off_epoch_ms(&r), Some(1_790_850_910_705));
assert_eq!(power_off_epoch_ms(&format!("INFO {SAID} {r}")), Some(1_790_850_910_705), "read back out of the next boot's line too");
}
}