remove useless ResourceType

this is dead code from back when .to and .to_async were different ways to add a service
This commit is contained in:
Rob Ede 2022-07-04 05:05:34 +01:00
parent df25c254a5
commit 4e8eb74600
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
1 changed files with 14 additions and 68 deletions

View File

@ -3,21 +3,9 @@ use std::{collections::HashSet, convert::TryFrom};
use actix_router::ResourceDef; use actix_router::ResourceDef;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2}; use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{format_ident, quote, ToTokens, TokenStreamExt}; use quote::{quote, ToTokens, TokenStreamExt};
use syn::{parse_macro_input, AttributeArgs, Ident, LitStr, Meta, NestedMeta, Path}; use syn::{parse_macro_input, AttributeArgs, Ident, LitStr, Meta, NestedMeta, Path};
enum ResourceType {
Async,
Sync,
}
impl ToTokens for ResourceType {
fn to_tokens(&self, stream: &mut TokenStream2) {
let ident = format_ident!("to");
stream.append(ident);
}
}
macro_rules! method_type { macro_rules! method_type {
( (
$($variant:ident, $upper:ident, $lower:ident,)+ $($variant:ident, $upper:ident, $lower:ident,)+
@ -213,35 +201,10 @@ pub struct Route {
/// AST of the handler function being annotated. /// AST of the handler function being annotated.
ast: syn::ItemFn, ast: syn::ItemFn,
/// TODO: remove
resource_type: ResourceType,
/// The doc comment attributes to copy to generated struct, if any. /// The doc comment attributes to copy to generated struct, if any.
doc_attributes: Vec<syn::Attribute>, doc_attributes: Vec<syn::Attribute>,
} }
fn guess_resource_type(typ: &syn::Type) -> ResourceType {
let mut guess = ResourceType::Sync;
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;
}
}
}
}
}
guess
}
impl Route { impl Route {
pub fn new( pub fn new(
args: AttributeArgs, args: AttributeArgs,
@ -268,25 +231,17 @@ impl Route {
)); ));
} }
let resource_type = if ast.sig.asyncness.is_some() { if matches!(ast.sig.output, syn::ReturnType::Default) {
ResourceType::Async
} else {
match ast.sig.output {
syn::ReturnType::Default => {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(
ast, ast,
"Function has no return type. Cannot be used as handler", "Function has no return type. Cannot be used as handler",
)); ));
} }
syn::ReturnType::Type(_, ref typ) => guess_resource_type(typ.as_ref()),
}
};
Ok(Self { Ok(Self {
name, name,
args: vec![args], args: vec![args],
ast, ast,
resource_type,
doc_attributes, doc_attributes,
}) })
} }
@ -303,25 +258,17 @@ impl Route {
.cloned() .cloned()
.collect(); .collect();
let resource_type = if ast.sig.asyncness.is_some() { if matches!(ast.sig.output, syn::ReturnType::Default) {
ResourceType::Async
} else {
match ast.sig.output {
syn::ReturnType::Default => {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(
ast, ast,
"Function has no return type. Cannot be used as handler", "Function has no return type. Cannot be used as handler",
)); ));
} }
syn::ReturnType::Type(_, ref typ) => guess_resource_type(typ.as_ref()),
}
};
Ok(Self { Ok(Self {
name, name,
args, args,
ast, ast,
resource_type,
doc_attributes, doc_attributes,
}) })
} }
@ -333,7 +280,6 @@ impl ToTokens for Route {
name, name,
ast, ast,
args, args,
resource_type,
doc_attributes, doc_attributes,
} = self; } = self;
@ -378,7 +324,7 @@ impl ToTokens for Route {
#method_guards #method_guards
#(.guard(::actix_web::guard::fn_guard(#guards)))* #(.guard(::actix_web::guard::fn_guard(#guards)))*
#(.wrap(#wrappers))* #(.wrap(#wrappers))*
.#resource_type(#name); .to(#name);
::actix_web::dev::HttpServiceFactory::register(__resource, __config); ::actix_web::dev::HttpServiceFactory::register(__resource, __config);
} }