2019-03-31 09:23:15 +02:00
|
|
|
extern crate proc_macro;
|
|
|
|
|
|
|
|
use proc_macro::TokenStream;
|
2019-10-14 17:34:17 +02:00
|
|
|
use proc_macro2::{Span, TokenStream as TokenStream2};
|
2020-03-14 23:23:28 +01:00
|
|
|
use quote::{format_ident, quote, ToTokens, TokenStreamExt};
|
2020-07-22 01:28:33 +02:00
|
|
|
use syn::{parse_macro_input, AttributeArgs, Ident, NestedMeta};
|
2019-03-31 09:23:15 +02:00
|
|
|
|
|
|
|
enum ResourceType {
|
|
|
|
Async,
|
|
|
|
Sync,
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
impl ToTokens for ResourceType {
|
|
|
|
fn to_tokens(&self, stream: &mut TokenStream2) {
|
2020-03-14 23:23:28 +01:00
|
|
|
let ident = format_ident!("to");
|
2019-10-14 17:34:17 +02:00
|
|
|
stream.append(ident);
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum GuardType {
|
|
|
|
Get,
|
|
|
|
Post,
|
|
|
|
Put,
|
|
|
|
Delete,
|
2019-06-04 18:30:43 +02:00
|
|
|
Head,
|
|
|
|
Connect,
|
|
|
|
Options,
|
|
|
|
Trace,
|
2019-06-05 04:43:13 +02:00
|
|
|
Patch,
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
impl GuardType {
|
|
|
|
fn as_str(&self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
GuardType::Get => "Get",
|
|
|
|
GuardType::Post => "Post",
|
|
|
|
GuardType::Put => "Put",
|
|
|
|
GuardType::Delete => "Delete",
|
|
|
|
GuardType::Head => "Head",
|
|
|
|
GuardType::Connect => "Connect",
|
|
|
|
GuardType::Options => "Options",
|
|
|
|
GuardType::Trace => "Trace",
|
|
|
|
GuardType::Patch => "Patch",
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
impl ToTokens for GuardType {
|
|
|
|
fn to_tokens(&self, stream: &mut TokenStream2) {
|
2020-03-14 23:23:28 +01:00
|
|
|
let ident = Ident::new(self.as_str(), Span::call_site());
|
2019-10-14 17:34:17 +02:00
|
|
|
stream.append(ident);
|
|
|
|
}
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
struct Args {
|
|
|
|
path: syn::LitStr,
|
|
|
|
guards: Vec<Ident>,
|
2020-05-07 11:31:12 +02:00
|
|
|
wrappers: Vec<syn::Type>,
|
2019-10-14 17:34:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Args {
|
|
|
|
fn new(args: AttributeArgs) -> syn::Result<Self> {
|
|
|
|
let mut path = None;
|
|
|
|
let mut guards = Vec::new();
|
2020-05-07 11:31:12 +02:00
|
|
|
let mut wrappers = Vec::new();
|
2019-10-14 17:34:17 +02:00
|
|
|
for arg in args {
|
|
|
|
match arg {
|
|
|
|
NestedMeta::Lit(syn::Lit::Str(lit)) => match path {
|
|
|
|
None => {
|
|
|
|
path = Some(lit);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
lit,
|
|
|
|
"Multiple paths specified! Should be only one!",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
NestedMeta::Meta(syn::Meta::NameValue(nv)) => {
|
|
|
|
if nv.path.is_ident("guard") {
|
|
|
|
if let syn::Lit::Str(lit) = nv.lit {
|
|
|
|
guards.push(Ident::new(&lit.value(), Span::call_site()));
|
|
|
|
} else {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
nv.lit,
|
|
|
|
"Attribute guard expects literal string!",
|
|
|
|
));
|
|
|
|
}
|
2020-05-07 11:31:12 +02:00
|
|
|
} 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",
|
|
|
|
));
|
|
|
|
}
|
2019-10-14 17:34:17 +02:00
|
|
|
} else {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
nv.path,
|
2020-05-07 11:31:12 +02:00
|
|
|
"Unknown attribute key is specified. Allowed: guard and wrap",
|
2019-10-14 17:34:17 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arg => {
|
2020-03-14 23:23:28 +01:00
|
|
|
return Err(syn::Error::new_spanned(arg, "Unknown attribute."));
|
2019-10-14 17:34:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Args {
|
|
|
|
path: path.unwrap(),
|
|
|
|
guards,
|
2020-05-07 11:31:12 +02:00
|
|
|
wrappers,
|
2019-10-14 17:34:17 +02:00
|
|
|
})
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
pub struct Route {
|
|
|
|
name: syn::Ident,
|
|
|
|
args: Args,
|
|
|
|
ast: syn::ItemFn,
|
|
|
|
resource_type: ResourceType,
|
|
|
|
guard: GuardType,
|
|
|
|
}
|
|
|
|
|
2019-03-31 09:23:15 +02:00
|
|
|
fn guess_resource_type(typ: &syn::Type) -> ResourceType {
|
|
|
|
let mut guess = ResourceType::Sync;
|
|
|
|
|
2019-07-17 11:08:30 +02:00
|
|
|
if let syn::Type::ImplTrait(typ) = typ {
|
|
|
|
for bound in typ.bounds.iter() {
|
|
|
|
if let syn::TypeParamBound::Trait(bound) = bound {
|
|
|
|
for bound in bound.path.segments.iter() {
|
|
|
|
if bound.ident == "Future" {
|
|
|
|
guess = ResourceType::Async;
|
|
|
|
break;
|
|
|
|
} else if bound.ident == "Responder" {
|
|
|
|
guess = ResourceType::Sync;
|
|
|
|
break;
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
2019-04-10 21:24:17 +02:00
|
|
|
}
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
2019-04-10 21:24:17 +02:00
|
|
|
}
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
guess
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
impl Route {
|
|
|
|
pub fn new(
|
|
|
|
args: AttributeArgs,
|
|
|
|
input: TokenStream,
|
|
|
|
guard: GuardType,
|
|
|
|
) -> syn::Result<Self> {
|
2019-03-31 09:23:15 +02:00
|
|
|
if args.is_empty() {
|
2019-10-14 17:34:17 +02:00
|
|
|
return Err(syn::Error::new(
|
|
|
|
Span::call_site(),
|
|
|
|
format!(
|
|
|
|
r#"invalid server definition, expected #[{}("<some path>")]"#,
|
|
|
|
guard.as_str().to_ascii_lowercase()
|
|
|
|
),
|
|
|
|
));
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
2019-10-14 17:34:17 +02:00
|
|
|
let ast: syn::ItemFn = syn::parse(input)?;
|
|
|
|
let name = ast.sig.ident.clone();
|
2019-03-31 09:23:15 +02:00
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
let args = Args::new(args)?;
|
2019-03-31 09:23:15 +02:00
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
let resource_type = if ast.sig.asyncness.is_some() {
|
2019-03-31 09:23:15 +02:00
|
|
|
ResourceType::Async
|
|
|
|
} else {
|
2019-10-14 17:34:17 +02:00
|
|
|
match ast.sig.output {
|
|
|
|
syn::ReturnType::Default => {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
ast,
|
|
|
|
"Function has no return type. Cannot be used as handler",
|
|
|
|
));
|
|
|
|
}
|
2019-03-31 09:23:15 +02:00
|
|
|
syn::ReturnType::Type(_, ref typ) => guess_resource_type(typ.as_ref()),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
Ok(Self {
|
2019-03-31 09:23:15 +02:00
|
|
|
name,
|
2019-10-14 17:34:17 +02:00
|
|
|
args,
|
2019-03-31 09:23:15 +02:00
|
|
|
ast,
|
|
|
|
resource_type,
|
|
|
|
guard,
|
2019-10-14 17:34:17 +02:00
|
|
|
})
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
2020-03-14 23:23:28 +01:00
|
|
|
}
|
2019-03-31 09:23:15 +02:00
|
|
|
|
2020-03-14 23:23:28 +01:00
|
|
|
impl ToTokens for Route {
|
|
|
|
fn to_tokens(&self, output: &mut TokenStream2) {
|
|
|
|
let Self {
|
|
|
|
name,
|
|
|
|
guard,
|
|
|
|
ast,
|
2020-07-22 01:28:33 +02:00
|
|
|
args:
|
|
|
|
Args {
|
|
|
|
path,
|
|
|
|
guards,
|
|
|
|
wrappers,
|
|
|
|
},
|
2020-03-14 23:23:28 +01:00
|
|
|
resource_type,
|
|
|
|
} = self;
|
2019-11-26 12:40:29 +01:00
|
|
|
let resource_name = name.to_string();
|
2019-10-14 17:34:17 +02:00
|
|
|
let stream = quote! {
|
2020-02-22 10:32:12 +01:00
|
|
|
#[allow(non_camel_case_types, missing_docs)]
|
2019-10-14 17:34:17 +02:00
|
|
|
pub struct #name;
|
|
|
|
|
|
|
|
impl actix_web::dev::HttpServiceFactory for #name {
|
2020-01-25 23:22:40 +01:00
|
|
|
fn register(self, __config: &mut actix_web::dev::AppService) {
|
2019-10-14 17:34:17 +02:00
|
|
|
#ast
|
2020-01-25 23:22:40 +01:00
|
|
|
let __resource = actix_web::Resource::new(#path)
|
2019-11-26 12:40:29 +01:00
|
|
|
.name(#resource_name)
|
2019-10-14 17:34:17 +02:00
|
|
|
.guard(actix_web::guard::#guard())
|
2020-03-14 23:23:28 +01:00
|
|
|
#(.guard(actix_web::guard::fn_guard(#guards)))*
|
2020-05-07 11:31:12 +02:00
|
|
|
#(.wrap(#wrappers))*
|
2019-10-14 17:34:17 +02:00
|
|
|
.#resource_type(#name);
|
|
|
|
|
2020-01-25 23:22:40 +01:00
|
|
|
actix_web::dev::HttpServiceFactory::register(__resource, __config)
|
2019-10-14 17:34:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-03-14 23:23:28 +01:00
|
|
|
|
|
|
|
output.extend(stream);
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-19 20:40:42 +01:00
|
|
|
|
|
|
|
pub(crate) fn generate(
|
|
|
|
args: TokenStream,
|
|
|
|
input: TokenStream,
|
|
|
|
guard: GuardType,
|
|
|
|
) -> TokenStream {
|
|
|
|
let args = parse_macro_input!(args as syn::AttributeArgs);
|
|
|
|
match Route::new(args, input, guard) {
|
|
|
|
Ok(route) => route.into_token_stream().into(),
|
|
|
|
Err(err) => err.to_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|