use serde_json::Value;
use std::collections::HashMap;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ToolResult {
pub success: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub metadata: HashMap<String, Value>,
}
impl ToolResult {
pub fn success(data: Option<Value>) -> Self {
Self {
success: true,
data,
error: None,
metadata: HashMap::new(),
}
}
pub fn success_with<T: serde::Serialize>(data: T) -> Self {
Self {
success: true,
data: serde_json::to_value(data).ok(),
error: None,
metadata: HashMap::new(),
}
}
pub fn failure(error: impl Into<String>) -> Self {
Self {
success: false,
data: None,
error: Some(error.into()),
metadata: HashMap::new(),
}
}
pub fn failure_with<T: serde::Serialize>(error: impl Into<String>, data: T) -> Self {
Self {
success: false,
data: serde_json::to_value(data).ok(),
error: Some(error.into()),
metadata: HashMap::new(),
}
}
pub fn with_metadata(mut self, key: impl Into<String>, value: Value) -> Self {
self.metadata.insert(key.into(), value);
self
}
}