mirror of https://github.com/fafhrd91/actix-web
code formatting cleanup
This commit is contained in:
parent
efe990dca5
commit
ec4633a911
|
@ -2,6 +2,10 @@
|
|||
|
||||
## Unreleased
|
||||
|
||||
## 4.2.3
|
||||
|
||||
- Add a scope macro that takes a path
|
||||
|
||||
## 4.2.2
|
||||
|
||||
- Fix regression when declaring `wrap` attribute using an expression.
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
use std::collections::HashSet;
|
||||
use std::{collections::HashSet, fmt};
|
||||
|
||||
use actix_router::ResourceDef;
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::{Span, TokenStream as TokenStream2};
|
||||
use quote::{quote, ToTokens, TokenStreamExt};
|
||||
use std::fmt;
|
||||
use syn::{punctuated::Punctuated, Ident, LitStr, Path, Token};
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -570,23 +569,29 @@ impl ScopeItems {
|
|||
for item in items {
|
||||
match item {
|
||||
syn::Item::Fn(ref fun) => {
|
||||
|
||||
for attr in fun.attrs.iter() {
|
||||
for bound in attr.path().segments.iter() {
|
||||
if bound.ident == "get" || bound.ident == "post" || bound.ident == "put" || bound.ident == "head" || bound.ident == "connect" || bound.ident == "options" || bound.ident == "trace" || bound.ident == "patch" || bound.ident == "delete" {
|
||||
if bound.ident == "get"
|
||||
|| bound.ident == "post"
|
||||
|| bound.ident == "put"
|
||||
|| bound.ident == "head"
|
||||
|| bound.ident == "connect"
|
||||
|| bound.ident == "options"
|
||||
|| bound.ident == "trace"
|
||||
|| bound.ident == "patch"
|
||||
|| bound.ident == "delete"
|
||||
{
|
||||
handlers.push(format!("{}", fun.sig.ident));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
handlers,
|
||||
}
|
||||
Self { handlers }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -610,12 +615,14 @@ impl ScopeArgs {
|
|||
|
||||
let mut items = Vec::new();
|
||||
match ast.expr.as_ref() {
|
||||
syn::Expr::Block(expr) => for item in expr.block.stmts.iter() {
|
||||
syn::Expr::Block(expr) => {
|
||||
for item in expr.block.stmts.iter() {
|
||||
match item {
|
||||
syn::Stmt::Item(ref item) => items.push(item.clone()),
|
||||
_ => continue,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
_ => panic!("Scope should containt only code block"),
|
||||
}
|
||||
|
||||
|
@ -641,10 +648,9 @@ impl ScopeArgs {
|
|||
|
||||
match text.parse() {
|
||||
Ok(res) => res,
|
||||
Err(error) => panic!("Error: {:?}\nGenerated code: {}", error, text)
|
||||
Err(error) => panic!("Error: {:?}\nGenerated code: {}", error, text),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl fmt::Display for ScopeArgs {
|
||||
|
@ -655,22 +661,36 @@ impl fmt::Display for ScopeArgs {
|
|||
let module_name = syn::Ident::new(&module_name, ast.ident.span());
|
||||
let ast = match ast.expr.as_ref() {
|
||||
syn::Expr::Block(expr) => quote!(pub mod #module_name #expr),
|
||||
_ => panic!("Unexpect non-block ast in scope macro")
|
||||
_ => panic!("Unexpect non-block ast in scope macro"),
|
||||
};
|
||||
|
||||
writeln!(f, "{}\n", ast)?;
|
||||
writeln!(f, "#[allow(non_camel_case_types)]")?;
|
||||
writeln!(f, "struct {};\n", self.name)?;
|
||||
writeln!(f, "impl actix_web::dev::HttpServiceFactory for {} {{", self.name)?;
|
||||
writeln!(f, " fn register(self, __config: &mut actix_web::dev::AppService) {{")?;
|
||||
write!(f, " let scope = actix_web::Scope::new(\"{}\")", self.path)?;
|
||||
writeln!(
|
||||
f,
|
||||
"impl actix_web::dev::HttpServiceFactory for {} {{",
|
||||
self.name
|
||||
)?;
|
||||
writeln!(
|
||||
f,
|
||||
" fn register(self, __config: &mut actix_web::dev::AppService) {{"
|
||||
)?;
|
||||
write!(
|
||||
f,
|
||||
" let scope = actix_web::Scope::new(\"{}\")",
|
||||
self.path
|
||||
)?;
|
||||
|
||||
for handler in self.scope_items.handlers.iter() {
|
||||
write!(f, ".service({}::{})", module_name, handler)?;
|
||||
}
|
||||
|
||||
writeln!(f, ";\n")?;
|
||||
writeln!(f, " actix_web::dev::HttpServiceFactory::register(scope, __config)")?;
|
||||
writeln!(
|
||||
f,
|
||||
" actix_web::dev::HttpServiceFactory::register(scope, __config)"
|
||||
)?;
|
||||
writeln!(f, " }}\n}}")
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ use actix_web::{
|
|||
web, App, Error, HttpRequest, HttpResponse, Responder,
|
||||
};
|
||||
use actix_web_codegen::{
|
||||
connect, delete, get, head, options, patch, post, put, route, routes, trace, scope
|
||||
connect, delete, get, head, options, patch, post, put, route, routes, scope, trace,
|
||||
};
|
||||
use futures_core::future::LocalBoxFuture;
|
||||
|
||||
|
@ -387,8 +387,10 @@ async fn test_wrap() {
|
|||
|
||||
#[scope("/test")]
|
||||
const mod_inner: () = {
|
||||
use actix_web::{HttpResponse, HttpRequest, Responder, put, head, connect, options, trace, patch, delete, web };
|
||||
use actix_web::web::Json;
|
||||
use actix_web::{
|
||||
connect, delete, head, options, patch, put, trace, web, web::Json, HttpRequest,
|
||||
HttpResponse, Responder,
|
||||
};
|
||||
|
||||
#[actix_web::get("/test")]
|
||||
pub async fn test() -> impl Responder {
|
||||
|
|
Loading…
Reference in New Issue