Cotton code
66 lines · 2.6 KB · RustRaw
//! THE RAIL (owner, 15 Nov 2026): a thin strip down the right edge of a//! chat in Messages' stream. Dragging its thumb moves through the chat by//! date, a date's label beside the thumb; the pair that steps to the//! previous and the next chat stands at its foot. It is a strip of its own://! a touch on it is never ink and never the chat's scroll, which stays as//! it is everywhere else on the chat.//!//! The rule is here; a shell draws it. The rail runs from the chat's oldest//! message held (its top) to its newest (its foot). Each day the chat holds//! is a stop: it stands where its first message stands among the messages//! (`at`, 0 at the top to 1 at the foot), it says the day as the chat's own//! day line says it, and it names that first message, which the chat is//! turned to. The thumb at a place on the rail is on the last stop at or//! above it ([`stop_at`]). A message that names no day belongs to the stop//! before it./// One message of the chat, oldest first: its id, its day's key and the/// words the chat's day line says for it.#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize)]pub struct RailMessage {    pub id: String,    #[serde(default)]    pub day: Option<String>,    #[serde(default)]    pub label: Option<String>,}/// A day on the rail.#[derive(Debug, Clone, PartialEq, serde::Serialize)]pub struct Stop {    /// The day's key.    pub day: String,    /// The day as the chat's day line says it.    pub label: String,    /// Where it stands: 0 the rail's top (the oldest held), 1 its foot.    pub at: f64,    /// The day's first message: what the chat is turned to.    pub message: String,}/// The rail's stops for `messages`, oldest first: one a day, in order.pub fn stops(messages: &[RailMessage]) -> Vec<Stop> {    let last = messages.len().saturating_sub(1).max(1) as f64;    let mut stops: Vec<Stop> = Vec::new();    for (place, message) in messages.iter().enumerate() {        let Some(day) = message.day.as_deref().filter(|day| !day.is_empty()) else { continue };        if stops.last().is_some_and(|stop| stop.day == day) {            continue;        }        stops.push(Stop { day: day.to_string(), label: message.label.clone().unwrap_or_default(), at: place as f64 / last, message: message.id.clone() });    }    stops}/// The stop the thumb is on at `at` (0 the top, 1 the foot): the last at or/// above it; above the first, the first. None on a rail with no stop.pub fn stop_at(stops: &[Stop], at: f64) -> Option<usize> {    if stops.is_empty() {        return None;    }    Some(stops.iter().rposition(|stop| stop.at <= at).unwrap_or(0))}#[cfg(test)]#[path = "rail_tests.rs"]mod tests;