use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("Configuration error: {0}")]
Config(String),
#[error("Invalid path: {0}")]
InvalidPath(String),
#[error("Alias not found: {0}")]
AliasNotFound(String),
#[error("Alias already exists: {0}")]
AliasExists(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("TOML parse error: {0}")]
TomlParse(#[from] toml::de::Error),
#[error("TOML serialization error: {0}")]
TomlSerialize(#[from] toml::ser::Error),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("Invalid URL: {0}")]
InvalidUrl(#[from] url::ParseError),
#[error("Authentication failed: {0}")]
Auth(String),
#[error("Not found: {0}")]
NotFound(String),
#[error("Network error: {0}")]
Network(String),
#[error("Conflict: {0}")]
Conflict(String),
#[error("Unsupported feature: {0}")]
UnsupportedFeature(String),
#[error("{0}")]
General(String),
}
impl Error {
pub const fn exit_code(&self) -> i32 {
match self {
Error::InvalidPath(_) => 2, Error::Config(_) => 2, Error::Network(_) => 3, Error::Auth(_) => 4, Error::NotFound(_) | Error::AliasNotFound(_) => 5, Error::Conflict(_) | Error::AliasExists(_) => 6, Error::UnsupportedFeature(_) => 7, _ => 1, }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_exit_codes() {
assert_eq!(Error::InvalidPath("test".into()).exit_code(), 2);
assert_eq!(Error::Config("test".into()).exit_code(), 2);
assert_eq!(Error::Network("test".into()).exit_code(), 3);
assert_eq!(Error::Auth("test".into()).exit_code(), 4);
assert_eq!(Error::NotFound("test".into()).exit_code(), 5);
assert_eq!(Error::AliasNotFound("test".into()).exit_code(), 5);
assert_eq!(Error::Conflict("test".into()).exit_code(), 6);
assert_eq!(Error::AliasExists("test".into()).exit_code(), 6);
assert_eq!(Error::UnsupportedFeature("test".into()).exit_code(), 7);
assert_eq!(Error::General("test".into()).exit_code(), 1);
}
#[test]
fn test_error_display() {
let err = Error::AliasNotFound("myalias".into());
assert_eq!(err.to_string(), "Alias not found: myalias");
let err = Error::InvalidPath("/bad/path".into());
assert_eq!(err.to_string(), "Invalid path: /bad/path");
}
}