ngyn_macros 0.4.5

Modular backend framework for web applications
Documentation
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
extern crate proc_macro;

mod common;
mod core;
mod utils;

use crate::common::check::check_fn_macro;
use crate::common::{controller::*, injectable::*, route::*, routes::*};
use crate::core::dto::dto_macro;
use crate::core::module::*;
use crate::core::param::param_macro;
use crate::core::query::query_macro;
use common::check::check_impl_macro;
use common::http_code::http_code_macro;
use common::inject::inject_macro;
use proc_macro::TokenStream;

#[proc_macro_attribute]
/// `module` marks a struct that contains controllers and imports other modules.
///
/// ### Arguments
/// * `controllers` - A list of controllers that the module will contain.
/// * `imports` - A list of modules that the module will import.
/// * `init` - The name of the init method that will be called when the module is initialized.
///
/// ### Example
/// ```rust ignore
/// #[module(controllers = [MyController1, MyController2])]
/// struct MyModule;
/// ```
///
/// ### Example with imports
/// ```rust ignore
/// #[module(controllers = [MyController1, MyController2], imports = [MyModule1, MyModule2])]
/// struct MyModule;
/// ```
///
/// ### Example with init method
/// ```rust ignore
/// #[module(controllers = [MyController], imports = [MyModule], init = "start")]
/// struct MyModule;
///
/// impl MyModule {
///    fn start() {
///       // init method implementation
///   }
/// }
/// ```
pub fn module(args: TokenStream, input: TokenStream) -> TokenStream {
    module_macro(args, input)
}

#[proc_macro_attribute]
/// `injectable` attribute is used to mark a struct as injectable.
///
/// ### Arguments
/// * `init` - The name of the init method that will be called when the struct is initialized.
///
/// ### Example
/// ```rust ignore
/// #[injectable]
/// struct MyStruct {
///     // struct fields and methods
/// }
/// ```
pub fn injectable(args: TokenStream, input: TokenStream) -> TokenStream {
    injectable_macro(args, input)
}

#[proc_macro_attribute]
/// The `inject` attribute is used to mark a field as injectable.
/// The field must be a struct that is marked with the [`injectable`] attribute.
pub fn inject(args: TokenStream, input: TokenStream) -> TokenStream {
    inject_macro(args, input)
}

#[proc_macro_attribute]
/// `controller` attribute is used to mark a struct as a controller.
///
/// ### Example
/// ```rust ignore
/// #[controller]
/// struct MyController {
///     // controller fields and methods
/// }
/// ```
pub fn controller(args: TokenStream, input: TokenStream) -> TokenStream {
    controller_macro(args, input)
}

#[proc_macro_attribute]
/// The `routes` attribute is used to mark a struct impl as a routes container.
/// The struct must be marked with the [`controller`] attribute.
///
/// ### Panics
///
/// Panics if the attribute is not used on an impl block.
///
/// ### Example
/// ```rust ignore
/// #[controller]
/// struct MyController {
///     // controller fields and methods
/// }
///
/// #[routes]
/// impl MyController {
///     // route methods
/// }
/// ```
pub fn routes(_args: TokenStream, input: TokenStream) -> TokenStream {
    routes_macro(input)
}

#[proc_macro_attribute]
/// The `route` attribute is used to mark a method of a controller impl as a route.
///
/// ### Arguments
/// * `method` - The HTTP method that the route will handle. (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS)
/// * `path` - The path that the route will handle.
///
/// ### Example
/// ```rust ignore
/// #[controller]
/// struct MyController {
///     // controller fields and methods
/// }
///
/// #[routes]
/// impl MyController {
///     #[route("GET", "/users")]
///     fn get_users(&self) {
///         // route implementation
///     }
/// }
/// ```
pub fn route(args: TokenStream, input: TokenStream) -> TokenStream {
    route_macro(args, input)
}

#[proc_macro_attribute]
/// The `get` attribute is used to mark a controller method as a `GET` route.
///
/// ### Arguments
/// * `path` - The path that the route will handle.
///
/// ### Example
/// ```rust ignore
/// #[controller]
/// struct MyController {
///     // controller fields and methods
/// }
///
/// #[routes]
/// impl MyController {
///     #[get("/users")]
///     fn get_users(&self) {
///         // route implementation
///     }
/// }
/// ```
pub fn get(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = syn::parse_str::<syn::Expr>(args.to_string().as_str()).unwrap();
    let args_with_method = quote::quote! { "GET", #args };
    route_macro(args_with_method.into(), input)
}

#[proc_macro_attribute]
/// The `post` attribute is used to mark a controller method as a `POST` route.
///
/// ### Arguments
/// * `path` - The path that the route will handle.
///
/// ### Example
/// ```rust ignore
/// #[controller]
/// struct MyController {
///     // controller fields and methods
/// }
///
/// #[routes]
/// impl MyController {
///     #[post("/users")]
///     fn create_user(&self) {
///         // route implementation
///     }
/// }
/// ```
pub fn post(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = syn::parse_str::<syn::Expr>(args.to_string().as_str()).unwrap();
    let args_with_method = quote::quote! { "POST", #args };
    route_macro(args_with_method.into(), input)
}

#[proc_macro_attribute]
/// The `put` attribute is used to mark a controller method as a `PUT` route.
///
/// ### Arguments
/// * `path` - The path that the route will handle.
///
/// ### Example
/// ```rust ignore
/// #[controller]
/// struct MyController {
///     // controller fields and methods
/// }
///
/// #[routes]
/// impl MyController {
///     #[put("/users")]
///     fn update_user(&self) {
///         // route implementation
///     }
/// }
/// ```
pub fn put(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = syn::parse_str::<syn::Expr>(args.to_string().as_str()).unwrap();
    let args_with_method = quote::quote! { "PUT", #args };
    route_macro(args_with_method.into(), input)
}

#[proc_macro_attribute]
/// The `delete` attribute is used to mark a controller method as a `DELETE` route.
///
/// ### Arguments
/// * `path` - The path that the route will handle.
///
/// ### Example
/// ```rust ignore
/// #[controller]
/// struct MyController {
///     // controller fields and methods
/// }
///
/// #[routes]
/// impl MyController {
///     #[delete("/users")]
///     fn delete_user(&self) {
///         // route implementation
///     }
/// }
/// ```
pub fn delete(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = syn::parse_str::<syn::Expr>(args.to_string().as_str()).unwrap();
    let args_with_method = quote::quote! { "DELETE", #args };
    route_macro(args_with_method.into(), input)
}

#[proc_macro_attribute]
/// The `patch` attribute is used to mark a method of a controller impl as a `PATCH` route.
///
/// ### Arguments
/// * `path` - The path that the route will handle.
///
/// ### Example
/// ```rust ignore
/// #[controller]
/// struct MyController {
///     // controller fields and methods
/// }
///
/// #[routes]
/// impl MyController {
///     #[patch("/users")]
///     fn update_user(&self) {
///         // route implementation
///     }
/// }
/// ```
pub fn patch(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = syn::parse_str::<syn::Expr>(args.to_string().as_str()).unwrap();
    let args_with_method = quote::quote! { "PATCH", #args };
    route_macro(args_with_method.into(), input)
}

#[proc_macro_attribute]
/// The `head` attribute is used to mark a controller method as a `HEAD` route.
///
/// ### Arguments
/// * `path` - The path that the route will handle.
///
/// ### Example
/// ```rust ignore
/// #[controller]
/// struct MyController {
///     // controller fields and methods
/// }
///
/// #[routes]
/// impl MyController {
///     #[head("/users")]
///     fn get_users(&self) {
///         // route implementation
///     }
/// }
/// ```
pub fn head(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = syn::parse_str::<syn::Expr>(args.to_string().as_str()).unwrap();
    let args_with_method = quote::quote! { "HEAD", #args };
    route_macro(args_with_method.into(), input)
}

#[proc_macro_attribute]
/// The `check` macro is used to determine if a route should be executed.
/// If the gate returns false, the route will not be executed.
///
/// ### Arguments
///
/// * `CheckGate` - The path to the gate that will be used to determine if the route should be executed.
///
/// ### Panics
///
/// Panics if the attribute is not used on a method or an impl block.
///
/// ### Example
/// ```rust ignore
/// #[check(CheckGate)]
/// fn my_route(&self) {
///     // route implementation
/// }
/// ```
pub fn check(args: TokenStream, input: TokenStream) -> TokenStream {
    let parsed_input = syn::parse::<syn::Item>(input);
    match parsed_input {
        Ok(syn::Item::Fn(input)) => check_fn_macro(args, input),
        Ok(syn::Item::Impl(impl_item)) => check_impl_macro(args, impl_item),
        _ => panic!("`check` attribute can only be used on methods or impl blocks"),
    }
}

#[proc_macro_attribute]
/// The `http_code` macro is used to set the default HTTP status code for a route.
///
/// ### Arguments
/// * `code` - The HTTP status code to set.
///
/// ### Example
/// ```rust ignore
/// #[http_code(200)]
/// fn my_route(&self) {
///    // route implementation
/// }
/// ```
pub fn http_code(args: TokenStream, input: TokenStream) -> TokenStream {
    http_code_macro(args, input)
}

#[proc_macro_derive(Dto)]
/// The `Dto` derive macro is used to generate a DTO struct.
///
/// ### Example
/// ```rust ignore
/// #[derive(Dto)]
/// struct MyDto {
///     // fields
/// }
/// ```
pub fn dto_derive_dto(input: TokenStream) -> TokenStream {
    dto_macro(input)
}

#[proc_macro_derive(Query)]
/// The `Query` derive macro is used to derive a struct that can be used to parse query parameters.
///
/// ### Example
/// ```rust ignore
/// #[derive(Query)]
/// struct MyQuery {
///    page: u32,
///   limit: u32,
/// }
/// ```
pub fn query_derive_macro(input: TokenStream) -> TokenStream {
    query_macro(input)
}

#[proc_macro_derive(Param)]
/// The `Param` derive macro is used to derive a struct that can be used to parse route parameters.
///
/// ### Example
/// ```rust ignore
/// #[derive(Param)]
/// struct MyParam {
///   id: u32,
/// }
/// ```
pub fn param_derive_macro(input: TokenStream) -> TokenStream {
    param_macro(input)
}

#[proc_macro_derive(AppState)]
/// The `AppState` derive macro is used to derive a struct that can be used as a state in a server.
///
/// ### Example
/// ```rust ignore
/// #[derive(AppState)]
/// struct MyState {
///    // fields
/// }
/// ```
pub fn app_state_derive_macro(input: TokenStream) -> TokenStream {
    common::state::derive_app_state_macro(input)
}