Repository: actix/actix-web
File: actix-web-codegen/src/route.rs (Rust)

Code before:
            name,
            args,
            ast,
            resource_type,
            guard,
        })
    }
}

impl ToTokens for Route {
    fn to_tokens(&self, output: &mut TokenStream2) {
        let Self {
            name,
            guard,
            ast,
            args:
                Args {
                    path,
                    guards,
                    wrappers,
                    methods,
                },
            resource_type,
        } = self;
        let resource_name = name.to_string();
        let stream = if guard != &GuardType::Multi {
            quote! {
                #[allow(non_camel_case_types, missing_docs)]
                pub struct #name;

                impl actix_web::dev::HttpServiceFactory for #name {
                    fn register(self, __config: &mut actix_web::dev::AppService) {
                        #ast
                        let __resource = actix_web::Resource::new(#path)
                            .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)
                    }
                }
            }
        } else {
            quote! {
                #[allow(non_camel_case_types, missing_docs)]
                pub struct #name;

                impl actix_web::dev::HttpServiceFactory for #name {
                    fn register(self, __config: &mut actix_web::dev::AppService) {

Review comment (suggestion):
`if` branches differ in one line only. I suggest generating only that line inside `if`, and then use it as a part of the shared `quote`, like:

```rust
let guard = if guard != &GuardType::Multi {
    // ...
} else {
    // ...
};

let stream = quote! { ... };
```

Code after:
        let resource_type = if ast.sig.asyncness.is_some() {
            ResourceType::Async
        } else {
            match ast.sig.output {
                syn::ReturnType::Default => {
                    return Err(syn::Error::new_spanned(
                        ast,
                        "Function has no return type. Cannot be used as handler",
                    ));
                }
                syn::ReturnType::Type(_, ref typ) => guess_resource_type(typ.as_ref()),
            }
        };

        Ok(Self {
            name,
            args,
            ast,
            resource_type,
            guard,
        })
    }
}

impl ToTokens for Route {
    fn to_tokens(&self, output: &mut TokenStream2) {
        let Self {
            name,
            guard,
            ast,
            args:
                Args {
                    path,
                    guards,
                    wrappers,
                    methods,
                },
            resource_type,
        } = self;
        let resource_name = name.to_string();
        let mut methods = methods.iter();

        let method_guards = if *guard == GuardType::Multi {
            // unwrapping since length is checked to be at least one
            let first = methods.next().unwrap();

            quote! {
                .guard(
                    actix_web::guard::Any(actix_web::guard::#first())
                        #(.or(actix_web::guard::#methods()))*
                )