gopher_mcp_rust/
result.rs1use std::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum AgentResultStatus {
8 Success,
10 Error,
12 Timeout,
14 MaxIterationsReached,
16}
17
18impl fmt::Display for AgentResultStatus {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 match self {
21 AgentResultStatus::Success => write!(f, "SUCCESS"),
22 AgentResultStatus::Error => write!(f, "ERROR"),
23 AgentResultStatus::Timeout => write!(f, "TIMEOUT"),
24 AgentResultStatus::MaxIterationsReached => write!(f, "MAX_ITERATIONS_REACHED"),
25 }
26 }
27}
28
29#[derive(Debug, Clone)]
31pub struct AgentResult {
32 response: String,
33 status: AgentResultStatus,
34 error_message: Option<String>,
35 iteration_count: usize,
36 tokens_used: u64,
37}
38
39impl AgentResult {
40 pub fn response(&self) -> &str {
42 &self.response
43 }
44
45 pub fn status(&self) -> AgentResultStatus {
47 self.status
48 }
49
50 pub fn error_message(&self) -> Option<&str> {
52 self.error_message.as_deref()
53 }
54
55 pub fn iteration_count(&self) -> usize {
57 self.iteration_count
58 }
59
60 pub fn tokens_used(&self) -> u64 {
62 self.tokens_used
63 }
64
65 pub fn is_success(&self) -> bool {
67 self.status == AgentResultStatus::Success
68 }
69
70 pub fn success<S: Into<String>>(response: S) -> Self {
72 AgentResult {
73 response: response.into(),
74 status: AgentResultStatus::Success,
75 error_message: None,
76 iteration_count: 1,
77 tokens_used: 0,
78 }
79 }
80
81 pub fn error<S: Into<String>>(message: S) -> Self {
83 AgentResult {
84 response: String::new(),
85 status: AgentResultStatus::Error,
86 error_message: Some(message.into()),
87 iteration_count: 0,
88 tokens_used: 0,
89 }
90 }
91
92 pub fn timeout<S: Into<String>>(message: S) -> Self {
94 AgentResult {
95 response: String::new(),
96 status: AgentResultStatus::Timeout,
97 error_message: Some(message.into()),
98 iteration_count: 0,
99 tokens_used: 0,
100 }
101 }
102}
103
104#[derive(Debug, Default)]
106#[allow(dead_code)]
107pub struct AgentResultBuilder {
108 response: String,
109 status: Option<AgentResultStatus>,
110 error_message: Option<String>,
111 iteration_count: usize,
112 tokens_used: u64,
113}
114
115#[allow(dead_code)]
116impl AgentResultBuilder {
117 pub fn new() -> Self {
119 Self::default()
120 }
121
122 pub fn with_response<S: Into<String>>(mut self, response: S) -> Self {
124 self.response = response.into();
125 self
126 }
127
128 pub fn with_status(mut self, status: AgentResultStatus) -> Self {
130 self.status = Some(status);
131 self
132 }
133
134 pub fn with_error_message<S: Into<String>>(mut self, message: S) -> Self {
136 self.error_message = Some(message.into());
137 self
138 }
139
140 pub fn with_iteration_count(mut self, count: usize) -> Self {
142 self.iteration_count = count;
143 self
144 }
145
146 pub fn with_tokens_used(mut self, tokens: u64) -> Self {
148 self.tokens_used = tokens;
149 self
150 }
151
152 pub fn build(self) -> AgentResult {
154 AgentResult {
155 response: self.response,
156 status: self.status.unwrap_or(AgentResultStatus::Success),
157 error_message: self.error_message,
158 iteration_count: self.iteration_count,
159 tokens_used: self.tokens_used,
160 }
161 }
162}
163
164#[cfg(test)]
165mod tests {
166 use super::*;
167
168 #[test]
169 fn test_success_result() {
170 let result = AgentResult::success("Hello, world!");
171
172 assert_eq!(result.response(), "Hello, world!");
173 assert_eq!(result.status(), AgentResultStatus::Success);
174 assert!(result.is_success());
175 }
176
177 #[test]
178 fn test_error_result() {
179 let result = AgentResult::error("Something went wrong");
180
181 assert_eq!(result.error_message(), Some("Something went wrong"));
182 assert_eq!(result.status(), AgentResultStatus::Error);
183 assert!(!result.is_success());
184 }
185
186 #[test]
187 fn test_timeout_result() {
188 let result = AgentResult::timeout("Operation timed out");
189
190 assert_eq!(result.error_message(), Some("Operation timed out"));
191 assert_eq!(result.status(), AgentResultStatus::Timeout);
192 assert!(!result.is_success());
193 }
194
195 #[test]
196 fn test_builder() {
197 let result = AgentResultBuilder::new()
198 .with_response("Test response")
199 .with_status(AgentResultStatus::Success)
200 .with_iteration_count(5)
201 .with_tokens_used(100)
202 .build();
203
204 assert_eq!(result.response(), "Test response");
205 assert_eq!(result.status(), AgentResultStatus::Success);
206 assert_eq!(result.iteration_count(), 5);
207 assert_eq!(result.tokens_used(), 100);
208 }
209
210 #[test]
211 fn test_status_display() {
212 assert_eq!(AgentResultStatus::Success.to_string(), "SUCCESS");
213 assert_eq!(AgentResultStatus::Error.to_string(), "ERROR");
214 assert_eq!(AgentResultStatus::Timeout.to_string(), "TIMEOUT");
215 assert_eq!(
216 AgentResultStatus::MaxIterationsReached.to_string(),
217 "MAX_ITERATIONS_REACHED"
218 );
219 }
220}