Cotton code
70 lines · 3.3 KB · RustRaw
//! A LIST'S CARD (owner, 7 Nov 2026: Music's list of songs is a cassette,//! first on its own deck): what the card of one `record_group` holds, as the//! deck answers it (`shell.deck`) for a tool whose lists stand in a face of//! their own (`ToolDefinition::lists`). The face draws from these places//! alone://!//! - `title`: the list's name;//! - `songs`: its members in the list's own order (`group_member.place`),//!   each its `record`, `title`, `subtitle`, `photo`, its length in seconds//!   (`duration`) and the recording it plays (`play`) — or `unavailable`;//! - `count`: how many, said ("5 songs", "1 song");//! - `length`: every member's length added, as a player's clock ("19:18",//!   "3:44:20"), absent while the list is empty;//! - `photo`: the list's own picture (`record_group.picture`), else the//!   first member's that has one (a song's album cover), else none.//!//! The stripes a cassette is dyed with are read off `photo` on the phone,//! as a song's are off its cover (pixels are the phone's).use serde_json::{json, Map, Value};use crate::records::page::clock_said;use crate::rules::shell_lines::{self, LineRow};/// One member of a list as its card reads it.#[derive(Debug, Clone, Default, PartialEq)]pub struct Member {    pub record: String,    pub title: Option<String>,    pub subtitle: Option<String>,    pub photo: Option<String>,    /// The member's length, in seconds.    pub seconds: f64,    /// The recording it plays; none, it cannot be played.    pub play: Option<String>,}/// The places of a list's card (see the module's header), its members in/// the order given.pub fn places(name: &str, picture: Option<&str>, members: &[Member], lines: &[LineRow]) -> Map<String, Value> {    let mut places = Map::new();    places.insert("title".into(), json!(name));    let songs: Vec<Value> = members.iter().map(|member| {        let mut entry = json!({"record": member.record, "duration": member.seconds.max(0.0)});        for (slot, words) in [("title", &member.title), ("subtitle", &member.subtitle), ("photo", &member.photo)] {            if let Some(words) = words.as_deref().map(str::trim).filter(|words| !words.is_empty()) { entry[slot] = json!(words); }        }        match member.play.as_deref().filter(|file| !file.is_empty()) {            Some(file) => entry["play"] = json!(file),            None => entry["unavailable"] = json!(true),        }        entry    }).collect();    places.insert("songs".into(), Value::Array(songs));    let n = members.len().to_string();    let count = if members.len() == 1 { "cassette.song" } else { "cassette.songs" };    if let Some(said) = shell_lines::resolve("cards", count, lines, &[("n", &n)]) { places.insert("count".into(), json!(said)); }    let whole: f64 = members.iter().map(|member| member.seconds.max(0.0)).sum();    if !members.is_empty() {        if let Some(clock) = clock_said(whole) { places.insert("length".into(), json!(clock)); }    }    let own = picture.map(str::trim).filter(|key| !key.is_empty());    let first = members.iter().find_map(|member| member.photo.as_deref().map(str::trim).filter(|key| !key.is_empty()));    if let Some(photo) = own.or(first) { places.insert("photo".into(), json!(photo)); }    places}#[cfg(test)]#[path = "list_card_tests.rs"]mod tests;