1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#![deny(unsafe_code)]
#![deny(warnings)]
#![deny(clippy::all)]
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::DeriveInput;
#[proc_macro_derive(RocketGovernor)]
pub fn derive_rocket_governor_fn(input: TokenStream) -> TokenStream {
let ast: DeriveInput = syn::parse(input).unwrap();
let struct_name = &ast.ident;
let catcher_method = &format_ident!(
"{}_rocket_governor_catcher",
struct_name.to_string().to_lowercase()
);
let gen = quote! {
#[::rocket::async_trait]
impl<'r> ::rocket::request::FromRequest<'r> for #struct_name {
type Error = ::rocket_governor::LimitError;
async fn from_request(
request: &'r ::rocket::Request<'_>,
) -> ::rocket::request::Outcome<Self, ::rocket_governor::LimitError> {
::rocket_governor::RocketGovernorMacroUtil::<'r, #struct_name>::handle_from_request(request)
}
}
impl Default for #struct_name {
fn default() -> Self {
Self {}
}
}
#[::rocket::catch(429)]
pub fn #catcher_method<'c>(request: &'c ::rocket::Request) -> &'c ::rocket_governor::LimitError {
#struct_name::rocket_governor_catcher(request)
}
};
gen.into()
}
#[proc_macro_derive(RocketGovernorWithMember)]
pub fn derive_rocket_governor_with_member_fn(input: TokenStream) -> TokenStream {
let ast: DeriveInput = syn::parse(input).unwrap();
let struct_name = &ast.ident;
let catcher_method = &format_ident!(
"{}_rocket_governor_catcher",
struct_name.to_string().to_lowercase()
);
let gen = quote! {
#[::rocket::async_trait]
impl<'r> ::rocket::request::FromRequest<'r> for #struct_name {
type Error = ::rocket_governor::LimitError;
async fn from_request(
request: &'r ::rocket::Request<'_>,
) -> ::rocket::request::Outcome<Self, ::rocket_governor::LimitError> {
::rocket_governor::RocketGovernorMacroUtil::<'r, #struct_name>::handle_from_request(request)
}
}
#[::rocket::catch(429)]
pub fn #catcher_method<'c>(request: &'c ::rocket::Request) -> &'c ::rocket_governor::LimitError {
#struct_name::rocket_governor_catcher(request)
}
};
gen.into()
}