//! `cards.when {table, id, date?, time?, timezone?}`, which writes the card's//! date column keeping the half it was not given; its choices are//! `shell.opened {step: "when" | "time"}`'s//! (`cotton_core::rules::card_when`). A column that holds days (a money//! row's date) is written the bare day and refuses a time.usecrate::methods::MethodError;usecrate::phone::card_actions::read_card;usecrate::shell::client::ClientController;use cotton_core::rules::card_actions::date_column;use cotton_core::rules::card_when;use cotton_core::transforms::moment_split::MomentSplitError;use cotton_core::transforms::zoned_clock;use serde_json::{json, Value};pubfn when(client: &ClientController, request: &Value) -> Result<Value, MethodError> {let card = read_card(client, request, "cards.when")?;let column = date_column(&card.definition).ok_or_else(no_date)?;let zone = zone_of(client, request);let held = card_when::held(card.data.get(&column), &zone).map_err(refused)?;let half = |name: &str| -> Result<Option<Option<&str>>, MethodError> {match request.get(name) {None => Ok(None),Some(Value::Null) => Ok(Some(None)),Some(Value::String(text)) => Ok(Some(Some(text.as_str()))),Some(_) => Err(MethodError::new("bad_request", format!("cards.when takes {name} as text or null"))), } };let (date, time) = (half("date")?, half("time")?);if date.is_none() && time.is_none() {returnErr(MethodError::new("bad_request", "cards.when takes a date, a time or both")); }let value = if card_when::holds_days(&card.definition, &column) { card_when::written_day(held.as_ref(), date, time).map_err(|error| MethodError::new("bad_request", error.to_string()))? } else {let today = zoned_clock::local_day_key(client.clock().now().timestamp_millis(), &zone).map_err(|_| bad_zone())?; card_when::written(held.as_ref(), date, time, &zone, &today).map_err(refused)? } .map_or(Value::Null, Value::from);let change = crate::phone::records::call( client,"records.update", &json!({ "table": card.table, "id": card.id, "patch": { column.clone(): value.clone() } }), )?;Ok(json!({ "column": column, "value": value, "change": change }))}fn zone_of(client: &ClientController, request: &Value) -> String { request["timezone"].as_str().unwrap_or(client.time_zone().unwrap_or("UTC")).to_string()}fn no_date() -> MethodError {MethodError::new("bad_request", "this card has no date a person writes")}/// A day, an hour or a stored moment that does not read, said as the/// core says it.fn refused(error: MomentSplitError) -> MethodError {MethodError::new("bad_request", error.to_string())}fn bad_zone() -> MethodError {MethodError::new("bad_request", "timezone is a named time zone")}#[cfg(test)]#[path = "card_when_tests.rs"]mod tests;