code formatting cleanup

This commit is contained in:
Jon Lim 2023-09-13 22:45:37 -07:00
parent efe990dca5
commit ec4633a911
4 changed files with 69 additions and 43 deletions

View File

@ -2,6 +2,10 @@
## Unreleased ## Unreleased
## 4.2.3
- Add a scope macro that takes a path
## 4.2.2 ## 4.2.2
- Fix regression when declaring `wrap` attribute using an expression. - Fix regression when declaring `wrap` attribute using an expression.

View File

@ -1,10 +1,9 @@
use std::collections::HashSet; use std::{collections::HashSet, fmt};
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::{quote, ToTokens, TokenStreamExt}; use quote::{quote, ToTokens, TokenStreamExt};
use std::fmt;
use syn::{punctuated::Punctuated, Ident, LitStr, Path, Token}; use syn::{punctuated::Punctuated, Ident, LitStr, Path, Token};
#[derive(Debug)] #[derive(Debug)]
@ -570,23 +569,29 @@ impl ScopeItems {
for item in items { for item in items {
match item { match item {
syn::Item::Fn(ref fun) => { syn::Item::Fn(ref fun) => {
for attr in fun.attrs.iter() { for attr in fun.attrs.iter() {
for bound in attr.path().segments.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)); handlers.push(format!("{}", fun.sig.ident));
break; break;
} }
} }
} }
}, }
_ => continue, _ => continue,
} }
} }
Self { Self { handlers }
handlers,
}
} }
} }
@ -610,12 +615,14 @@ impl ScopeArgs {
let mut items = Vec::new(); let mut items = Vec::new();
match ast.expr.as_ref() { 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 { match item {
syn::Stmt::Item(ref item) => items.push(item.clone()), syn::Stmt::Item(ref item) => items.push(item.clone()),
_ => continue, _ => continue,
} }
}, }
}
_ => panic!("Scope should containt only code block"), _ => panic!("Scope should containt only code block"),
} }
@ -641,10 +648,9 @@ impl ScopeArgs {
match text.parse() { match text.parse() {
Ok(res) => res, Ok(res) => res,
Err(error) => panic!("Error: {:?}\nGenerated code: {}", error, text) Err(error) => panic!("Error: {:?}\nGenerated code: {}", error, text),
} }
} }
} }
impl fmt::Display for ScopeArgs { 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 module_name = syn::Ident::new(&module_name, ast.ident.span());
let ast = match ast.expr.as_ref() { let ast = match ast.expr.as_ref() {
syn::Expr::Block(expr) => quote!(pub mod #module_name #expr), 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, "{}\n", ast)?;
writeln!(f, "#[allow(non_camel_case_types)]")?; writeln!(f, "#[allow(non_camel_case_types)]")?;
writeln!(f, "struct {};\n", self.name)?; writeln!(f, "struct {};\n", self.name)?;
writeln!(f, "impl actix_web::dev::HttpServiceFactory for {} {{", self.name)?; writeln!(
writeln!(f, " fn register(self, __config: &mut actix_web::dev::AppService) {{")?; f,
write!(f, " let scope = actix_web::Scope::new(\"{}\")", self.path)?; "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() { for handler in self.scope_items.handlers.iter() {
write!(f, ".service({}::{})", module_name, handler)?; write!(f, ".service({}::{})", module_name, handler)?;
} }
writeln!(f, ";\n")?; writeln!(f, ";\n")?;
writeln!(f, " actix_web::dev::HttpServiceFactory::register(scope, __config)")?; writeln!(
f,
" actix_web::dev::HttpServiceFactory::register(scope, __config)"
)?;
writeln!(f, " }}\n}}") writeln!(f, " }}\n}}")
} }
} }

View File

@ -11,7 +11,7 @@ use actix_web::{
web, App, Error, HttpRequest, HttpResponse, Responder, web, App, Error, HttpRequest, HttpResponse, Responder,
}; };
use actix_web_codegen::{ 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; use futures_core::future::LocalBoxFuture;
@ -387,8 +387,10 @@ async fn test_wrap() {
#[scope("/test")] #[scope("/test")]
const mod_inner: () = { const mod_inner: () = {
use actix_web::{HttpResponse, HttpRequest, Responder, put, head, connect, options, trace, patch, delete, web }; use actix_web::{
use actix_web::web::Json; connect, delete, head, options, patch, put, trace, web, web::Json, HttpRequest,
HttpResponse, Responder,
};
#[actix_web::get("/test")] #[actix_web::get("/test")]
pub async fn test() -> impl Responder { pub async fn test() -> impl Responder {