2021-12-08 07:09:56 +01:00
|
|
|
use std::{collections::HashSet, convert::TryFrom};
|
2020-09-16 23:37:41 +02:00
|
|
|
|
2021-08-30 22:50:40 +02:00
|
|
|
use actix_router::ResourceDef;
|
2019-03-31 09:23:15 +02:00
|
|
|
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};
|
2021-06-26 16:33:43 +02:00
|
|
|
use syn::{parse_macro_input, AttributeArgs, Ident, LitStr, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 23:42:51 +02:00
|
|
|
macro_rules! method_type {
|
|
|
|
(
|
|
|
|
$($variant:ident, $upper:ident,)+
|
|
|
|
) => {
|
|
|
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub enum MethodType {
|
|
|
|
$(
|
|
|
|
$variant,
|
|
|
|
)+
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MethodType {
|
|
|
|
fn as_str(&self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
$(Self::$variant => stringify!($variant),)+
|
|
|
|
}
|
|
|
|
}
|
2019-03-31 09:23:15 +02:00
|
|
|
|
2020-09-22 23:42:51 +02:00
|
|
|
fn parse(method: &str) -> Result<Self, String> {
|
|
|
|
match method {
|
|
|
|
$(stringify!($upper) => Ok(Self::$variant),)+
|
|
|
|
_ => Err(format!("Unexpected HTTP method: `{}`", method)),
|
|
|
|
}
|
|
|
|
}
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
2020-09-22 23:42:51 +02:00
|
|
|
};
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-22 23:42:51 +02:00
|
|
|
method_type! {
|
|
|
|
Get, GET,
|
|
|
|
Post, POST,
|
|
|
|
Put, PUT,
|
|
|
|
Delete, DELETE,
|
|
|
|
Head, HEAD,
|
|
|
|
Connect, CONNECT,
|
|
|
|
Options, OPTIONS,
|
|
|
|
Trace, TRACE,
|
|
|
|
Patch, PATCH,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTokens for MethodType {
|
2019-10-14 17:34:17 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-22 23:42:51 +02:00
|
|
|
impl TryFrom<&syn::LitStr> for MethodType {
|
2020-09-16 23:37:41 +02:00
|
|
|
type Error = syn::Error;
|
|
|
|
|
|
|
|
fn try_from(value: &syn::LitStr) -> Result<Self, Self::Error> {
|
2020-09-22 23:42:51 +02:00
|
|
|
Self::parse(value.value().as_str())
|
|
|
|
.map_err(|message| syn::Error::new_spanned(value, message))
|
2020-09-16 23:37:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
struct Args {
|
|
|
|
path: syn::LitStr,
|
2021-03-04 13:38:47 +01:00
|
|
|
resource_name: Option<syn::LitStr>,
|
2019-10-14 17:34:17 +02:00
|
|
|
guards: Vec<Ident>,
|
2020-05-07 11:31:12 +02:00
|
|
|
wrappers: Vec<syn::Type>,
|
2020-09-22 23:42:51 +02:00
|
|
|
methods: HashSet<MethodType>,
|
2019-10-14 17:34:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Args {
|
2020-09-22 23:42:51 +02:00
|
|
|
fn new(args: AttributeArgs, method: Option<MethodType>) -> syn::Result<Self> {
|
2019-10-14 17:34:17 +02:00
|
|
|
let mut path = None;
|
2021-03-04 13:38:47 +01:00
|
|
|
let mut resource_name = None;
|
2019-10-14 17:34:17 +02:00
|
|
|
let mut guards = Vec::new();
|
2020-05-07 11:31:12 +02:00
|
|
|
let mut wrappers = Vec::new();
|
2020-09-16 23:37:41 +02:00
|
|
|
let mut methods = HashSet::new();
|
2020-09-22 23:42:51 +02:00
|
|
|
|
|
|
|
let is_route_macro = method.is_none();
|
|
|
|
if let Some(method) = method {
|
|
|
|
methods.insert(method);
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
for arg in args {
|
|
|
|
match arg {
|
|
|
|
NestedMeta::Lit(syn::Lit::Str(lit)) => match path {
|
|
|
|
None => {
|
2021-08-30 22:50:40 +02:00
|
|
|
let _ = ResourceDef::new(lit.value());
|
2019-10-14 17:34:17 +02:00
|
|
|
path = Some(lit);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
lit,
|
|
|
|
"Multiple paths specified! Should be only one!",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
NestedMeta::Meta(syn::Meta::NameValue(nv)) => {
|
2021-03-04 13:38:47 +01:00
|
|
|
if nv.path.is_ident("name") {
|
|
|
|
if let syn::Lit::Str(lit) = nv.lit {
|
|
|
|
resource_name = Some(lit);
|
|
|
|
} else {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
nv.lit,
|
|
|
|
"Attribute name expects literal string!",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else if nv.path.is_ident("guard") {
|
2019-10-14 17:34:17 +02:00
|
|
|
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",
|
|
|
|
));
|
|
|
|
}
|
2020-09-16 23:37:41 +02:00
|
|
|
} else if nv.path.is_ident("method") {
|
2020-09-22 23:42:51 +02:00
|
|
|
if !is_route_macro {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
&nv,
|
|
|
|
"HTTP method forbidden here. To handle multiple methods, use `route` instead",
|
|
|
|
));
|
|
|
|
} else if let syn::Lit::Str(ref lit) = nv.lit {
|
|
|
|
let method = MethodType::try_from(lit)?;
|
|
|
|
if !methods.insert(method) {
|
2020-09-16 23:37:41 +02:00
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
&nv.lit,
|
|
|
|
&format!(
|
2020-09-22 23:42:51 +02:00
|
|
|
"HTTP method defined more than once: `{}`",
|
2020-09-16 23:37:41 +02:00
|
|
|
lit.value()
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
nv.lit,
|
|
|
|
"Attribute method expects literal string!",
|
|
|
|
));
|
|
|
|
}
|
2019-10-14 17:34:17 +02:00
|
|
|
} else {
|
|
|
|
return Err(syn::Error::new_spanned(
|
|
|
|
nv.path,
|
2020-09-16 23:37:41 +02:00
|
|
|
"Unknown attribute key is specified. Allowed: guard, method 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(),
|
2021-03-04 13:38:47 +01:00
|
|
|
resource_name,
|
2019-10-14 17:34:17 +02:00
|
|
|
guards,
|
2020-05-07 11:31:12 +02:00
|
|
|
wrappers,
|
2020-09-16 23:37:41 +02:00
|
|
|
methods,
|
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,
|
2021-02-24 13:26:56 +01:00
|
|
|
|
|
|
|
/// The doc comment attributes to copy to generated struct, if any.
|
|
|
|
doc_attributes: Vec<syn::Attribute>,
|
2019-10-14 17:34:17 +02:00
|
|
|
}
|
|
|
|
|
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,
|
2021-10-19 18:30:32 +02:00
|
|
|
ast: syn::ItemFn,
|
2020-09-22 23:42:51 +02:00
|
|
|
method: Option<MethodType>,
|
2019-10-14 17:34:17 +02:00
|
|
|
) -> 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!(
|
2020-09-22 23:42:51 +02:00
|
|
|
r#"invalid service definition, expected #[{}("<some path>")]"#,
|
|
|
|
method
|
2021-06-26 16:33:43 +02:00
|
|
|
.map_or("route", |it| it.as_str())
|
2020-09-22 23:42:51 +02:00
|
|
|
.to_ascii_lowercase()
|
2019-10-14 17:34:17 +02:00
|
|
|
),
|
|
|
|
));
|
2019-03-31 09:23:15 +02:00
|
|
|
}
|
2021-10-19 18:30:32 +02:00
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
let name = ast.sig.ident.clone();
|
2019-03-31 09:23:15 +02:00
|
|
|
|
2021-10-19 18:30:32 +02:00
|
|
|
// Try and pull out the doc comments so that we can reapply them to the generated struct.
|
|
|
|
// Note that multi line doc comments are converted to multiple doc attributes.
|
2021-02-24 13:26:56 +01:00
|
|
|
let doc_attributes = ast
|
|
|
|
.attrs
|
|
|
|
.iter()
|
|
|
|
.filter(|attr| attr.path.is_ident("doc"))
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
|
2020-09-22 23:42:51 +02:00
|
|
|
let args = Args::new(args, method)?;
|
|
|
|
if args.methods.is_empty() {
|
2020-09-16 23:37:41 +02:00
|
|
|
return Err(syn::Error::new(
|
|
|
|
Span::call_site(),
|
|
|
|
"The #[route(..)] macro requires at least one `method` attribute",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
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,
|
2021-02-24 13:26:56 +01:00
|
|
|
doc_attributes,
|
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,
|
|
|
|
ast,
|
2020-07-22 01:28:33 +02:00
|
|
|
args:
|
|
|
|
Args {
|
|
|
|
path,
|
2021-03-04 13:38:47 +01:00
|
|
|
resource_name,
|
2020-07-22 01:28:33 +02:00
|
|
|
guards,
|
|
|
|
wrappers,
|
2020-09-16 23:37:41 +02:00
|
|
|
methods,
|
2020-07-22 01:28:33 +02:00
|
|
|
},
|
2020-03-14 23:23:28 +01:00
|
|
|
resource_type,
|
2021-02-24 13:26:56 +01:00
|
|
|
doc_attributes,
|
2020-03-14 23:23:28 +01:00
|
|
|
} = self;
|
2021-03-04 13:38:47 +01:00
|
|
|
let resource_name = resource_name
|
|
|
|
.as_ref()
|
2021-06-26 16:33:43 +02:00
|
|
|
.map_or_else(|| name.to_string(), LitStr::value);
|
2020-09-22 23:42:51 +02:00
|
|
|
let method_guards = {
|
|
|
|
let mut others = methods.iter();
|
2020-09-16 23:37:41 +02:00
|
|
|
// unwrapping since length is checked to be at least one
|
2020-09-22 23:42:51 +02:00
|
|
|
let first = others.next().unwrap();
|
2020-09-16 23:37:41 +02:00
|
|
|
|
2020-09-22 23:42:51 +02:00
|
|
|
if methods.len() > 1 {
|
|
|
|
quote! {
|
|
|
|
.guard(
|
|
|
|
actix_web::guard::Any(actix_web::guard::#first())
|
|
|
|
#(.or(actix_web::guard::#others()))*
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote! {
|
|
|
|
.guard(actix_web::guard::#first())
|
|
|
|
}
|
2020-09-16 23:37:41 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-14 17:34:17 +02:00
|
|
|
let stream = quote! {
|
2021-02-24 13:26:56 +01:00
|
|
|
#(#doc_attributes)*
|
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)
|
2020-09-16 23:37:41 +02:00
|
|
|
#method_guards
|
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
|
|
|
|
2020-09-22 23:42:51 +02:00
|
|
|
pub(crate) fn with_method(
|
|
|
|
method: Option<MethodType>,
|
2020-03-19 20:40:42 +01:00
|
|
|
args: TokenStream,
|
|
|
|
input: TokenStream,
|
|
|
|
) -> TokenStream {
|
|
|
|
let args = parse_macro_input!(args as syn::AttributeArgs);
|
2021-10-19 18:30:32 +02:00
|
|
|
|
|
|
|
let ast = match syn::parse::<syn::ItemFn>(input.clone()) {
|
|
|
|
Ok(ast) => ast,
|
|
|
|
// on parse error, make IDEs happy; see fn docs
|
|
|
|
Err(err) => return input_and_compile_error(input, err),
|
|
|
|
};
|
|
|
|
|
|
|
|
match Route::new(args, ast, method) {
|
2020-03-19 20:40:42 +01:00
|
|
|
Ok(route) => route.into_token_stream().into(),
|
2021-10-19 18:30:32 +02:00
|
|
|
// on macro related error, make IDEs happy; see fn docs
|
2021-10-14 19:06:31 +02:00
|
|
|
Err(err) => input_and_compile_error(input, err),
|
2020-03-19 20:40:42 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-14 19:06:31 +02:00
|
|
|
|
|
|
|
/// Converts the error to a token stream and appends it to the original input.
|
|
|
|
///
|
|
|
|
/// Returning the original input in addition to the error is good for IDEs which can gracefully
|
|
|
|
/// recover and show more precise errors within the macro body.
|
|
|
|
///
|
|
|
|
/// See <https://github.com/rust-analyzer/rust-analyzer/issues/10468> for more info.
|
|
|
|
fn input_and_compile_error(mut item: TokenStream, err: syn::Error) -> TokenStream {
|
|
|
|
let compile_err = TokenStream::from(err.to_compile_error());
|
|
|
|
item.extend(compile_err);
|
2021-10-19 02:32:58 +02:00
|
|
|
item
|
2021-10-14 19:06:31 +02:00
|
|
|
}
|