Skip to main content

erio_tools/
result.rs

1//! Tool execution result types.
2
3use serde::{Deserialize, Serialize};
4
5/// Result of a tool execution.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(untagged)]
8pub enum ToolResult {
9    /// Successful execution with content.
10    Success { success: bool, content: String },
11    /// Failed execution with error message.
12    Error { success: bool, error: String },
13}
14
15impl ToolResult {
16    /// Creates a successful result with the given content.
17    pub fn success(content: impl Into<String>) -> Self {
18        Self::Success {
19            success: true,
20            content: content.into(),
21        }
22    }
23
24    /// Creates an error result with the given message.
25    pub fn error(message: impl Into<String>) -> Self {
26        Self::Error {
27            success: false,
28            error: message.into(),
29        }
30    }
31
32    /// Returns `true` if this is a successful result.
33    pub fn is_success(&self) -> bool {
34        matches!(self, Self::Success { .. })
35    }
36
37    /// Returns the content if this is a successful result.
38    pub fn content(&self) -> Option<&str> {
39        match self {
40            Self::Success { content, .. } => Some(content),
41            Self::Error { .. } => None,
42        }
43    }
44
45    /// Returns the error message if this is an error result.
46    pub fn error_message(&self) -> Option<&str> {
47        match self {
48            Self::Success { .. } => None,
49            Self::Error { error, .. } => Some(error),
50        }
51    }
52}