From f0e4bb11c8f31d1dcf36bdadd4ba571ee07a2f9a Mon Sep 17 00:00:00 2001 From: John Vandenberg Date: Mon, 11 Mar 2024 09:52:22 +0800 Subject: [PATCH] chore: Linting fixes --- README.md | 2 +- .../src/response_error.rs | 50 +++++++++++-------- actix-web-thiserror/src/lib.rs | 6 +-- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 0408a43..c5eb2c8 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ The error text automatically prints to the log when the error is returned out through a http response. ``` -Apr 23 02:19:35.211 ERRO Response error: invalid image format +Apr 23 02:19:35.211 ERROR Response error: invalid image format Base64ImageError(InvalidImageFormat), place: example/src/handler.rs:5 example::handler ``` diff --git a/actix-web-thiserror-derive/src/response_error.rs b/actix-web-thiserror-derive/src/response_error.rs index a43dc0e..110aefe 100644 --- a/actix-web-thiserror-derive/src/response_error.rs +++ b/actix-web-thiserror-derive/src/response_error.rs @@ -2,10 +2,10 @@ use std::collections::{HashMap, HashSet}; use std::iter::Peekable; use proc_macro::TokenStream; -use proc_macro2::{TokenTree, token_stream::IntoIter}; +use proc_macro2::{token_stream::IntoIter, TokenTree}; use quote::quote; -use syn::DeriveInput; use syn::parse::Parser; +use syn::DeriveInput; pub fn derive_response_error(input: TokenStream) -> TokenStream { let ast = syn::parse_macro_input!(input as DeriveInput); @@ -169,31 +169,39 @@ pub fn derive_response_error(input: TokenStream) -> TokenStream { } }; - let transform = ast.attrs + let transform = ast + .attrs .into_iter() .find_map(|x| { if !(x.path.segments.len() == 1 && x.path.segments.first()?.ident == "response") { return None; } - let proc_macro2::TokenTree::Group(group) = x.tokens.into_iter().next()? else { return None; }; - let ident_assigns = syn::punctuated::Punctuated::::parse_terminated - .parse(group.stream().into()) - .unwrap() - .into_iter() - .filter_map(|x| { - let syn::Expr::Path(syn::ExprPath {path, ..}) = *x.left else { return None; }; - let left = path.get_ident()?; - - let syn::Expr::Path(syn::ExprPath {path, ..}) = *x.right else { return None; }; - let right = path.get_ident()?; - - Some((left.to_string(), right.to_string())) - }) - .collect::>(); + let proc_macro2::TokenTree::Group(group) = x.tokens.into_iter().next()? else { + return None; + }; + let ident_assigns = + syn::punctuated::Punctuated::::parse_terminated + .parse(group.stream().into()) + .unwrap() + .into_iter() + .filter_map(|x| { + let syn::Expr::Path(syn::ExprPath { path, .. }) = *x.left else { + return None; + }; + let left = path.get_ident()?; + + let syn::Expr::Path(syn::ExprPath { path, .. }) = *x.right else { + return None; + }; + let right = path.get_ident()?; + + Some((left.to_string(), right.to_string())) + }) + .collect::>(); if ident_assigns.get("transform")? == "custom" { - return Some(quote! { self.transform }) + return Some(quote! { self.transform }); } // could add additional to check for invalid options? @@ -341,7 +349,7 @@ fn get_string(tokens: &mut Peekable) -> Option fn get_status_code(tokens: &mut Peekable) -> Option { match tokens.peek() { - Some(TokenTree::Ident(_)) => get_ident_stream(tokens).map(|tokens| tokens), + Some(TokenTree::Ident(_)) => get_ident_stream(tokens), Some(TokenTree::Literal(_)) => get_status_code_literal(tokens).map(|tokens| { quote! { @@ -356,7 +364,7 @@ fn get_status_code(tokens: &mut Peekable) -> Option) -> Option { match tokens.peek() { - Some(TokenTree::Ident(_)) => get_ident_stream(tokens).map(|tokens| tokens), + Some(TokenTree::Ident(_)) => get_ident_stream(tokens), Some(TokenTree::Literal(_)) => get_string(tokens).map(|tokens| tokens.into()), diff --git a/actix-web-thiserror/src/lib.rs b/actix-web-thiserror/src/lib.rs index 499647c..1ee11b9 100644 --- a/actix-web-thiserror/src/lib.rs +++ b/actix-web-thiserror/src/lib.rs @@ -66,7 +66,7 @@ //! through a http response. //! //! ```text -//! Apr 23 02:19:35.211 ERRO Response error: invalid image format +//! Apr 23 02:19:35.211 ERROR Response error: invalid image format //! Base64ImageError(InvalidImageFormat), place: example/src/handler.rs:5 example::handler //! ``` //! @@ -123,7 +123,7 @@ pub fn apply_global_transform( reason: Option, ) -> HttpResponse { ResponseTransform::transform( - (&**RESPONSE_TRANSFORM.load()).as_ref(), + (**RESPONSE_TRANSFORM.load()).as_ref(), name, err, status_code, @@ -133,7 +133,7 @@ pub fn apply_global_transform( #[doc(hidden)] pub fn default_global_error_status_code() -> actix_web::http::StatusCode { - ResponseTransform::default_error_status_code((&**RESPONSE_TRANSFORM.load()).as_ref()) + ResponseTransform::default_error_status_code((**RESPONSE_TRANSFORM.load()).as_ref()) } #[doc(hidden)]