1use std::fmt;
2
3pub type CarpResult<T> = Result<T, CarpError>;
5
6#[derive(Debug)]
8pub enum CarpError {
9 Io(std::io::Error),
11 Http(reqwest::Error),
13 Json(serde_json::Error),
15 Toml(toml::de::Error),
17 Config(String),
19 Auth(String),
21 Api { status: u16, message: String },
23 AgentNotFound(String),
25 InvalidAgent(String),
27 ManifestError(String),
29 FileSystem(String),
31 Network(String),
33 Other(String),
35}
36
37impl fmt::Display for CarpError {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 match self {
40 CarpError::Io(e) => write!(f, "IO error: {}", e),
41 CarpError::Http(e) => write!(f, "HTTP error: {}", e),
42 CarpError::Json(e) => write!(f, "JSON error: {}", e),
43 CarpError::Toml(e) => write!(f, "TOML error: {}", e),
44 CarpError::Config(msg) => write!(f, "Configuration error: {}", msg),
45 CarpError::Auth(msg) => write!(f, "Authentication error: {}", msg),
46 CarpError::Api { status, message } => {
47 write!(f, "API error ({}): {}", status, message)
48 }
49 CarpError::AgentNotFound(name) => write!(f, "Agent '{}' not found", name),
50 CarpError::InvalidAgent(msg) => write!(f, "Invalid agent: {}", msg),
51 CarpError::ManifestError(msg) => write!(f, "Manifest error: {}", msg),
52 CarpError::FileSystem(msg) => write!(f, "File system error: {}", msg),
53 CarpError::Network(msg) => write!(f, "Network error: {}", msg),
54 CarpError::Other(msg) => write!(f, "{}", msg),
55 }
56 }
57}
58
59impl std::error::Error for CarpError {
60 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
61 match self {
62 CarpError::Io(e) => Some(e),
63 CarpError::Http(e) => Some(e),
64 CarpError::Json(e) => Some(e),
65 CarpError::Toml(e) => Some(e),
66 _ => None,
67 }
68 }
69}
70
71impl From<std::io::Error> for CarpError {
72 fn from(err: std::io::Error) -> Self {
73 CarpError::Io(err)
74 }
75}
76
77impl From<reqwest::Error> for CarpError {
78 fn from(err: reqwest::Error) -> Self {
79 CarpError::Http(err)
80 }
81}
82
83impl From<serde_json::Error> for CarpError {
84 fn from(err: serde_json::Error) -> Self {
85 CarpError::Json(err)
86 }
87}
88
89impl From<toml::de::Error> for CarpError {
90 fn from(err: toml::de::Error) -> Self {
91 CarpError::Toml(err)
92 }
93}
94
95impl From<zip::result::ZipError> for CarpError {
96 fn from(err: zip::result::ZipError) -> Self {
97 CarpError::FileSystem(format!("ZIP error: {}", err))
98 }
99}