Cotton code
59 lines · 2.7 KB · RustRaw
//! The fan tile of the tool owning a table (`cotton_core::definitions:://! fan_tile`), laid on the answers that deal a tier under that tool — a held//! card's acts (`shell.opened`), an empty deck's (`tools.acts`) and Plus's//! doors (`shell.plus`, and the deck's own `plus`) — so the shell draws each//! tile from the answer alone and reads no manifest://!//! - the answer's `tile`: the stock, the type and the role pictures//!   (`FanTile::look`); where the tool names no tile the answer is left//!   as it was, so every other tool's answers stand byte for byte;//! - each act's and each door's `pictogram`: the kit picture the tile names//!   for it by id, by reference (`photo:<sha256>`).use crate::methods::MethodError;use crate::shell::client::ClientController;use cotton_core::definitions::fan_tile::FanTile;use cotton_core::definitions::{TableDefinition, ToolDefinition};use serde_json::{json, Value};/// The tile of the first tool owning `table` that names one.pub fn of_tools(table: &TableDefinition, tools: &[ToolDefinition]) -> Option<FanTile> {    tools.iter().filter(|tool| super::tool_views::owns(&tool.id, table, tools)).find_map(|tool| tool.fan_tile.clone())}/// The tile of the tool owning `table`, read from this database's tools.pub fn of_table(client: &ClientController, table: &TableDefinition, method: &str) -> Result<Option<FanTile>, MethodError> {    let mut tools = Vec::new();    client        .read_rows(method, &mut |ctx| {            tools = cotton_tools::definitions::tool_definitions(ctx).map_err(|failure| failure.to_string())?.definitions;            Ok(Value::Null)        })        .map_err(|why| MethodError::new("unreadable", why))?;    Ok(of_tools(table, &tools))}/// `answer` with the tile laid on it: its `tile`, and each of `items` (the/// entries under `key`, each with an `id`) its pictogram by `named`.fn dress(tile: Option<&FanTile>, answer: &mut Value, key: &str, named: impl Fn(&FanTile, &str) -> Option<String>) {    let Some(tile) = tile else { return };    answer["tile"] = tile.look();    for item in answer[key].as_array_mut().into_iter().flatten() {        if let Some(photo) = item["id"].as_str().and_then(|id| named(tile, id)) {            item["pictogram"] = json!(photo);        }    }}/// A list of acts (`acts` of `shell.opened`, `actions` of `tools.acts`), by their ids.pub fn dress_acts(tile: Option<&FanTile>, answer: &mut Value, key: &str) {    dress(tile, answer, key, |tile, id| tile.act(id).map(str::to_string));}/// What "+" does (`shell.plus`), its doors by their ids.pub fn dress_doors(tile: Option<&FanTile>, plus: &mut Value) {    dress(tile, plus, "doors", |tile, id| tile.door(id).map(str::to_string));}#[cfg(test)]#[path = "fan_tile_tests.rs"]mod tests;