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 #[allow(dead_code)]
25 AgentNotFound(String),
26 InvalidAgent(String),
28 ManifestError(String),
30 FileSystem(String),
32 #[allow(dead_code)]
34 Network(String),
35 Other(String),
37}
38
39impl fmt::Display for CarpError {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 match self {
42 CarpError::Io(e) => write!(f, "IO error: {e}"),
43 CarpError::Http(e) => write!(f, "HTTP error: {e}"),
44 CarpError::Json(e) => write!(f, "JSON error: {e}"),
45 CarpError::Toml(e) => write!(f, "TOML error: {e}"),
46 CarpError::Config(msg) => write!(f, "Configuration error: {msg}"),
47 CarpError::Auth(msg) => write!(f, "Authentication error: {msg}"),
48 CarpError::Api { status, message } => {
49 write!(f, "API error ({status}): {message}")
50 }
51 CarpError::AgentNotFound(name) => write!(f, "Agent '{name}' not found"),
52 CarpError::InvalidAgent(msg) => write!(f, "Invalid agent: {msg}"),
53 CarpError::ManifestError(msg) => write!(f, "Manifest error: {msg}"),
54 CarpError::FileSystem(msg) => write!(f, "File system error: {msg}"),
55 CarpError::Network(msg) => write!(f, "Network error: {msg}"),
56 CarpError::Other(msg) => write!(f, "{msg}"),
57 }
58 }
59}
60
61impl std::error::Error for CarpError {
62 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
63 match self {
64 CarpError::Io(e) => Some(e),
65 CarpError::Http(e) => Some(e),
66 CarpError::Json(e) => Some(e),
67 CarpError::Toml(e) => Some(e),
68 _ => None,
69 }
70 }
71}
72
73impl From<std::io::Error> for CarpError {
74 fn from(err: std::io::Error) -> Self {
75 CarpError::Io(err)
76 }
77}
78
79impl From<reqwest::Error> for CarpError {
80 fn from(err: reqwest::Error) -> Self {
81 CarpError::Http(err)
82 }
83}
84
85impl From<serde_json::Error> for CarpError {
86 fn from(err: serde_json::Error) -> Self {
87 CarpError::Json(err)
88 }
89}
90
91impl From<toml::de::Error> for CarpError {
92 fn from(err: toml::de::Error) -> Self {
93 CarpError::Toml(err)
94 }
95}
96
97impl From<zip::result::ZipError> for CarpError {
98 fn from(err: zip::result::ZipError) -> Self {
99 CarpError::FileSystem(format!("ZIP error: {err}"))
100 }
101}