hirust-macros 0.1.18

A Rust Macros
Documentation
extern crate proc_macro;
mod auth_file;
mod delete;
mod empty_file;
mod gen_dist;
mod get;
mod head;
mod post;
mod put;
mod route_file;
mod scope;
mod utils;

use crate::route_file::route_file_impl;
use auth_file::auth_file_impl;
use empty_file::empty_file_impl;
use gen_dist::gen_dist_impl;
use proc_macro::TokenStream;
use scope::scope_impl;

#[cfg(test)]
mod tests {
    use super::*;
    use quote::quote;
    use syn::{parse_quote, Attribute};

    // cargo test run -- --show-output
    #[test]
    fn run() {
        //let s = r#"#[post(path = "/post", tag = "controllers::test1::post", middleware = {middlewares::auth::auth}, desc = "post")]"#;
        let attr: Attribute = parse_quote! {
            //#[post(path = "/post", tag = "controllers::test1::post", middleware = {middlewares::auth::auth,middlewares::auth::auth1}, desc = "post" auth = false)]
            //#[post(path = "/login", tag = "LoginController.Login", desc = "登录")]
            #[head(path = "/head", tag = "crate::app::controllers::test::head", middleware = {middlewares::auth::my_auth_middleware}, desc = "head")]
        };
        println!("{:?}", attr);
        println!("{:?}", quote! {#attr});
        let auth_info = utils::parse_auth_info(quote! {#attr});
        println!("{:?}", auth_info);
    }
}

///
/// Clear the specified resource file
///
/// Examples
///```text
/// #[empty_file(filename="./resources/route")]
/// pub fn empty() {
///     println!("empty route file")
/// }
///```
///
#[proc_macro_attribute]
pub fn empty_file(args: TokenStream, item: TokenStream) -> TokenStream {
    empty_file_impl(args, item)
}

///
/// Configure the path of the routing resource file to generate the routing table
///
/// Examples
///```text
/// #[route_file(filename="./resources/route")]
/// pub fn empty() {
///     println!("empty route file")
/// }
///```
///
#[proc_macro_attribute]
pub fn route_file(args: TokenStream, item: TokenStream) -> TokenStream {
    route_file_impl(args, item)
}

///
/// Build and generate a static resource directory
///
/// Examples
///```text
/// #[gen_dist(zip = "./resources/dist.zip", target_dir = "./resources")]
/// pub fn dist() {}
///```
///
#[proc_macro_attribute]
pub fn gen_dist(args: TokenStream, item: TokenStream) -> TokenStream {
    gen_dist_impl(args, item)
}

#[proc_macro_attribute]
#[deprecated]
pub fn auth_file(args: TokenStream, item: TokenStream) -> TokenStream {
    auth_file_impl(args, item)
}

///
/// Build and generate the scope function
///
/// Examples
///```text
/// #[scope(file = "./src/app/controllers/test2.rs")]
/// pub fn routes(cfg: &mut web::ServiceConfig) {
///    let scope = web::scope("/test2");
///    cfg.service(scope);
/// }
///```
/// Generate Code
///```text
/// pub fn routes(cfg: &mut web::ServiceConfig) {
///    let scope = web::scope("/test2");
///    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)));
///    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)));
///    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)));
///    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)));
///    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)));
///    cfg.service(scope);
/// }
///```
///
#[proc_macro_attribute]
pub fn scope(args: TokenStream, item: TokenStream) -> TokenStream {
    scope_impl(args, item)
}

///
/// Sample POST request
///
/// Introduction to Parameters
/// ```text
/// path: request url path.
/// tag: The unique label of the function.
/// middleware: Middleware, with multiple configurations separated by commas.
/// desc: The description of a function.
/// auth: Whether it is authenticated or not, Default "true".
/// ```
///
/// Examples
///```text
///#[post(path = "/post", desc = "post")]
///async fn post(req: actix_web::HttpRequest) -> impl Responder {
///    success_respond_to(&req, Some(String::from("Hey post!")))
///}
///```
///
// https://rust.biofan.org/crates/attributes.html
#[proc_macro_attribute]
pub fn post(_args: TokenStream, item: TokenStream) -> TokenStream {
    //post_impl(args, item)
    item
}

///
/// Sample GET request
///
/// Introduction to Parameters
/// ```text
/// path: request url path.
/// tag: The unique label of the function.
/// middleware: Middleware, with multiple configurations separated by commas.
/// desc: The description of a function.
/// auth: Whether it is authenticated or not, Default "true".
/// ```
///
/// Examples
///```text
///#[get(path = "/get", desc = "get")]
///async fn get(req: actix_web::HttpRequest) -> impl Responder {
///    success_respond_to(&req, Some(String::from("Hey get!")))
///}
///```
///
#[proc_macro_attribute]
pub fn get(_args: TokenStream, item: TokenStream) -> TokenStream {
    //get_impl(args, item)
    item
}

///
/// Sample PUT request
///
/// Introduction to Parameters
/// ```text
/// path: request url path.
/// tag: The unique label of the function.
/// middleware: Middleware, with multiple configurations separated by commas.
/// desc: The description of a function.
/// auth: Whether it is authenticated or not, Default "true".
/// ```
///
/// Examples
///```text
///#[put(path = "/put", desc = "put")]
///async fn put(req: actix_web::HttpRequest) -> impl Responder {
///    success_respond_to(&req, Some(String::from("Hey put!")))
///}
///```
///
#[proc_macro_attribute]
pub fn put(_args: TokenStream, item: TokenStream) -> TokenStream {
    //put_impl(args, item)
    item
}

///
/// Sample DELETE request
///
/// Introduction to Parameters
/// ```text
/// path: request url path.
/// tag: The unique label of the function.
/// middleware: Middleware, with multiple configurations separated by commas.
/// desc: The description of a function.
/// auth: Whether it is authenticated or not, Default "true".
/// ```
///
/// Examples
///```text
///#[delete(path = "/delete", desc = "delete")]
///async fn delete(req: actix_web::HttpRequest) -> impl Responder {
///    success_respond_to(&req, Some(String::from("Hey delete!")))
///}
///```
///
#[proc_macro_attribute]
pub fn delete(_args: TokenStream, item: TokenStream) -> TokenStream {
    //delete_impl(args, item)
    item
}

///
/// Sample HEAD request
///
/// Introduction to Parameters
/// ```text
/// path: request url path.
/// tag: The unique label of the function.
/// middleware: Middleware, with multiple configurations separated by commas.
/// desc: The description of a function.
/// auth: Whether it is authenticated or not, Default "true".
/// ```
///
/// Examples
///```text
///#[head(path = "/delete", desc = "head")]
///async fn head(req: actix_web::HttpRequest) -> impl Responder {
///    success_respond_to(&req, Some(String::from("Hey head!")))
///}
///```
///
#[proc_macro_attribute]
pub fn head(_args: TokenStream, item: TokenStream) -> TokenStream {
    //head_impl(args, item)
    item
}