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