//! A file's format, read off its media type and else its name's ending: a//! transform over two text values (a mime and a file name) that answers a//! typed form, never a word. The word a person reads is the form's line in//! the `documents` set (`format.<key>`), so "Web page" is a row, not a build./// What kind of file a mime (else a name's ending) says it is.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pubenumFileFormat {Pdf,Jpeg,Png,Heic,Image,Film,Audio,WebPage,Markdown,RichText,Word,Spreadsheet,Slides,Text,}implFileFormat {/// The format a mime names, else the one the name's ending names; none/// for a file neither says anything about.pubfn of(mime: Option<&str>, name: Option<&str>) -> Option<Self> {match (Self::by_mime(mime), Self::by_ending(name)) {// The server files Markdown as plain text (it takes no `text/markdown`): the name's ending says it. (Some(Self::Text), Some(Self::Markdown)) => Some(Self::Markdown), (by_mime, by_ending) => by_mime.or(by_ending), } }fn by_mime(mime: Option<&str>) -> Option<Self> {let mime = mime?.trim().to_ascii_lowercase();// A parameter (`; charset=utf-8`) says nothing about the kind.let mime = mime.split(';').next().unwrap_or_default().trim();Some(match mime {"application/pdf" => Self::Pdf,"image/jpeg" => Self::Jpeg,"image/png" => Self::Png,"image/heic" => Self::Heic,"text/html" | "application/xhtml+xml" => Self::WebPage,"text/markdown" | "text/x-markdown" => Self::Markdown,"application/rtf" | "text/rtf" => Self::RichText,"application/msword" | "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => Self::Word,"application/vnd.ms-excel" | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | "text/csv" => {Self::Spreadsheet }"application/vnd.ms-powerpoint" | "application/vnd.openxmlformats-officedocument.presentationml.presentation" => {Self::Slides } mime if mime.starts_with("image/") => Self::Image, mime if mime.starts_with("video/") => Self::Film, mime if mime.starts_with("audio/") => Self::Audio, mime if mime.starts_with("text/") => Self::Text, _ => returnNone, }) }fn by_ending(name: Option<&str>) -> Option<Self> {let (stem, ending) = name?.rsplit_once('.')?;if stem.is_empty() {returnNone; }Some(match ending.to_ascii_lowercase().as_str() {"pdf" => Self::Pdf,"jpg" | "jpeg" => Self::Jpeg,"png" => Self::Png,"heic" => Self::Heic,"html" | "htm" => Self::WebPage,"md" | "markdown" => Self::Markdown,"txt" => Self::Text,"rtf" => Self::RichText,"doc" | "docx" => Self::Word,"xls" | "xlsx" | "csv" => Self::Spreadsheet,"ppt" | "pptx" => Self::Slides, _ => returnNone, }) }/// The line of the `documents` set this format is said by.pubfn line(self) -> &'static str {matchself {Self::Pdf => "format.pdf",Self::Jpeg => "format.jpeg",Self::Png => "format.png",Self::Heic => "format.heic",Self::Image => "format.image",Self::Film => "format.film",Self::Audio => "format.audio",Self::WebPage => "format.web_page",Self::Markdown => "format.markdown",Self::RichText => "format.rich_text",Self::Word => "format.word",Self::Spreadsheet => "format.spreadsheet",Self::Slides => "format.slides",Self::Text => "format.text", } }/// The line for a file no format is known for.pubconst UNKNOWN_LINE: &'static str = "format.file";/// Every format, for a guard that each has its line.pubconst ALL: [Self; 14] = [Self::Pdf,Self::Jpeg,Self::Png,Self::Heic,Self::Image,Self::Film,Self::Audio,Self::WebPage,Self::Markdown,Self::RichText,Self::Word,Self::Spreadsheet,Self::Slides,Self::Text, ];/// How a file of this format opens to be read, never written (Words' attached files, 22 Nov 2026): the one/// answer both phones draw a file by, so a PDF opens in the same reader on each.pubfn opens(self) -> Opens {matchself {Self::WebPage => Opens::Page,Self::Markdown | Self::Text => Opens::Note,Self::Pdf => Opens::Pdf,Self::Jpeg | Self::Png | Self::Heic | Self::Image => Opens::Picture,Self::Film | Self::Audio | Self::RichText | Self::Word | Self::Spreadsheet | Self::Slides => Opens::Elsewhere, } }/// How a file opens from its mime, else its name's ending: elsewhere when neither says.pubfn opens_of(mime: Option<&str>, name: Option<&str>) -> Opens {Self::of(mime, name).map_or(Opens::Elsewhere, Self::opens) }}/// How a file opens, read-only, on the phone.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pubenumOpens {/// A web page: the sealed web view, scripts off, nothing loaded but the file.Page,/// Markdown or plain text: drawn as a note is drawn, read-only.Note,/// A PDF: its pages, drawn on the phone.Pdf,/// A picture: whole, to pinch and zoom.Picture,/// Anything else: handed to another app on the phone (Open with).Elsewhere,}implOpens {/// The word an answer says it by.pubfn id(self) -> &'static str {matchself {Self::Page => "page",Self::Note => "note",Self::Pdf => "pdf",Self::Picture => "picture",Self::Elsewhere => "elsewhere", } }}#[cfg(test)]mod tests {usesuper::*;usecrate::rules::shell_lines;/// GUARD (lane artifactwords, 22 Oct 2026): an HTML document is a web/// page, said by the `documents` set's `format.web_page` line — never/// "Text" because its mime starts with `text/`. Web pages (the Artifacts tool of old) are HTML pages/// and their tiles said "Text"; the word is data so a rewording is a row. #[test]fn an_html_document_is_said_by_the_web_page_line() { assert_eq!(FileFormat::of(Some("text/html"), None), Some(FileFormat::WebPage)); assert_eq!(FileFormat::of(Some("text/html; charset=utf-8"), None), Some(FileFormat::WebPage)); assert_eq!(FileFormat::of(None, Some("tides.html")), Some(FileFormat::WebPage)); assert_eq!(FileFormat::WebPage.line(), "format.web_page");let said = shell_lines::resolve("documents", FileFormat::WebPage.line(), &[], &[]); assert_eq!(said.as_deref(), Some("Web page")); }/// Every format, and the unknown file, has its line in the shipped set:/// a format with no line would draw its path to a person. #[test]fn every_format_has_its_line() {for format inFileFormat::ALL { assert!(shell_lines::resolve("documents", format.line(), &[], &[]).is_some(), "{format:?}"); } assert!(shell_lines::resolve("documents", FileFormat::UNKNOWN_LINE, &[], &[]).is_some()); }/// RULE (Words' attached files, 22 Nov 2026): each kind a note may carry opens in one reader, the same on/// both phones: a web page sealed, Markdown and text as a note, a PDF's pages, a picture whole, and anything/// else in another app. Markdown is its own format, said "Markdown", never "Text". #[test]fn each_kind_of_file_opens_in_its_one_reader() {let opens = |mime: Option<&str>, name: Option<&str>| FileFormat::opens_of(mime, name).id(); assert_eq!(opens(Some("text/html"), Some("page.html")), "page"); assert_eq!(opens(None, Some("Invented page.HTM")), "page"); assert_eq!(opens(Some("text/markdown"), None), "note"); assert_eq!(opens(Some("text/x-markdown; charset=utf-8"), None), "note"); assert_eq!(opens(Some("application/octet-stream"), Some("plan.md")), "note"); assert_eq!(opens(None, Some("plan.markdown")), "note"); assert_eq!(opens(Some("text/plain"), Some("list.txt")), "note"); assert_eq!(opens(Some("application/pdf"), None), "pdf"); assert_eq!(opens(None, Some("Invented statement.pdf")), "pdf"); assert_eq!(opens(Some("image/png"), None), "picture"); assert_eq!(opens(Some("image/webp"), None), "picture"); assert_eq!(opens(None, Some("photo.JPG")), "picture"); assert_eq!(opens(Some("video/mp4"), None), "elsewhere"); assert_eq!(opens(Some("application/vnd.openxmlformats-officedocument.wordprocessingml.document"), None), "elsewhere"); assert_eq!(opens(Some("application/zip"), Some("bundle.zip")), "elsewhere"); assert_eq!(opens(None, None), "elsewhere"); assert_eq!(FileFormat::of(Some("text/markdown"), None), Some(FileFormat::Markdown));// The server files a .md as text/plain: its ending makes it Markdown; plain text stays Text. assert_eq!(FileFormat::of(Some("text/plain"), Some("packing.md")), Some(FileFormat::Markdown)); assert_eq!(FileFormat::of(Some("text/plain"), Some("list.txt")), Some(FileFormat::Text)); assert_eq!(FileFormat::of(Some("application/pdf"), Some("odd.md")), Some(FileFormat::Pdf)); assert_eq!(shell_lines::resolve("documents", FileFormat::Markdown.line(), &[], &[]).as_deref(), Some("Markdown")); } #[test]fn plain_text_stays_text_and_unknown_says_nothing() { assert_eq!(FileFormat::of(Some("text/plain"), None), Some(FileFormat::Text)); assert_eq!(FileFormat::of(Some("application/x-unknown"), Some("blob")), None); assert_eq!(FileFormat::of(Some("application/octet-stream"), Some("a.pdf")), Some(FileFormat::Pdf)); }}