Cotton code
73 lines · 3.1 KB · RustRaw
//! The one stream of chats (Messages on iPhone, 14 Nov 2026): every chat of//! every source in one order, the app's face.//!//! RULE (owner, 14 Nov 2026): Messages opens ON a chat. There is no fan and//! no list of sources: the chats are one stream, unread first, then newest;//! a quiet count says how many are unread and nothing when none; each chat//! carries a mark for where it is from.//!//! Pure: the rows arrive read, and the order a shell stands on arrives as an//! argument. The shell draws what this answers and decides nothing./// One chat as the stream reads it.#[derive(Debug, Clone, PartialEq, Eq)]pub struct StreamChat {    pub id: String,    /// The chat wears an unread mark (`unread::is_thread_unread`).    pub unread: bool,    /// Its last message's moment, an ISO instant; none for a chat with no message.    pub at: Option<String>,    /// Where it is from, as its row says them (`cotton`, `whatsapp`, …), first first.    pub channels: Vec<String>,}/// The stream's order: unread first, then newest. Ties and chats with no/// moment keep the order they were read in, the momentless last.pub fn ordered(mut chats: Vec<StreamChat>) -> Vec<StreamChat> {    // Stable: equal chats stay as read.    chats.sort_by(|a, b| {        b.unread.cmp(&a.unread).then_with(|| match (&a.at, &b.at) {            (Some(x), Some(y)) => y.cmp(x),            (Some(_), None) => std::cmp::Ordering::Less,            (None, Some(_)) => std::cmp::Ordering::Greater,            (None, None) => std::cmp::Ordering::Equal,        })    });    chats}/// The order while a person stands in the stream: a chat read a moment ago/// must not jump away under the hand, and the next swipe must still reach the/// next unread. The chats the shell already holds keep the order it holds/// them in (`standing`); a chat it does not hold (one that just arrived)/// goes in front, in the stream's own order; a chat no longer there leaves.pub fn standing_order(fresh: Vec<StreamChat>, standing: &[String]) -> Vec<StreamChat> {    if standing.is_empty() {        return fresh;    }    let place: std::collections::HashMap<&str, usize> = standing.iter().enumerate().map(|(at, id)| (id.as_str(), at)).collect();    let (mut held, new): (Vec<StreamChat>, Vec<StreamChat>) = fresh.into_iter().partition(|chat| place.contains_key(chat.id.as_str()));    held.sort_by_key(|chat| place[chat.id.as_str()]);    new.into_iter().chain(held).collect()}/// How many chats are unread: what the quiet count says.pub fn unread_count(chats: &[StreamChat]) -> usize {    chats.iter().filter(|chat| chat.unread).count()}/// The source a chat's mark names: its first channel. An archive read from/// two sources wears the first it names; a chat naming none wears no mark.pub fn mark(chat: &StreamChat) -> Option<&str> {    chat.channels.first().map(String::as_str).filter(|channel| !channel.is_empty())}/// The chat the stream opens on: its first, so the newest unread when any/// chat is unread, else the newest.pub fn opens(chats: &[StreamChat]) -> Option<&str> {    chats.first().map(|chat| chat.id.as_str())}#[cfg(test)]#[path = "stream_tests.rs"]mod tests;