//! `shell.lines` — the app's own words, one set at a time //! (`cotton_core::rules::shell_lines`). //! //! `{}` answers every shipped set at once — the whole voice in one read, so //! the app keeps no list of set names (a list there is a copy of the kit's //! file list, and the guard `NoTableInGenericSurfacesTests` reads "bin" and //! "backup" in it as table names). `{ set }` answers one set: the kit's file //! with this database's `lines` rows laid over it, so the phone reads it once //! per database and again after a sync. `{ set, line, fills }` answers that //! one line with its `{placeholders}` filled. A device with no file open yet (a refusal, the //! first join) answers the kit's words — they are what a database is written //! with. use serde_json::{json, Value}; use super::MethodError; use crate::handle::CottonHandle; use cotton_core::rules::shell_lines; pub fn lines(handle: &CottonHandle, request: &Value) -> Result { let rows = handle .client() .with_file(|file| Ok(cotton_tools::presentation::line_rows(file.driver, file.workspace_id))) .unwrap_or_default(); let Some(set) = request.get("set").and_then(Value::as_str) else { return Ok(json!({ "sets": every_set(&rows) })); }; let unknown = || MethodError::new("unknown_set", format!("the kit ships no `{set}` line set")); let Some(line) = request.get("line").and_then(Value::as_str) else { let lines = shell_lines::resolved(set, &rows).ok_or_else(unknown)?; return Ok(json!({ "set": set, "lines": lines })); }; let fills: Vec<(String, String)> = request .get("fills") .and_then(Value::as_object) .map(|fills| { fills .iter() .filter_map(|(name, value)| match value { Value::String(text) => Some((name.clone(), text.clone())), Value::Number(number) => Some((name.clone(), number.to_string())), _ => None, }) .collect() }) .unwrap_or_default(); let fills: Vec<(&str, &str)> = fills.iter().map(|(name, value)| (name.as_str(), value.as_str())).collect(); let words = shell_lines::resolve(set, line, &rows, &fills) .ok_or_else(|| MethodError::new("unknown_line", format!("the `{set}` set has no line `{line}`")))?; Ok(json!({ "set": set, "line": line, "words": words })) } /// `count.words` — a count as a person reads it, said by the core and never /// by the app: `{ n, set, line, fills? }` says that line with `{n}` grouped /// and its plural form picked by CLDR; `{ n, units }` says the count in a /// definition's units (`{one, other, zero?}`), the row fields a template names /// left out. Answers `{ words, form }`, `form` the CLDR category of `n`. pub fn count_words(handle: &CottonHandle, request: &Value) -> Result { let n = request .get("n") .and_then(Value::as_i64) .ok_or_else(|| MethodError::new("bad_request", "count.words needs a whole number `n`"))?; let form = shell_lines::plural_form(&n.to_string()); if let Some(units) = request.get("units") { let units: cotton_core::definitions::ColumnUnits = serde_json::from_value(units.clone()) .map_err(|error| MethodError::new("bad_request", format!("count.words units: {error}")))?; let words = cotton_core::records::page::units_count_words(&units, n as f64, &cotton_core::records::page::unit_without_fields) .unwrap_or_default(); return Ok(json!({ "words": words, "form": form })); } let (Some(set), Some(line)) = (request.get("set").and_then(Value::as_str), request.get("line").and_then(Value::as_str)) else { return Err(MethodError::new("bad_request", "count.words needs `units`, or a `set` and a `line`")); }; let rows = handle .client() .with_file(|file| Ok(cotton_tools::presentation::line_rows(file.driver, file.workspace_id))) .unwrap_or_default(); let figure = shell_lines::number(n); let mut fills: Vec<(String, String)> = request .get("fills") .and_then(Value::as_object) .map(|fills| fills.iter().filter_map(|(name, value)| value.as_str().map(|text| (name.clone(), text.to_string()))).collect()) .unwrap_or_default(); fills.push(("n".to_string(), figure)); let fills: Vec<(&str, &str)> = fills.iter().map(|(name, value)| (name.as_str(), value.as_str())).collect(); let words = shell_lines::resolve(set, line, &rows, &fills) .ok_or_else(|| MethodError::new("unknown_line", format!("the `{set}` set has no line `{line}`")))?; Ok(json!({ "words": words, "form": form })) } /// Every shipped set as this database resolves it, by set id — the core's /// `SHIPPED` list is the one list of sets there is. pub fn every_set(rows: &[shell_lines::LineRow]) -> serde_json::Map { shell_lines::SHIPPED .iter() .filter_map(|(set, _)| shell_lines::resolved(set, rows).map(|lines| (set.to_string(), lines))) .collect() } #[cfg(test)] mod tests { use super::*; /// RULE: a count is said by the core in a definition's units — grouped, /// its form picked by CLDR, the zero sentence at nothing, a row field /// left out where no row is read — so the app never words a count. #[test] fn a_count_is_said_in_its_units() { let units = |one: &str, other: &str, zero: Option<&str>| cotton_core::definitions::ColumnUnits { one: one.into(), other: other.into(), zero: zero.map(str::to_string), }; let say = |units: &cotton_core::definitions::ColumnUnits, n: f64| { cotton_core::records::page::units_count_words(units, n, &cotton_core::records::page::unit_without_fields).unwrap_or_default() }; let done = units("done once before", "done {n} times before", Some("")); assert_eq!(say(&done, 0.0), ""); assert_eq!(say(&done, 1.0), "done once before"); assert_eq!(say(&done, 3.0), "done 3 times before"); let words = units("word", "words", None); assert_eq!(say(&words, 1.0), "1 word"); assert_eq!(say(&words, 1240.0), "1,240 words"); let marbles = units("{count_one}", "{count_other}", Some("Sold out")); assert_eq!(say(&marbles, 0.0), "Sold out"); assert_eq!(say(&marbles, 4.0), "4"); } /// RULE: a line a count fills is picked by that count's plural form and /// says the number grouped. #[test] fn a_counted_line_is_picked_and_grouped() { let line = |n: &str| shell_lines::resolve("lists", "whole", &[], &[("n", n)]); assert_eq!(line(&shell_lines::number(2120)).as_deref(), Some("All 2,120")); assert_eq!(shell_lines::plural_form("1"), "one"); } /// RULE: a read naming no set answers every shipped set, so the app holds no list of set names — a list there is a copy of the kit's file list. #[test] fn no_set_named_answers_every_shipped_set() { let sets = every_set(&[]); let shipped: Vec<&str> = shell_lines::SHIPPED.iter().map(|(set, _)| *set).collect(); let mut answered: Vec<&str> = sets.keys().map(String::as_str).collect(); answered.sort(); let mut expected = shipped.clone(); expected.sort(); assert_eq!(answered, expected); for set in shipped { assert!(sets[set].as_object().is_some_and(|lines| !lines.is_empty()), "{set} answers its lines"); } } }