use std::time::Duration;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum RateLimitResult {
Allowed {
remaining_burst: u32,
reset_after: Duration,
},
Limited {
retry_after: Duration,
reset_after: Duration,
},
}
impl RateLimitResult {
#[inline]
pub fn is_allowed(&self) -> bool {
matches!(self, Self::Allowed { .. })
}
#[inline]
pub fn is_limited(&self) -> bool {
matches!(self, Self::Limited { .. })
}
#[inline]
pub fn retry_after(&self) -> Option<Duration> {
match self {
Self::Limited { retry_after, .. } => Some(*retry_after),
Self::Allowed { .. } => None,
}
}
#[inline]
pub fn remaining_burst(&self) -> u32 {
match self {
Self::Allowed {
remaining_burst, ..
} => *remaining_burst,
Self::Limited { .. } => 0,
}
}
#[inline]
pub fn reset_after(&self) -> Duration {
match self {
Self::Allowed { reset_after, .. } | Self::Limited { reset_after, .. } => *reset_after,
}
}
}