//! A chat as a text file a person reads anywhere: who said what and when, in//! order, and where each picture or file lies beside it in the export//! (owner, 23 Nov 2026; `assets/design/chat/CONTRACT.md` "Export").//!//! Pure: the phone's `chat.export` reads the messages, names the speakers and//! places the files; this writes the text. Each line is//! `2026-11-23 14:05 · Asha: the words`, at the clock in the person's zone; a//! group's event (someone joined) has no speaker. A message's attachment is//! said after its words, as `<attached: files/scan.pdf>` when the file is in//! the export, or as what it was when it is not on this phone.usecrate::cotton_rules::zoned_clock::zoned_parts;/// One message as the export says it.#[derive(Debug, Clone, Default, PartialEq, Eq)]pubstructSaid {pub at: i64,/// The speaker's name ("You" for the person's own); none for a group's event.pub who: Option<String>,pub words: Option<String>,/// Each attachment: in the export at this path, or said in words.pub files: Vec<Attachment>,}#[derive(Debug, Clone, PartialEq, Eq)]pubenumAttachment {/// The file's path inside the export (`files/scan.pdf`).Here(String),/// Not on this phone: what it was, in the reader's words ("a picture").NotHere(String),}/// The words the text is written in, from the chat line set.#[derive(Debug, Clone)]pubstructWords {/// The file's first line, the chat's name filled (`{name}`).pub heading: String,/// The second line, the day it was exported filled (`{day}`).pub exported: String,/// An attachment in the export (`{file}`).pub attached: String,/// An attachment not on this phone (`{what}`).pub not_here: String,}/// The whole file: the heading, when it was exported, a blank line, then each message.pubfn transcript(name: &str, said: &[Said], zone: &str, now: i64, words: &Words) -> String {letmut out = String::new(); out.push_str(&words.heading.replace("{name}", name)); out.push('\n'); out.push_str(&words.exported.replace("{day}", &stamp(now, zone).0)); out.push_str("\n\n");for one in said {let (day, clock) = stamp(one.at, zone);letmut parts: Vec<String> = Vec::new();ifletSome(words) = one.words.as_deref().map(str::trim).filter(|w| !w.is_empty()) { parts.push(words.replace("\r\n", "\n")); }for file in &one.files { parts.push(match file {Attachment::Here(path) => words.attached.replace("{file}", path),Attachment::NotHere(what) => words.not_here.replace("{what}", what), }); }if parts.is_empty() {continue; }let body = parts.join(" ");match &one.who {Some(who) => out.push_str(&format!("{day} {clock} · {who}: {body}\n")),None => out.push_str(&format!("{day} {clock} · {body}\n")), } } out}/// `("2026-11-23", "14:05")` at the clock in `zone`; the instant in UTC where the zone is unknown.fn stamp(ms: i64, zone: &str) -> (String, String) {let parts = zoned_parts(ms, zone).or_else(|_| zoned_parts(ms, "UTC"));match parts {Ok(p) => (format!("{:04}-{:02}-{:02}", p.year, p.month, p.day), format!("{:02}:{:02}", p.hour, p.minute)),Err(_) => (String::new(), String::new()), }}/// A name made safe as one file or folder name: no separator, nothing a/// folder refuses, no leading dot, at most 80 characters; `fallback` when/// nothing is left.pubfn file_name(name: &str, fallback: &str) -> String {let clean: String = name.chars().map(|c| if matches!(c, '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|') || c.is_control() { ' ' } else { c }).collect();let clean = clean.split_whitespace().collect::<Vec<_>>().join(" ");let clean = clean.trim_start_matches('.').trim();if clean.is_empty() { fallback.to_string() } else { clean.chars().take(80).collect::<String>().trim_end().to_string() }}/// `names` made distinct, case aside: a second "Asha" is "Asha 2".pubfn distinct(names: &[String]) -> Vec<String> {letmut seen = std::collections::BTreeSet::new(); names .iter() .map(|base| {letmut name = base.clone();letmut n = 2;while !seen.insert(name.to_lowercase()) { name = match base.rsplit_once('.') {// A file keeps its kind last: "scan 2.pdf".Some((stem, ext)) if !stem.is_empty() && ext.len() <= 8 => format!("{stem} {n}.{ext}"), _ => format!("{base} {n}"), }; n += 1; } name }) .collect()}#[cfg(test)]#[path = "chat_text_tests.rs"]mod tests;