sf_rate_limiter/
lib.rs

1pub mod error;
2pub mod policy;
3pub mod storage;
4
5mod rate_limit;
6mod reservation;
7
8use chrono::DateTime;
9use error::BuilderError;
10use policy::Policy;
11
12pub use rate_limit::RateLimit;
13pub use reservation::Reservation;
14
15pub(crate) use chrono::Local as LocalTime;
16pub(crate) type LocalDateTime = DateTime<LocalTime>;
17pub(crate) type ChronoTimestampMillis = i64;
18pub type Duration = chrono::Duration;
19
20#[derive(Debug)]
21pub struct RateLimiterBuilder<P: Policy> {
22    key: String,
23    policy: Option<P>,
24}
25
26impl<P: Policy> RateLimiterBuilder<P> {
27    pub fn new() -> Self {
28        Self {
29            key: Default::default(),
30            policy: None,
31        }
32    }
33
34    pub fn with_key<S: Into<String>>(mut self, key: S) -> Self {
35        self.key = key.into();
36        self
37    }
38
39    pub fn with_policy(mut self, policy: P) -> Self {
40        self.policy = Some(policy);
41        self
42    }
43
44    pub fn build(self) -> Result<(), BuilderError> {
45        if self.key.is_empty() {
46            return Err(BuilderError::KeyNotConfiguredError);
47        }
48
49        let Some(policy) = self.policy else {
50            return Err(BuilderError::PolicyNotConfiguredError);
51        };
52
53        Ok(())
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60    use crate::policy::FixedWindowPolicy;
61    use crate::storage::InMemoryStorage;
62
63    #[test]
64    fn abs() {}
65}