1use crate::types::Uint;
5use core::fmt;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum SimpleRateLimitError {
10 InsufficientCapacity,
11 BeyondCapacity,
12 ExpiredTick,
13 ContentionFailure,
14}
15
16pub type SimpleRateLimitResult = Result<(), SimpleRateLimitError>;
18
19#[derive(Debug, Clone, PartialEq, Eq)]
21pub enum VerboseRateLimitError {
22 InsufficientCapacity {
24 acquiring: Uint,
25 available: Uint,
26 retry_after_ticks: Uint,
27 },
28 BeyondCapacity {
30 acquiring: Uint,
31 capacity: Uint,
32 },
33 ExpiredTick {
35 min_acceptable_tick: Uint,
36 },
37 ContentionFailure,
39}
40
41pub type VerboseRateLimitResult = Result<(), VerboseRateLimitError>;
43
44impl fmt::Display for SimpleRateLimitError {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 use SimpleRateLimitError::*;
48 match self {
49 InsufficientCapacity => write!(f, "Insufficient capacity (fast path)."),
50 BeyondCapacity => write!(f, "Request exceeds maximum capacity (fast path)."),
51 ExpiredTick => write!(f, "Expired tick (fast path)."),
52 ContentionFailure => write!(f, "Contention failure (fast path)."),
53 }
54 }
55}
56
57impl fmt::Display for VerboseRateLimitError {
59 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60 use VerboseRateLimitError::*;
61 match self {
62 InsufficientCapacity { acquiring, available, retry_after_ticks } => {
63 write!(
64 f,
65 "Insufficient capacity: tried to acquire {}, available {}, retry after {} tick(s).",
66 acquiring, available, retry_after_ticks
67 )
68 }
69 BeyondCapacity { acquiring, capacity } => {
70 write!(
71 f,
72 "Request exceeds maximum capacity: tried to acquire {}, capacity {}. This request cannot succeed.",
73 acquiring, capacity
74 )
75 }
76 ExpiredTick { min_acceptable_tick } => {
77 write!(
78 f,
79 "Expired tick: minimum acceptable tick is {}.",
80 min_acceptable_tick
81 )
82 }
83 ContentionFailure => {
84 write!(f, "Contention failure: resource is locked by another operation. Please retry.")
85 }
86 }
87 }
88}
89
90impl std::error::Error for SimpleRateLimitError {}
91impl std::error::Error for VerboseRateLimitError {}