use core::fmt;
use error_forge::ForgeError;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BucketError {
ZeroCapacity,
ZeroRefillAmount,
ZeroRefillPeriod,
}
impl fmt::Display for BucketError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let message = match self {
Self::ZeroCapacity => "bucket capacity must be greater than zero",
Self::ZeroRefillAmount => "refill amount must be greater than zero",
Self::ZeroRefillPeriod => "refill period must be greater than zero",
};
f.write_str(message)
}
}
impl std::error::Error for BucketError {}
impl ForgeError for BucketError {
fn kind(&self) -> &'static str {
match self {
Self::ZeroCapacity => "ZeroCapacity",
Self::ZeroRefillAmount => "ZeroRefillAmount",
Self::ZeroRefillPeriod => "ZeroRefillPeriod",
}
}
fn caption(&self) -> &'static str {
"Invalid bucket configuration"
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::BucketError;
use error_forge::ForgeError;
#[test]
fn test_display_names_the_violated_constraint() {
assert!(BucketError::ZeroCapacity.to_string().contains("capacity"));
assert!(BucketError::ZeroRefillAmount.to_string().contains("amount"));
assert!(BucketError::ZeroRefillPeriod.to_string().contains("period"));
}
#[test]
fn test_forge_kind_matches_variant() {
assert_eq!(BucketError::ZeroCapacity.kind(), "ZeroCapacity");
assert_eq!(BucketError::ZeroRefillAmount.kind(), "ZeroRefillAmount");
assert_eq!(BucketError::ZeroRefillPeriod.kind(), "ZeroRefillPeriod");
}
#[test]
fn test_config_errors_are_not_retryable() {
assert!(!BucketError::ZeroCapacity.is_retryable());
}
}