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