claude_agent_sdk/skills/
error.rs1use std::fmt;
4use thiserror::Error;
5
6#[derive(Error, Debug, Clone)]
8pub enum SkillError {
9 #[error("Skill validation failed: {0}")]
11 Validation(String),
12
13 #[error("Skill execution failed: {0}")]
15 Execution(String),
16
17 #[error("IO error: {0}")]
19 Io(String),
20
21 #[error("Serialization error: {0}")]
23 Serialization(String),
24
25 #[error("Skill not found: {0}")]
27 NotFound(String),
28
29 #[error("Skill already exists: {0}")]
31 AlreadyExists(String),
32
33 #[error("Invalid skill metadata: {0}")]
35 InvalidMetadata(String),
36
37 #[error("Skill version conflict: {0}")]
39 VersionConflict(String),
40
41 #[error("Configuration error: {0}")]
43 Configuration(String),
44}
45
46pub type Result<T> = std::result::Result<T, SkillError>;
48
49#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
51pub struct SkillOutput {
52 pub success: bool,
54
55 pub data: serde_json::Value,
57
58 #[serde(skip_serializing_if = "Option::is_none")]
60 pub error: Option<String>,
61
62 #[serde(skip_serializing_if = "Option::is_none")]
64 pub metadata: Option<serde_json::Value>,
65}
66
67impl SkillOutput {
68 pub fn ok(data: impl Into<serde_json::Value>) -> Self {
70 SkillOutput {
71 success: true,
72 data: data.into(),
73 error: None,
74 metadata: None,
75 }
76 }
77
78 pub fn err(error: impl Into<String>) -> Self {
80 SkillOutput {
81 success: false,
82 data: serde_json::Value::Null,
83 error: Some(error.into()),
84 metadata: None,
85 }
86 }
87
88 pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
90 self.metadata = Some(metadata);
91 self
92 }
93}
94
95impl fmt::Display for SkillOutput {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 if self.success {
98 write!(f, "Success: {}", self.data)
99 } else {
100 write!(
101 f,
102 "Error: {}",
103 self.error.as_deref().unwrap_or("Unknown error")
104 )
105 }
106 }
107}
108
109pub type SkillResult = Result<SkillOutput>;