//! THE FACE A TABLE WEARS, READ FOR ONE TABLE: the database's face choices //! (`face_choices`, keyed by its `row_key`) as `shell.opened`'s Face step, //! `shell.input`'s commit and a dealt card (`records.get`, `records.list`) //! read them. A deck reads every table's at once (`shell_deck::read_held`). use super::catalogue::FaceChoiceTable; use crate::methods::MethodError; use crate::shell::client::ClientController; use cotton_core::definitions::TableDefinition; use serde_json::{json, Map, Value}; /// The table a database keeps its face choices in, and the choice made for /// one table (none yet: an empty `chosen`). pub(crate) struct Choices { pub table: FaceChoiceTable, pub chosen: Map, } /// Every table definition the database holds. pub(crate) fn tables(client: &ClientController, method: &str) -> Result, MethodError> { let mut tables = Vec::new(); client .read_rows(method, &mut |ctx| { tables = cotton_tools::definitions::table_definitions(ctx).map_err(|failure| failure.to_string())?.definitions; Ok(Value::Null) }) .map_err(|why| MethodError::new("unreadable", why))?; Ok(tables) } /// `table`'s choice; None where the database keeps no face choices. pub(crate) fn of(client: &ClientController, table: &str, method: &str) -> Result, MethodError> { let Some(choices) = FaceChoiceTable::of(&tables(client, method)?) else { return Ok(None) }; let asked = json!({ "table": choices.table, "where": { (choices.row_key.as_str()): table } }); let listed = super::records::call(client, "records.list", &asked)?; let mut chosen = Map::new(); if let Some(face) = listed["records"].as_array().into_iter().flatten().find_map(|row| row["data"]["face"].as_str()) { chosen.insert(table.to_string(), json!(face)); } Ok(Some(Choices { table: choices, chosen })) }