use crate::tool_handler::{ToolError, ToolHandler};
#[derive(Default)]
pub struct ToolRegistry {
handlers: Vec<Box<dyn ToolHandler>>,
}
impl std::fmt::Debug for ToolRegistry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let names: Vec<&str> = self.handlers.iter().map(|h| h.name()).collect();
f.debug_struct("ToolRegistry")
.field("tools", &names)
.finish()
}
}
impl ToolRegistry {
pub fn new() -> Self {
Self::default()
}
pub fn register(&mut self, handler: Box<dyn ToolHandler>) {
assert!(
!handler.gate_set().is_empty(),
"tool '{}' declares no gates — gate wiring validation failed (DA-3)",
handler.name()
);
tracing::debug!(tool = handler.name(), "registered tool handler");
self.handlers.push(handler);
}
pub fn dispatch(
&self,
method: &str,
params: serde_json::Value,
) -> Option<Result<serde_json::Value, ToolError>> {
self.handlers
.iter()
.find(|h| h.name() == method)
.map(|h| h.call(params))
}
}