mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-26 23:17:42 +02:00
add resource middleware on actix-web-codegen (#1467)
Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
This commit is contained in:
committed by
GitHub
parent
b521e9b221
commit
9164ed1f0c
@ -21,6 +21,7 @@
|
||||
//!
|
||||
//! - `"path"` - Raw literal string with path for which to register handle. Mandatory.
|
||||
//! - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
||||
//! - `wrap="Middleware"` - Registers a resource middleware.
|
||||
//!
|
||||
//! ## Notes
|
||||
//!
|
||||
@ -54,6 +55,7 @@ use proc_macro::TokenStream;
|
||||
///
|
||||
/// - `"path"` - Raw literal string with path for which to register handler. Mandatory.
|
||||
/// - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
|
||||
/// - `wrap="Middleware"` - Registers a resource middleware.
|
||||
#[proc_macro_attribute]
|
||||
pub fn get(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
route::generate(args, input, route::GuardType::Get)
|
||||
|
@ -56,12 +56,14 @@ impl ToTokens for GuardType {
|
||||
struct Args {
|
||||
path: syn::LitStr,
|
||||
guards: Vec<Ident>,
|
||||
wrappers: Vec<syn::Type>,
|
||||
}
|
||||
|
||||
impl Args {
|
||||
fn new(args: AttributeArgs) -> syn::Result<Self> {
|
||||
let mut path = None;
|
||||
let mut guards = Vec::new();
|
||||
let mut wrappers = Vec::new();
|
||||
for arg in args {
|
||||
match arg {
|
||||
NestedMeta::Lit(syn::Lit::Str(lit)) => match path {
|
||||
@ -85,10 +87,19 @@ impl Args {
|
||||
"Attribute guard expects literal string!",
|
||||
));
|
||||
}
|
||||
} else if nv.path.is_ident("wrap") {
|
||||
if let syn::Lit::Str(lit) = nv.lit {
|
||||
wrappers.push(lit.parse()?);
|
||||
} else {
|
||||
return Err(syn::Error::new_spanned(
|
||||
nv.lit,
|
||||
"Attribute wrap expects type",
|
||||
));
|
||||
}
|
||||
} else {
|
||||
return Err(syn::Error::new_spanned(
|
||||
nv.path,
|
||||
"Unknown attribute key is specified. Allowed: guard.",
|
||||
"Unknown attribute key is specified. Allowed: guard and wrap",
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -100,6 +111,7 @@ impl Args {
|
||||
Ok(Args {
|
||||
path: path.unwrap(),
|
||||
guards,
|
||||
wrappers,
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -184,7 +196,7 @@ impl ToTokens for Route {
|
||||
name,
|
||||
guard,
|
||||
ast,
|
||||
args: Args { path, guards },
|
||||
args: Args { path, guards, wrappers },
|
||||
resource_type,
|
||||
} = self;
|
||||
let resource_name = name.to_string();
|
||||
@ -199,6 +211,7 @@ impl ToTokens for Route {
|
||||
.name(#resource_name)
|
||||
.guard(actix_web::guard::#guard())
|
||||
#(.guard(actix_web::guard::fn_guard(#guards)))*
|
||||
#(.wrap(#wrappers))*
|
||||
.#resource_type(#name);
|
||||
|
||||
actix_web::dev::HttpServiceFactory::register(__resource, __config)
|
||||
|
Reference in New Issue
Block a user