//! The lines the data directory writes before there is anywhere to write them.//!//! Logging writes into the data directory, so it cannot be installed until the//! data directory is known — but the data directory's own story (the one-time//! move, the paths it rewrote, the old cache folder it removed) happens exactly//! there, before it. In a packaged app stdout goes nowhere, so those lines would//! be the only record of a one-time move and would vanish. Buffer them here,//! then replay them the moment there is a reader — in the Swift app that reader//! is the FFI event stream, which flushes this buffer when the first subscriber//! attaches.use std::sync::Mutex;#[derive(Debug, Default)]pubstructEarlyLog { lines: Mutex<Vec<String>>,}implEarlyLog {pubfn new() -> Self {Self::default() }/// Called by `prepare_data_dir` for each line it wants said.pubfn log(&self, line: implInto<String>) {self.lines .lock() .expect("the early log is never held across a panic") .push(line.into()); }/// Replay the buffered lines in order, then empty the buffer.////// The buffer is DRAINED before the first write, not after the last: a/// writer that logs back into it must not be replayed by this same flush,/// and a second flush must write nothing even when the first one wrote a/// hundred lines.pubfn flush_into(&self, write: &dynFn(&str)) {let drained: Vec<String> = {letmut lines = self .lines .lock() .expect("the early log is never held across a panic"); lines.drain(..).collect() };for line in &drained { write(line); } }}#[cfg(test)]mod tests {usesuper::*;use std::cell::RefCell; #[test]fn it_replays_the_buffered_lines_in_the_order_they_were_written() {let early = EarlyLog::new(); early.log("moved the data folder from A to B"); early.log("pointed 3 stored file path(s) at the new folder"); early.log("removed the app's old cache folder A");let written = RefCell::new(Vec::new()); early.flush_into(&|line| written.borrow_mut().push(line.to_string())); assert_eq!( written.into_inner(), vec!["moved the data folder from A to B".to_string(),"pointed 3 stored file path(s) at the new folder".to_string(),"removed the app's old cache folder A".to_string(), ] ); } #[test]fn it_empties_the_buffer_so_a_second_flush_writes_nothing() {let early = EarlyLog::new(); early.log("using the data folder COTTON_MAC_DATA_DIR names — /tmp/x");let first = RefCell::new(Vec::new()); early.flush_into(&|line| first.borrow_mut().push(line.to_string())); assert_eq!(first.into_inner().len(), 1);let second = RefCell::new(Vec::new()); early.flush_into(&|line| second.borrow_mut().push(line.to_string())); assert!(second.into_inner().is_empty()); } #[test]fn it_writes_nothing_when_nothing_was_logged() {let early = EarlyLog::new();let written = RefCell::new(Vec::new()); early.flush_into(&|line| written.borrow_mut().push(line.to_string())); assert!(written.into_inner().is_empty()); } #[test]fn it_does_not_re_write_a_line_logged_again_by_the_writer_itself() {let early = EarlyLog::new(); early.log("one");let written = RefCell::new(Vec::new()); early.flush_into(&|line| { written.borrow_mut().push(line.to_string());// A writer that logs back into the buffer must not be replayed by// this same flush — the buffer was emptied before the first write. early.log(format!("echo of {line}")); }); assert_eq!(written.into_inner(), vec!["one".to_string()]); }}