Cotton code
61 lines · 2.0 KB · RustRaw
//! Set the simple meaning fields on one money row.use crate::mcp::dispatch::{Ctx, ToolResult};use crate::mcp::handlers::common::{    clearable_string, fail, optional_string, require_uuid, Clearable,};use serde_json::{Map, Value};fn month(value: &str) -> bool {    value.len() == 7        && value.as_bytes()[4] == b'-'        && value[..4].bytes().all(|b| b.is_ascii_digit())        && matches!(            &value[5..],            "01" | "02" | "03" | "04" | "05" | "06" | "07" | "08" | "09" | "10" | "11" | "12"        )}/// The three focused acts share the same typed row writer.fn set_fields(ctx: &mut Ctx<'_>, args: &Map<String, Value>) -> ToolResult {    let id = require_uuid(args, "id")?;    let mut update = Map::new();    update.insert("id".into(), Value::from(id));    match clearable_string(args, "tag") {        Clearable::Set(v) => {            update.insert("tag".into(), Value::from(v));        }        Clearable::Cleared => {            update.insert("tag".into(), Value::Null);        }        Clearable::Unchanged => {}    }    match clearable_string(args, "personId") {        Clearable::Set(v) => {            update.insert("personId".into(), Value::from(v));        }        Clearable::Cleared => {            update.insert("personId".into(), Value::Null);        }        Clearable::Unchanged => {}    }    if let Some(period) = optional_string(args, "forPeriod") {        if !month(&period) {            return fail("forPeriod must be YYYY-MM");        }        update.insert("forPeriod".into(), Value::from(period));    }    if update.len() == 1 {        return fail("send tag, personId or forPeriod");    }    crate::mcp::handlers::records_2::money::update_transaction(ctx, &update)}pub fn set_tag(ctx: &mut Ctx<'_>, args: &Map<String, Value>) -> ToolResult {    set_fields(ctx, args)}pub fn set_person(ctx: &mut Ctx<'_>, args: &Map<String, Value>) -> ToolResult {    set_fields(ctx, args)}pub fn set_period(ctx: &mut Ctx<'_>, args: &Map<String, Value>) -> ToolResult {    set_fields(ctx, args)}