Cotton code
66 lines · 3.0 KB · RustRaw
//! What this phone keeps, as its own screen reads and changes it.//!//! Three methods, each the MCP tool of the same rule called through the same//! handler an assistant reaches (`cotton_tools::mcp::handlers::keep_rules`)://!//! | method       | tool              | request                                     |//! |--------------|-------------------|---------------------------------------------|//! | `keep.rules` | `list_keep_rules` | `{device?}`                                 |//! | `keep.set`   | `set_keep_rule`   | `{device?, kind, target, rule}`             |//! | `keep.clear` | `clear_keep_rule` | `{device?, kind, target}`                   |//!//! `device` is THIS device when the request leaves it out — "Keep on this//! phone" on a group's page or in a record's ⋯ names no device. A record's//! `target` may name its table by the definition's id (`note:<id>`); the//! handler takes it to the table its records live in. A read answers the//! tool's own answer; a write answers it too, after the `data` event naming//! `cotton_device_keep` that `ClientController::write` sends, so the device//! row redraws the moment a rule is written.use serde_json::{json, Map, Value};use crate::handle::CottonHandle;use crate::methods::MethodError;use crate::shell::client::ClientController;pub const METHODS: [&str; 3] = ["keep.rules", "keep.set", "keep.clear"];pub fn dispatch(handle: &CottonHandle, method: &str, request: &Value) -> Result<Value, MethodError> {    answer(&handle.client(), method, request)}/// [`dispatch`] over the controller, so a test needs no handle.pub fn answer(client: &ClientController, method: &str, request: &Value) -> Result<Value, MethodError> {    let args = arguments(client, request)?;    match method {        "keep.rules" => client            .read_tool("list_keep_rules", &args)            .map_err(|reason| MethodError::new("unreadable", reason)),        "keep.set" | "keep.clear" => {            let tool = if method == "keep.set" { "set_keep_rule" } else { "clear_keep_rule" };            let answer = client.write(tool, &args)                .map(crate::authoring::own_answer)                .map_err(|reason| MethodError::new("write_refused", reason))?;            Ok(answer)        }        other => Err(MethodError::new("unknown_method", format!("{other} is no keep method"))),    }}/// The request as the tool's arguments, with this device's own key where it/// names none.fn arguments(client: &ClientController, request: &Value) -> Result<Map<String, Value>, MethodError> {    let mut args = crate::phone::read_args(request);    let named = args.get("device").and_then(Value::as_str).is_some_and(|device| !device.trim().is_empty());    if !named {        let key = client.self_device_id().ok_or_else(|| {            MethodError::new("device_key_unreadable", crate::phone::owner::NO_DEVICE_IDENTITY)        })?;        args.insert("device".to_string(), json!(key));    }    Ok(args)}#[cfg(test)]#[path = "keep_rules_tests.rs"]mod tests;