//! A tool's fan tile, as data (owner, 6 Nov 2026: every Cinemas fan tile //! carries the app's world, and the look is changed through data //! afterwards). A tool that names one (`ToolDefinition::fan_tile`) says what //! every tile on its tiers is drawn as — its views, its lists, Plus's doors, //! a held card's acts, an empty deck's acts, the choices — and the shell //! only draws it: //! //! - `stock`: a kit picture drawn as the WHOLE face of every tile (Cotton //! Cinemas' black felt letterboard in its brass frame); //! - `type`: how the tile's words are set, one of [`TileType`]; //! - `words`: their ink and their baselines on the stock, where the painter //! set them otherwise than the type's own ([`TileWords`]); //! - `pictures`: the pictograms of the tiles the SHELL makes for this tool, //! by role ([`TILE_ROLES`]); //! - `views`, `acts`, `doors`: the pictogram at the head of each of the //! tool's own tiles, by the view's, the act's or the door's id. An act is //! one of the tool's acts, a command of one of its tables or one of the //! shell's own acts on a held card ([`SHELL_ACTS`]); a door is one of its //! tables' Plus doors. //! //! Every picture is a kit picture, the form a tool's own picture takes //! (`{"photo": "photo:"}`): its file is `pictures/.png` in //! the tool's kit folder and it travels as the tool's picture does — carried //! in the package (`cotton_tool_assets`), served by the object. A kit that //! names a picture it does not carry does not load //! (`package::decode_package`). //! //! The look is the tool's own, one object on its manifest, so a table — read //! by every app and by builds that refuse a key they do not know (a door's) //! — never carries it, and changing the whole look is one edit of one file. //! A tool that names none draws today's tiles, unchanged. use super::{is_valid_id, refusal_words, DefinitionError}; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; use std::collections::BTreeMap; /// A picture a kit carries: `{"photo": "photo:"}`. /// /// These structs name no `deny_unknown_fields`: a READ passes over a key a /// newer build wrote (the `words` map of 8 Nov 2026 would otherwise have made /// every tool carrying it unreadable on the builds before it), and AUTHORING /// is held strict by the package guard ([`super::tolerant`]), which recurses /// into `fan_tile`. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct KitPhoto { pub photo: String, } impl KitPhoto { /// The picture's content hash, where its reference is well formed: the /// SHA-256 of its bytes in lowercase hex. pub fn sha256(&self) -> Option<&str> { self.photo.strip_prefix("photo:").filter(|hash| hash.len() == 64 && hash.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))) } } /// How a tile's words are set: `courier`, the typewriter on cream (every /// tile until 6 Nov 2026, and the default), or `letterboard`, cream upright /// condensed capitals pressed into a felt board (Cotton Cinemas). #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] pub enum TileType { #[default] Courier, Letterboard, } impl TileType { fn is_default(&self) -> bool { *self == TileType::Courier } } /// The tallest stock a tile's words stand on, in its painter's pixels (the /// 474 x 696 blank every painted tile is cut to): a baseline lies inside it. pub const TILE_STOCK_HEIGHT: u32 = 696; /// A tile's words where its painter set them (`layout.json`'s `type`): their /// ink, a `#rrggbb` colour (`type.colour`), the name's first baseline /// (`type.name.baseline`) and the count's under a name of one line and of two /// (`type.count.baselineForOneLine`, `baselineForTwoLines`), in pixels on the /// 696-high stock. Every key is optional: one left out is the type's own — /// the letterboard's cream capitals and Cinemas' baselines, courier's theme /// ink and its label's (Music's pale legend field and Keys' brass tag ink /// their words dark, 8 Nov 2026). #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] pub struct TileWords { #[serde(default, skip_serializing_if = "Option::is_none")] pub ink: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub name_baseline: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub count_baseline_one: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub count_baseline_two: Option, } impl TileWords { /// The ink is `#rrggbb`; each baseline stands on the stock, the name's /// above the count's, a one-line name's count above a two-line name's. fn is_valid(&self) -> bool { let ink = self.ink.as_deref().is_none_or(|ink| ink.len() == 7 && ink.starts_with('#') && ink[1..].bytes().all(|b| b.is_ascii_hexdigit())); let on_stock = [self.name_baseline, self.count_baseline_one, self.count_baseline_two].into_iter().flatten().all(|y| y > 0 && y <= TILE_STOCK_HEIGHT); let below = |upper: Option, lower: Option| upper.zip(lower).is_none_or(|(upper, lower)| upper < lower); ink && on_stock && below(self.name_baseline, self.count_baseline_one) && below(self.name_baseline, self.count_baseline_two) && below(self.count_baseline_one, self.count_baseline_two) } } /// Kit schema: the tiles the shell makes for a tool, which name a pictogram /// by role — a list with no picture and no members' covers of its own /// (`list`), the Group step's "New list" (`new_list`), and More (`more`). pub const TILE_ROLES: &[&str] = &["list", "new_list", "more"]; /// Kit schema: the shell's own acts on a held card or a list's deck, which a /// tool's tile may picture by id beside its own: the choosers /// (`card_actions::opened`: When, Group — Cinemas' Add to list —, Repeat, /// Status, Face, Bin, Send, and Remove everything from this send on a record a /// send brought) and a list's own acts (`tools.acts`: Rename, Set /// picture, Remove picture, Move earlier, Move later, Remove list, and a /// card's moves along the list and off it). pub const SHELL_ACTS: &[&str] = &[ "when", "group", "repeat", "status", "face", "bin", "send", "unsend", "list_set_picture", "list_remove_picture", "list_rename", "list_move_earlier", "list_move_later", "list_remove", "list_member_earlier", "list_member_later", "list_member_remove", ]; /// A tool's fan tile (see the module's header). #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] pub struct FanTile { #[serde(default, skip_serializing_if = "Option::is_none")] pub stock: Option, #[serde(default, rename = "type", skip_serializing_if = "TileType::is_default")] pub kind: TileType, #[serde(default, skip_serializing_if = "Option::is_none")] pub words: Option, #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] pub pictures: BTreeMap, #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] pub views: BTreeMap, #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] pub acts: BTreeMap, #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] pub doors: BTreeMap, } impl FanTile { /// Every kit picture the tile names, once each, in a stable order. pub fn photos(&self) -> Vec<&KitPhoto> { let mut photos: Vec<&KitPhoto> = Vec::new(); let named = self.stock.iter().chain(self.pictures.values()).chain(self.views.values()).chain(self.acts.values()).chain(self.doors.values()); for photo in named { if !photos.iter().any(|held| held.photo == photo.photo) { photos.push(photo); } } photos } /// The tile as the fan's answers carry it to the shell: the stock's /// reference, the type, the role pictures by reference, and the words' /// ink and baselines as the kit gives them, where it gives them (a tile /// naming none answers as it did). pub fn look(&self) -> Value { let pictures: serde_json::Map = self.pictures.iter().map(|(role, photo)| (role.clone(), json!(photo.photo))).collect(); let mut look = json!({"stock": self.stock.as_ref().map(|stock| stock.photo.clone()), "type": self.kind, "pictures": pictures}); if let Some(words) = &self.words { look["words"] = json!(words); } look } /// The pictogram of the view `id`, by reference. pub fn view(&self, id: &str) -> Option<&str> { self.views.get(id).map(|photo| photo.photo.as_str()) } /// The pictogram of the act `id`, by reference. pub fn act(&self, id: &str) -> Option<&str> { self.acts.get(id).map(|photo| photo.photo.as_str()) } /// The pictogram of the door `id`, by reference. pub fn door(&self, id: &str) -> Option<&str> { self.doors.get(id).map(|photo| photo.photo.as_str()) } /// The pictogram of the shell's tile `role` ([`TILE_ROLES`]), by reference. pub fn role(&self, role: &str) -> Option<&str> { self.pictures.get(role).map(|photo| photo.photo.as_str()) } /// The tile on its own: each picture a well-formed kit picture, each role /// one of [`TILE_ROLES`], each view one of `views` (the tool's own), and /// each act and door a well-formed id. Whether an act or a door names /// one the tool's tables offer is told where the tables are in hand /// ([`FanTile::validate_names`]). pub fn validate(&self, views: &[&str]) -> Result<(), DefinitionError> { let bad = |slot: String, why: &str| DefinitionError::BadSlot { slot, why: refusal_words(why) }; if let Some(stock) = &self.stock { if stock.sha256().is_none() { return Err(bad("tool.fan_tile.stock".into(), "a_fan_tile_picture_is_a_kit_photo")); } } if self.words.as_ref().is_some_and(|words| !words.is_valid()) { return Err(bad("tool.fan_tile.words".into(), "a_fan_tile_s_words_are_an_ink_and_baselines_on_its_stock")); } let parts = [("pictures", &self.pictures), ("views", &self.views), ("acts", &self.acts), ("doors", &self.doors)]; for (part, named) in parts { for (tile, photo) in named { let slot = format!("tool.fan_tile.{part}.{tile}"); if photo.sha256().is_none() { return Err(bad(slot, "a_fan_tile_picture_is_a_kit_photo")); } let known = match part { "pictures" => TILE_ROLES.contains(&tile.as_str()), "views" => views.contains(&tile.as_str()), _ => is_valid_id(tile), }; if !known { return Err(bad(slot, if part == "pictures" { "a_fan_tile_role_is_list_new_list_or_more" } else { "a_fan_tile_names_its_tool_s_own_tiles" })); } } } Ok(()) } /// Each act the tile pictures is one of `acts` (the tool's acts, its /// tables' commands) or the shell's ([`SHELL_ACTS`]); each door is one /// of `doors` (its tables' Plus doors). pub fn validate_names(&self, acts: &[&str], doors: &[&str]) -> Result<(), DefinitionError> { let bad = |slot: String| DefinitionError::BadSlot { slot, why: refusal_words("a_fan_tile_names_its_tool_s_own_tiles") }; if let Some(act) = self.acts.keys().find(|act| !acts.contains(&act.as_str()) && !SHELL_ACTS.contains(&act.as_str())) { return Err(bad(format!("tool.fan_tile.acts.{act}"))); } if let Some(door) = self.doors.keys().find(|door| !doors.contains(&door.as_str())) { return Err(bad(format!("tool.fan_tile.doors.{door}"))); } Ok(()) } } #[cfg(test)] #[path = "fan_tile_tests.rs"] mod tests;