//! Gmail's IMAP names for its labels → the Gmail API's label ids, which is //! what an `email` row's `labels` holds and what its unread / starred / //! inbox columns are derived from. //! //! Moved here from `cotton-mac` (25 Nov 2026) so the Mac and the Worker's fetcher (Post CONTRACT row 13) map one //! way: less `merge`, neither can read the labels a row already holds, so IMAP's answer is the row's. /// The labels whose meaning Cotton reads. The API spells them as ids; IMAP /// spells them as `\Inbox`-style system labels and as flags. pub const SYSTEM: &[&str] = &[ "INBOX", "UNREAD", "STARRED", "IMPORTANT", "SENT", "DRAFT", "TRASH", "SPAM", "CHAT", ]; /// `X-GM-LABELS` and `FLAGS` → the API's ids. /// /// System labels map by name; `\Seen` absent is `UNREAD` and `\Flagged` is /// `STARRED` (Gmail keeps the star and the flag as one). A user label keeps /// its name: the API's `Label_` id is not knowable over IMAP. pub fn api_labels(gm_labels: &[String], flags: &[String]) -> Vec { let mut out: Vec = Vec::new(); let mut push = |label: &str| { if !out.iter().any(|have| have == label) { out.push(label.to_string()); } }; for label in gm_labels { match label.to_ascii_lowercase().as_str() { "\\inbox" | "inbox" => push("INBOX"), "\\starred" => push("STARRED"), "\\important" => push("IMPORTANT"), "\\sent" => push("SENT"), "\\draft" => push("DRAFT"), "\\trash" => push("TRASH"), "\\spam" => push("SPAM"), "\\chats" | "\\chat" => push("CHAT"), _ if label.starts_with('\\') => {} _ => push(label), } } let flagged = |name: &str| flags.iter().any(|flag| flag.eq_ignore_ascii_case(name)); if !flagged("\\Seen") { push("UNREAD"); } if flagged("\\Flagged") { push("STARRED"); } if flagged("\\Draft") { push("DRAFT"); } out } #[cfg(test)] mod tests { use super::*; fn v(items: &[&str]) -> Vec { items.iter().map(|s| (*s).to_string()).collect() } #[test] fn gmail_system_labels_and_flags_read_as_the_api_ids() { assert_eq!( api_labels(&v(&["\\Inbox", "\\Important", "Receipts"]), &v(&[])), v(&["INBOX", "IMPORTANT", "Receipts", "UNREAD"]) ); assert_eq!( api_labels(&v(&["\\Starred"]), &v(&["\\Seen", "\\Flagged"])), v(&["STARRED"]) ); } }