//! `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};usecrate::mcp::dispatch::{Ctx, HandlerGroup, ToolResult};usecrate::mcp::handlers::common::require_string;usecrate::mcp::protocol::ToolFailure;pubconst NOT_HERE: &str = "Records are moved by the Mac serving this database.";pubconst 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)]pubstructMoveAsk {pub ids: Vec<String>,pub to: String,}pubfn handlers() -> HandlerGroup { &[("move_record", move_record), ("move_records", move_records)]}pubfn ask_of(name: &str, args: &Map<String, Value>) -> Result<MoveAsk, ToolFailure> {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() {returnErr(ToolFailure::new("ids must not be empty")); }if values.len() > BATCH_MAX {returnErr(ToolFailure::new(format!("ids must contain at most {BATCH_MAX} records" ))); }letmut ids = Vec::with_capacity(values.len());letmut 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()) {returnErr(ToolFailure::new("ids must not contain duplicates")); } ids.push(id); } ids } _ => returnErr(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: &mutCtx<'_>, name: &str, args: &Map<String, Value>) -> 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)}pubfn move_record(ctx: &mutCtx<'_>, args: &Map<String, Value>) -> ToolResult { asked(ctx, "move_record", args)}pubfn move_records(ctx: &mutCtx<'_>, args: &Map<String, Value>) -> ToolResult { asked(ctx, "move_records", args)}#[cfg(test)]mod tests {use serde_json::json;usesuper::*;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]); }}