arh_macros/lib.rs
1use proc_macro::TokenStream;
2use syn::{parse_macro_input, ItemImpl};
3
4mod impls;
5mod models;
6use models::RouterAttributes;
7
8/// Attribute macro to define a router.
9///
10/// # Arguments
11///
12/// * `state` - Application state. Optional and set to `()` by default.
13/// * `session_type` - the type of the session data. Optional and set to `SessionData` by default.
14/// * `base_path` - the base path of these routes (must start with "/"). Optional and set to `""` by default.
15///
16/// # Example
17///
18/// ```rust
19/// #[router(state=AppState, session_type=SessionData, base_path="/path")]
20/// impl MyRouter {
21/// // implementation
22/// }
23/// ```
24/// State is optional
25/// ```rust
26/// #[router("/path")]
27/// impl MyRouter {
28/// // implementation
29/// }
30/// ```
31#[proc_macro_attribute]
32pub fn router(attr: TokenStream, item: TokenStream) -> TokenStream {
33 let parsed_item = parse_macro_input!(item as ItemImpl);
34 let router_attr = parse_macro_input!(attr as RouterAttributes);
35 impls::router(router_attr, parsed_item)
36}
37
38/// Derive macro to generate helper methods for a router.
39///
40/// # Arguments
41///
42/// * `router_config` - proc macro argument that start with the application's state and followd by all the routers.
43///
44/// # Example
45///
46/// ```rust
47/// #[derive(RouterHelper)]
48/// #[router_config(AppState, MyRouter, AnotherRouter, ...)]
49/// struct MyRouter {
50/// // fields
51/// }
52/// ```
53#[proc_macro_derive(RouterHelper, attributes(router_config))]
54pub fn router_helper_derive(input: TokenStream) -> TokenStream {
55 let input = parse_macro_input!(input as syn::ItemStruct);
56 impls::router_helper_derive(input)
57}
58
59/// Attribute macro to define a POST route.
60///
61/// # Arguments
62///
63/// * `path` - the route path.
64/// * `auth` - optional boolean to tell if the route need to be authenticated.
65///
66/// # Example
67///
68/// ```rust
69/// #[post("/path")]
70/// fn my_post_route() {
71/// // implementation
72/// }
73/// ```
74#[proc_macro_attribute]
75pub fn post(_attr: TokenStream, item: TokenStream) -> TokenStream {
76 item
77}
78
79/// Attribute macro to define a PUT route.
80/// # Arguments
81/// * `path` - the route path.
82/// * `auth` - optional boolean to tell if the route need to be authenticated.
83/// # Example
84/// ```rust
85/// #[put("/path", auth=true)]
86/// fn my_put_route() {
87/// // implementation
88/// }
89/// ```
90///
91#[proc_macro_attribute]
92pub fn put(_attr: TokenStream, item: TokenStream) -> TokenStream {
93 item
94}
95/// Attribute macro to define a GET route.
96/// # Arguments
97/// * `path` - the route path.
98/// * `auth` - optional boolean to tell if the route need to be authenticated.
99/// # Example
100/// ```rust
101/// #[get("/path")]
102/// fn my_get_route() {
103/// // implementation
104/// }
105/// ```
106#[proc_macro_attribute]
107pub fn get(_attr: TokenStream, item: TokenStream) -> TokenStream {
108 item
109}
110
111/// Attribute macro to define a DELETE route.
112/// # Arguments
113/// * `path` - the route path.
114/// * `auth` - optional boolean to tell if the route need to be authenticated.
115/// # Example
116/// ```rust
117/// #[delete("/path")]
118/// fn my_delete_route() {
119/// // implementation
120/// }
121/// ```
122#[proc_macro_attribute]
123pub fn delete(_attr: TokenStream, item: TokenStream) -> TokenStream {
124 item
125}
126
127/// Attribute macro to define a router configuration.
128/// # Arguments
129/// * `state` - Application state.
130/// * `routers` - all the routers.
131/// # Example
132/// ```rust
133/// #[router_config(AppState, MyRouter, AnotherRouter, ...)]
134/// struct MyRouter {
135/// // fields
136/// }
137/// ```
138#[proc_macro_attribute]
139pub fn router_config(_attr: TokenStream, item: TokenStream) -> TokenStream {
140 item
141}