Skip to content

Commit

Permalink
chore: Linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Mar 11, 2024
1 parent 0c20927 commit f0e4bb1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
50 changes: 29 additions & 21 deletions actix-web-thiserror-derive/src/response_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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::<syn::ExprAssign, syn::Token![,]>::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::<HashMap<String, String>>();
let proc_macro2::TokenTree::Group(group) = x.tokens.into_iter().next()? else {
return None;
};
let ident_assigns =
syn::punctuated::Punctuated::<syn::ExprAssign, syn::Token![,]>::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::<HashMap<String, String>>();

if ident_assigns.get("transform")? == "custom" {
return Some(quote! { self.transform })
return Some(quote! { self.transform });
}

// could add additional to check for invalid options?
Expand Down Expand Up @@ -341,7 +349,7 @@ fn get_string(tokens: &mut Peekable<IntoIter>) -> Option<proc_macro2::TokenTree>

fn get_status_code(tokens: &mut Peekable<IntoIter>) -> Option<proc_macro2::TokenStream> {
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! {
Expand All @@ -356,7 +364,7 @@ fn get_status_code(tokens: &mut Peekable<IntoIter>) -> Option<proc_macro2::Token

fn get_reason(tokens: &mut Peekable<IntoIter>) -> Option<proc_macro2::TokenStream> {
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()),

Expand Down
6 changes: 3 additions & 3 deletions actix-web-thiserror/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//! ```
//!
Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn apply_global_transform(
reason: Option<serde_json::Value>,
) -> HttpResponse {
ResponseTransform::transform(
(&**RESPONSE_TRANSFORM.load()).as_ref(),
(**RESPONSE_TRANSFORM.load()).as_ref(),
name,
err,
status_code,
Expand All @@ -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)]
Expand Down

0 comments on commit f0e4bb1

Please sign in to comment.