tower_redis_cell/
config.rs1use crate::error::Error;
2use crate::rule::RequestAllowedDetails;
3
4pub(crate) type SyncSuccessHandler<RespTy> =
5 Box<dyn Fn(RequestAllowedDetails, &mut RespTy) + Send + Sync + 'static>;
6
7pub(crate) type SyncUnruledHandler<RespTy> = Box<dyn Fn(&mut RespTy) + Send + Sync + 'static>;
8
9pub(crate) type SyncErrorHandler<ReqTy, IntoRespTy> =
10 Box<dyn Fn(Error, &ReqTy) -> IntoRespTy + Send + Sync + 'static>;
11
12pub(crate) enum OnSuccess<RespTy> {
13 Noop,
14 Sync(SyncSuccessHandler<RespTy>),
15}
16
17pub(crate) enum OnUnruled<RespTy> {
18 Noop,
19 Sync(SyncUnruledHandler<RespTy>),
20}
21
22pub(crate) enum OnError<ReqTy, IntoRespTy> {
23 Sync(SyncErrorHandler<ReqTy, IntoRespTy>),
24}
25
26pub struct RateLimitConfig<PR, ReqTy, RespTy, IntoRespTy> {
27 pub(crate) rule_provider: PR,
28 pub(crate) on_error: OnError<ReqTy, IntoRespTy>,
29 pub(crate) on_success: OnSuccess<RespTy>,
30 pub(crate) on_unruled: OnUnruled<RespTy>,
31}
32
33impl<RP, ReqTy, RespTy, IntoRespTy> RateLimitConfig<RP, ReqTy, RespTy, IntoRespTy> {
34 pub fn new<EH>(rule_provider: RP, error_handler: EH) -> Self
35 where
36 EH: Fn(Error, &ReqTy) -> IntoRespTy + Send + Sync + 'static,
37 {
38 RateLimitConfig {
39 rule_provider,
40 on_error: OnError::Sync(Box::new(error_handler)),
41 on_success: OnSuccess::Noop,
42 on_unruled: OnUnruled::Noop,
43 }
44 }
45
46 pub fn on_success<H>(mut self, handler: H) -> Self
47 where
48 H: Fn(RequestAllowedDetails, &mut RespTy) + Send + Sync + 'static,
49 {
50 self.on_success = OnSuccess::Sync(Box::new(handler));
51 self
52 }
53
54 pub fn on_unruled<H>(mut self, handler: H) -> Self
55 where
56 H: Fn(&mut RespTy) + Send + Sync + 'static,
57 {
58 self.on_unruled = OnUnruled::Sync(Box::new(handler));
59 self
60 }
61}