use core::fmt;
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BunsenErrorKind {
Illegal,
Internal,
Policy,
Lookup,
Unavailable,
InvalidResource,
Unsupported,
Sys,
Other,
}
impl BunsenErrorKind {
pub fn is_bug(self) -> bool {
matches!(self, Self::Illegal | Self::Internal)
}
pub fn is_retryable(self) -> bool {
self == Self::Unavailable
}
pub fn is_runtime(self) -> bool {
!self.is_bug()
}
pub fn name(self) -> &'static str {
match self {
Self::Illegal => "Illegal",
Self::Internal => "Internal",
Self::Policy => "Policy",
Self::Lookup => "Lookup",
Self::Unavailable => "Unavailable",
Self::InvalidResource => "InvalidResource",
Self::Unsupported => "Unsupported",
Self::Sys => "Sys",
Self::Other => "Other",
}
}
}
impl fmt::Display for BunsenErrorKind {
fn fmt(
&self,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
f.write_str(self.name())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bugs_and_retryable() {
assert!(BunsenErrorKind::Illegal.is_bug());
assert!(BunsenErrorKind::Internal.is_bug());
assert!(!BunsenErrorKind::Policy.is_bug());
assert!(BunsenErrorKind::Policy.is_runtime());
assert!(BunsenErrorKind::Unavailable.is_retryable());
assert!(!BunsenErrorKind::Lookup.is_retryable());
assert!(!BunsenErrorKind::Illegal.is_retryable());
}
#[test]
fn test_display() {
assert_eq!(
BunsenErrorKind::InvalidResource.to_string(),
"InvalidResource"
);
}
}