//! AN ACT'S PICKS ON A BUSINESS'S ROW (`shell.opened {step: pick, business://! {server, slug}, table, id, act, answers, count?, query?, ink?}`), the//! customer's side of the pick step (`shell_input::pick`), answered in the//! same shape over this phone's cache of the object instead of a database://! the row's act as `business.pages` offers it to this role, its pick now//! due as fan rows.//!//! - A number: its labels, or a stepper (`shell_input::number_rows`).//! - A record: the rows the object served of the pick's table its `where`//! keeps on this row, each by its title, narrowed by `query` and by the//! `ink` written over them, and Search. No store stands behind them.//! - Any other kind asks nothing a customer can answer: `nothing`.//!//! A row's value is committed through `shell.input {op: commit, writes://! {kind: business_pick, …}}`: the next `step`, or `answers`, every pick's//! answer in order, once it was the last. The core holds no credential, so//! the shell posts those (`business.act`) as it posts an act that asks none.usesuper::shell_input::{filter_on, labelled_rows, number_rows};usecrate::handle::CottonHandle;usecrate::methods::MethodError;usecrate::phone::business;use cotton_core::definitions::{CommandPick, PickKind};use cotton_core::rules::{ink_match, shell_lines};use serde_json::{json, Value};fn bad(why: &str) -> MethodError {MethodError::new("bad_request", why.to_string())}fn text<'a>(request: &'a Value, key: &str) -> Option<&'a str> { request[key].as_str().filter(|value| !value.is_empty())}/// Every row the object served this role, once each.fn served(pages: &Value) -> Vec<&Value> {letmut seen = std::collections::HashSet::new(); pages["tools"].as_array().into_iter().flatten() .flat_map(|tool| tool["views"].as_array().into_iter().flatten()) .flat_map(|view| view["rows"].as_array().into_iter().flatten()) .filter(|row| seen.insert((row["table"].as_str(), row["id"].as_str()))) .collect()}pubfn pick(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {let business = &request["business"];let table = text(request, "table").ok_or_else(|| bad("an act's picks name the card's table"))?;let id = text(request, "id").ok_or_else(|| bad("an act's picks name the card"))?;let act = text(request, "act").ok_or_else(|| bad("the pick step takes the act that asks it"))?;let (object, cache) = business::opened(handle, business)?;let pages = object.pages(&cache, text(request, "timezone"))?;let rows = served(&pages);let held = rows.iter().find(|row| row["table"] == table && row["id"] == id).ok_or_else(|| bad("the object served no such row"))?;let offered = held["acts"].as_array().into_iter().flatten().find(|one| one["id"] == act).ok_or_else(|| bad("this row offers no such act"))?;let picks: Vec<CommandPick> = serde_json::from_value(offered["picks"].clone()).map_err(|why| bad(&format!("{act}'s picks do not read: {why}")))?;let answers = request["answers"].as_array().cloned().unwrap_or_default();let current = picks.get(answers.len()).ok_or_else(|| bad("the act has asked every pick: its last answer is committed"))?;let lines = shell_lines::shipped_rows();let label = current.label.clone().unwrap_or_else(|| offered["label"].as_str().unwrap_or_default().to_string());let index = answers.len();let writes = json!({"kind": "business_pick", "business": business, "table": table, "id": id, "act": act, "answers": answers, "of": picks.len()});letmut step = json!({"kind": "pick", "label": label, "index": index, "writes": writes});let found: Vec<Value> = match current.kind {PickKind::Number => match current.labels.as_ref().filter(|labels| !labels.is_empty()) {Some(labels) => labelled_rows(current, labels, &lines),None => number_rows(current, request["count"].as_f64(), &mut step), },PickKind::Record => {let picked = current.table.as_deref().ok_or_else(|| bad("a record pick names its table"))?; step["table"] = json!(picked);let query = text(request, "query").unwrap_or_default();let ink = text(request, "ink").unwrap_or_default();let lowered = query.to_lowercase();let filter = filter_on(current, Some(id));let kept: Vec<(&str, &str)> = rows.iter() .filter(|row| row["table"] == picked) .filter(|row| filter.iter().all(|(key, wanted)| business::when_matches(row["data"].get(key), wanted))) .filter_map(|row| Some((row["id"].as_str()?, row["title"].as_str()?))) .filter(|(_, title)| lowered.is_empty() || title.to_lowercase().contains(&lowered)) .collect();let stops = ink_match::stop_words();let search = shell_lines::resolve("editing", "command.search", &lines, &[]).unwrap_or_default();letmut said: Vec<Value> = kept.iter() .filter(|(_, title)| ink_match::holds(Some(title), ink, &stops)) .map(|(value, title)| json!({"value": value, "words": title, "does": "answer"})) .collect();if !kept.is_empty() || !query.is_empty() { said.push(json!({"value": "search", "words": search, "does": "search","search": {"editor": "line", "words": query, "title": label, "hint": search, "keeps_words": false, "writes": {"kind": "pick_search"}}})); } said } _ => Vec::new(), };if found.is_empty() && text(request, "ink").is_none() { step["nothing"] = json!(shell_lines::resolve("editing", "command.the_choices_could_not", &lines, &[])); } step["rows"] = Value::Array(found);Ok(json!({"step": step}))}/// One pick answered: the next one due, or every answer once it was the last.pubfn answer(handle: &CottonHandle, writes: &Value, typed: &str, request: &Value) -> Result<Value, MethodError> {letmut answers = writes["answers"].as_array().cloned().unwrap_or_default(); answers.push(json!(typed));if (answers.len() as u64) < writes["of"].as_u64().unwrap_or_default() {return pick(handle, &json!({"business": writes["business"], "table": writes["table"], "id": writes["id"], "act": writes["act"], "answers": answers, "timezone": request["timezone"]})); }Ok(json!({"answers": answers}))}#[cfg(test)]#[path = "shell_input_business_tests.rs"]mod tests;