//! `move_record` and `move_records` — transfer records between databases. //! //! The handler validates the stable MCP contract. The serving Mac owns the //! paired-file operation, so it receives the request through `MoveRail`. use std::collections::HashSet; use serde_json::{Map, Value}; use crate::mcp::dispatch::{Ctx, HandlerGroup, ToolResult}; use crate::mcp::handlers::common::require_string; use crate::mcp::protocol::ToolFailure; pub const NOT_HERE: &str = "Records are moved by the Mac serving this database."; pub const BATCH_MAX: usize = 1000; /// The requested ids and destination. The server rail captures this call's /// source database when it is initialized. #[derive(Debug, Clone, PartialEq, Eq)] pub struct MoveAsk { pub ids: Vec, pub to: String, } pub fn handlers() -> HandlerGroup { &[("move_record", move_record), ("move_records", move_records)] } pub fn ask_of(name: &str, args: &Map) -> Result { let ids = match name { "move_record" => vec![ require_string(args, "id").ok_or_else(|| ToolFailure::new("id is the record id"))?, ], "move_records" => { let values = args .get("ids") .and_then(Value::as_array) .ok_or_else(|| ToolFailure::new("ids must be an array of record ids"))?; if values.is_empty() { return Err(ToolFailure::new("ids must not be empty")); } if values.len() > BATCH_MAX { return Err(ToolFailure::new(format!( "ids must contain at most {BATCH_MAX} records" ))); } let mut ids = Vec::with_capacity(values.len()); let mut seen = HashSet::with_capacity(values.len()); for value in values { let id = value .as_str() .filter(|id| !id.trim().is_empty()) .ok_or_else(|| ToolFailure::new("ids must be an array of record ids"))? .to_string(); if !seen.insert(id.clone()) { return Err(ToolFailure::new("ids must not contain duplicates")); } ids.push(id); } ids } _ => return Err(ToolFailure::new("unknown move tool")), }; let to = require_string(args, "to") .ok_or_else(|| ToolFailure::new("to is the destination database's id or name"))?; Ok(MoveAsk { ids, to }) } fn asked(ctx: &mut Ctx<'_>, name: &str, args: &Map) -> ToolResult { let ask = ask_of(name, args)?; let rail = ctx .services .record_moves .clone() .ok_or_else(|| ToolFailure::new(NOT_HERE))?; rail(&ask).map_err(ToolFailure::new) } pub fn move_record(ctx: &mut Ctx<'_>, args: &Map) -> ToolResult { asked(ctx, "move_record", args) } pub fn move_records(ctx: &mut Ctx<'_>, args: &Map) -> ToolResult { asked(ctx, "move_records", args) } #[cfg(test)] mod tests { use serde_json::json; use super::*; const ID: &str = "d8c69879-9616-4268-a4b4-24df0b9178be"; const OTHER: &str = "d3770adc-af64-416c-ae3e-25c983d343d3"; /// RULE: a move batch names each record once and is bounded before the serving Mac touches either database, so its all-or-none operation has a finite, unambiguous subject. #[test] fn batches_refuse_empty_duplicate_and_oversized_ids() { for ids in [json!([]), json!([ID, ID]), json!(vec![ID; BATCH_MAX + 1])] { let args = Map::from_iter([ (String::from("ids"), ids), (String::from("to"), json!("Work")), ]); assert!(ask_of("move_records", &args).is_err()); } } #[test] fn stable_single_and_batch_requests_share_one_ask() { let single = Map::from_iter([ (String::from("id"), json!(ID)), (String::from("to"), json!("Work")), ]); assert_eq!( ask_of("move_record", &single).unwrap(), MoveAsk { ids: vec![ID.to_string()], to: "Work".to_string() } ); let batch = Map::from_iter([ (String::from("ids"), json!([ID, OTHER])), (String::from("to"), json!("Work")), ]); assert_eq!(ask_of("move_records", &batch).unwrap().ids, vec![ID, OTHER]); } }