#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BackendError {
UnsupportedVerb,
InvalidArgs(String),
Internal(String),
}
impl BackendError {
pub fn exit_code(&self) -> u8 {
match self {
BackendError::UnsupportedVerb | BackendError::InvalidArgs(_) => {
crate::error::EXIT_USAGE
}
BackendError::Internal(_) => crate::error::EXIT_INTERNAL,
}
}
}
impl std::fmt::Display for BackendError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BackendError::UnsupportedVerb => f.write_str("this backend does not support that verb"),
BackendError::InvalidArgs(m) => write!(f, "the backend rejected the arguments: {m}"),
BackendError::Internal(m) => write!(f, "the backend failed internally: {m}"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn backend_error_exit_codes_match_the_table() {
assert_eq!(
BackendError::UnsupportedVerb.exit_code(),
crate::error::EXIT_USAGE
);
assert_eq!(
BackendError::InvalidArgs("x".into()).exit_code(),
crate::error::EXIT_USAGE
);
assert_eq!(
BackendError::Internal("x".into()).exit_code(),
crate::error::EXIT_INTERNAL
);
}
#[test]
fn backend_error_messages_name_the_backend_situation() {
assert!(BackendError::UnsupportedVerb
.to_string()
.contains("does not support"));
assert!(BackendError::InvalidArgs("a".into())
.to_string()
.contains('a'));
assert!(BackendError::Internal("b".into()).to_string().contains('b'));
}
}