himacros/lib.rs
1extern crate proc_macro;
2mod auth_file;
3mod delete;
4mod empty_file;
5mod gen_dist;
6mod get;
7mod head;
8mod post;
9mod put;
10mod route_file;
11mod scope;
12mod utils;
13
14use crate::route_file::route_file_impl;
15use auth_file::auth_file_impl;
16use empty_file::empty_file_impl;
17use gen_dist::gen_dist_impl;
18use proc_macro::TokenStream;
19use scope::scope_impl;
20
21#[cfg(test)]
22mod tests {
23 use super::*;
24 use quote::quote;
25 use syn::{parse_quote, Attribute};
26
27 // cargo test run -- --show-output
28 #[test]
29 fn run() {
30 //let s = r#"#[post(path = "/post", tag = "controllers::test1::post", middleware = {middlewares::auth::auth}, desc = "post")]"#;
31 let attr: Attribute = parse_quote! {
32 //#[post(path = "/post", tag = "controllers::test1::post", middleware = {middlewares::auth::auth,middlewares::auth::auth1}, desc = "post" auth = false)]
33 //#[post(path = "/login", tag = "LoginController.Login", desc = "登录")]
34 #[head(path = "/head", tag = "crate::app::controllers::test::head", middleware = {middlewares::auth::my_auth_middleware}, desc = "head")]
35 };
36 println!("{:?}", attr);
37 println!("{:?}", quote! {#attr});
38 let auth_info = utils::parse_auth_info(quote! {#attr});
39 println!("{:?}", auth_info);
40 }
41}
42
43///
44/// Clear the specified resource file
45///
46/// Examples
47///```text
48/// #[empty_file(filename="./resources/route")]
49/// pub fn empty() {
50/// println!("empty route file")
51/// }
52///```
53///
54#[proc_macro_attribute]
55pub fn empty_file(args: TokenStream, item: TokenStream) -> TokenStream {
56 empty_file_impl(args, item)
57}
58
59///
60/// Configure the path of the routing resource file to generate the routing table
61///
62/// Examples
63///```text
64/// #[route_file(filename="./resources/route")]
65/// pub fn empty() {
66/// println!("empty route file")
67/// }
68///```
69///
70#[proc_macro_attribute]
71pub fn route_file(args: TokenStream, item: TokenStream) -> TokenStream {
72 route_file_impl(args, item)
73}
74
75///
76/// Build and generate a static resource directory
77///
78/// Examples
79///```text
80/// #[gen_dist(zip = "./resources/dist.zip", target_dir = "./resources")]
81/// pub fn dist() {}
82///```
83///
84#[proc_macro_attribute]
85pub fn gen_dist(args: TokenStream, item: TokenStream) -> TokenStream {
86 gen_dist_impl(args, item)
87}
88
89#[proc_macro_attribute]
90#[deprecated]
91pub fn auth_file(args: TokenStream, item: TokenStream) -> TokenStream {
92 auth_file_impl(args, item)
93}
94
95///
96/// Build and generate the scope function
97///
98/// Examples
99///```text
100/// #[scope(file = "./src/app/controllers/test2.rs")]
101/// pub fn routes(cfg: &mut web::ServiceConfig) {
102/// let scope = web::scope("/test2");
103/// cfg.service(scope);
104/// }
105///```
106/// Generate Code
107///```text
108/// pub fn routes(cfg: &mut web::ServiceConfig) {
109/// let scope = web::scope("/test2");
110/// let scope = scope.service(web::resource("/post").app_data(r#"{"method":"post","path":"/post","tag":"src::app::controllers::test2::post","desc":"post","middleware":"","auth":"true"}"#.to_string()).route(web::post().to(post)));
111/// let scope = scope.service(web::resource("/get").app_data(r#"{"method":"get","path":"/get","tag":"src::app::controllers::test2::get","desc":"get","middleware":"","auth":"true"}"#.to_string()).route(web::get().to(get)));
112/// let scope = scope.service(web::resource("/put").app_data(r#"{"method":"put","path":"/put","tag":"src::app::controllers::test2::put","desc":"put","middleware":"","auth":"true"}"#.to_string()).route(web::put().to(put)));
113/// let scope = scope.service(web::resource("/delete").app_data(r#"{"method":"delete","path":"/delete","tag":"src::app::controllers::test2::delete","desc":"delete","middleware":"","auth":"true"}"#.to_string()).route(web::delete().to(delete)));
114/// let scope = scope.service(web::resource("/head").app_data(r#"{"method":"head","path":"/head","tag":"src::app::controllers::test2::head","desc":"head","middleware":"","auth":"true"}"#.to_string()).route(web::head().to(head)));
115/// cfg.service(scope);
116/// }
117///```
118///
119#[proc_macro_attribute]
120pub fn scope(args: TokenStream, item: TokenStream) -> TokenStream {
121 scope_impl(args, item)
122}
123
124///
125/// Sample POST request
126///
127/// Introduction to Parameters
128/// ```text
129/// path: request url path.
130/// tag: The unique label of the function.
131/// middleware: Middleware, with multiple configurations separated by commas.
132/// desc: The description of a function.
133/// auth: Whether it is authenticated or not, Default "true".
134/// ```
135///
136/// Examples
137///```text
138///#[post(path = "/post", desc = "post")]
139///async fn post(req: actix_web::HttpRequest) -> impl Responder {
140/// success_respond_to(&req, Some(String::from("Hey post!")))
141///}
142///```
143///
144// https://rust.biofan.org/crates/attributes.html
145#[proc_macro_attribute]
146pub fn post(_args: TokenStream, item: TokenStream) -> TokenStream {
147 //post_impl(args, item)
148 item
149}
150
151///
152/// Sample GET request
153///
154/// Introduction to Parameters
155/// ```text
156/// path: request url path.
157/// tag: The unique label of the function.
158/// middleware: Middleware, with multiple configurations separated by commas.
159/// desc: The description of a function.
160/// auth: Whether it is authenticated or not, Default "true".
161/// ```
162///
163/// Examples
164///```text
165///#[get(path = "/get", desc = "get")]
166///async fn get(req: actix_web::HttpRequest) -> impl Responder {
167/// success_respond_to(&req, Some(String::from("Hey get!")))
168///}
169///```
170///
171#[proc_macro_attribute]
172pub fn get(_args: TokenStream, item: TokenStream) -> TokenStream {
173 //get_impl(args, item)
174 item
175}
176
177///
178/// Sample PUT request
179///
180/// Introduction to Parameters
181/// ```text
182/// path: request url path.
183/// tag: The unique label of the function.
184/// middleware: Middleware, with multiple configurations separated by commas.
185/// desc: The description of a function.
186/// auth: Whether it is authenticated or not, Default "true".
187/// ```
188///
189/// Examples
190///```text
191///#[put(path = "/put", desc = "put")]
192///async fn put(req: actix_web::HttpRequest) -> impl Responder {
193/// success_respond_to(&req, Some(String::from("Hey put!")))
194///}
195///```
196///
197#[proc_macro_attribute]
198pub fn put(_args: TokenStream, item: TokenStream) -> TokenStream {
199 //put_impl(args, item)
200 item
201}
202
203///
204/// Sample DELETE request
205///
206/// Introduction to Parameters
207/// ```text
208/// path: request url path.
209/// tag: The unique label of the function.
210/// middleware: Middleware, with multiple configurations separated by commas.
211/// desc: The description of a function.
212/// auth: Whether it is authenticated or not, Default "true".
213/// ```
214///
215/// Examples
216///```text
217///#[delete(path = "/delete", desc = "delete")]
218///async fn delete(req: actix_web::HttpRequest) -> impl Responder {
219/// success_respond_to(&req, Some(String::from("Hey delete!")))
220///}
221///```
222///
223#[proc_macro_attribute]
224pub fn delete(_args: TokenStream, item: TokenStream) -> TokenStream {
225 //delete_impl(args, item)
226 item
227}
228
229///
230/// Sample HEAD request
231///
232/// Introduction to Parameters
233/// ```text
234/// path: request url path.
235/// tag: The unique label of the function.
236/// middleware: Middleware, with multiple configurations separated by commas.
237/// desc: The description of a function.
238/// auth: Whether it is authenticated or not, Default "true".
239/// ```
240///
241/// Examples
242///```text
243///#[head(path = "/delete", desc = "head")]
244///async fn head(req: actix_web::HttpRequest) -> impl Responder {
245/// success_respond_to(&req, Some(String::from("Hey head!")))
246///}
247///```
248///
249#[proc_macro_attribute]
250pub fn head(_args: TokenStream, item: TokenStream) -> TokenStream {
251 //head_impl(args, item)
252 item
253}