Cotton code
63 lines · 3.1 KB · RustRaw
//! `shell.rail`: the date rail down the right edge of a chat in Messages'//! stream (owner, 15 Nov 2026; the rule is `cotton_core::messages::rail`).//!//! Takes `{messages: [{id, day?, label?}], at?}`: the chat's messages as the//! shell holds them, oldest first, each with its day's key and the words//! its day line says (both the core's own, from the record's said day), and//! optionally a place on the rail (0 its top, 1 its foot).//!//! Answers `{stops: [{day, label, at, message}], on?, previous, next,//! width, thumb}`: one stop a day, where it stands and the message the chat//! is turned to; `on`, the stop the thumb is on at `at`; the words of the//! pair at the rail's foot; and the strip's width and the thumb's length in//! points. A touch on the strip is never ink and never the chat's scroll.use crate::methods::MethodError;use cotton_core::messages::rail::{self, RailMessage};use cotton_core::rules::shell_lines::resolve;use serde_json::{json, Value};/// The strip's width and the thumb's length, in points.const WIDTH: f64 = 30.0;const THUMB: f64 = 44.0;fn say(line: &str) -> String {    resolve("chat", &format!("stream.{line}"), &[], &[]).unwrap_or_default()}pub fn call(request: &Value) -> Result<Value, MethodError> {    let messages: Vec<RailMessage> = serde_json::from_value(request["messages"].clone())        .map_err(|_| MethodError::new("bad_request", "shell.rail takes messages: [{id, day?, label?}]"))?;    let stops = rail::stops(&messages);    let mut answer = json!({ "previous": say("previous"), "next": say("next"), "width": WIDTH, "thumb": THUMB });    if let Some(at) = request["at"].as_f64() {        answer["on"] = json!(rail::stop_at(&stops, at));    }    answer["stops"] = json!(stops);    Ok(answer)}#[cfg(test)]mod tests {    use super::*;    /// RULE (owner, 15 Nov 2026): the rail answers a stop a day with the day's own words and its first message, the stop under the thumb, the pair's words and the strip's size.    #[test]    fn the_rail_answers_its_days_and_the_stop_under_the_thumb() {        let request = json!({ "at": 0.6, "messages": [            { "id": "a", "day": "2026-11-01", "label": "Sun 1 Nov" }, { "id": "b", "day": "2026-11-01", "label": "Sun 1 Nov" },            { "id": "c", "day": "2026-11-14", "label": "Yesterday" }, { "id": "d" }, { "id": "e", "day": "2026-11-15", "label": "Today" },        ] });        let answer = call(&request).expect("the rail");        assert_eq!(answer["stops"], json!([            { "day": "2026-11-01", "label": "Sun 1 Nov", "at": 0.0, "message": "a" },            { "day": "2026-11-14", "label": "Yesterday", "at": 0.5, "message": "c" },            { "day": "2026-11-15", "label": "Today", "at": 1.0, "message": "e" },        ]));        assert_eq!(answer["on"], 1);        assert_eq!((answer["previous"].as_str(), answer["next"].as_str()), (Some("Previous chat"), Some("Next chat")));        assert_eq!((answer["width"].as_f64(), answer["thumb"].as_f64()), (Some(30.0), Some(44.0)));        assert!(call(&json!({ "messages": "none" })).is_err());        assert_eq!(call(&json!({ "messages": [] })).unwrap()["stops"], json!([]));    }}