gunnar_server/
stop_record.rs1pub const RECORD: &str = "/boot/gunnar-last-stop.txt";
11
12pub const SAID: &str = "the previous stop (kept on the ESP):";
14
15pub fn render(power_off_epoch_ms: u128, line: &str) -> String {
17 format!("power_off_epoch_ms={power_off_epoch_ms} {}\n", line.replace('\n', " "))
18}
19
20pub fn power_off_epoch_ms(text: &str) -> Option<u128> {
22 text.split("power_off_epoch_ms=").nth(1)?.split_whitespace().next()?.parse().ok()
23}
24
25pub fn keep(line: &str) -> bool {
27 let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis()).unwrap_or(0);
28 crate::narrate::write_esp_file(std::path::Path::new(RECORD), render(now, line).as_bytes())
29}
30
31pub fn say_previous() {
33 let path = std::path::Path::new(RECORD);
34 if let Ok(text) = std::fs::read_to_string(path) {
35 tracing::info!("{SAID} {}", text.trim());
36 crate::narrate::remove_esp_file(path);
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43
44 #[test]
46 fn a_stop_record_is_one_line_whose_power_off_time_reads_back() {
47 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");
48 assert_eq!(r.matches('\n').count(), 1, "{r}");
49 assert!(r.starts_with("power_off_epoch_ms=1790850910705 front: stopped cleanly"), "{r}");
50 assert_eq!(power_off_epoch_ms(&r), Some(1_790_850_910_705));
51 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");
52 }
53}