From 73ce87838bf4b1a5ce5979d254b27f616e4306b4 Mon Sep 17 00:00:00 2001 From: Awayume Date: Wed, 1 Nov 2023 17:27:06 +0900 Subject: [PATCH] style: Fix code styles --- url_parser_derive/src/lib.rs | 127 +++++++++++++----------- url_parser_derive/tests/query_params.rs | 11 +- 2 files changed, 79 insertions(+), 59 deletions(-) diff --git a/url_parser_derive/src/lib.rs b/url_parser_derive/src/lib.rs index d39a600..560ef93 100644 --- a/url_parser_derive/src/lib.rs +++ b/url_parser_derive/src/lib.rs @@ -1,14 +1,26 @@ // SPDX-FileCopyrightText: 2023 Awayume // SPDX-License-Identifier: Apache-2.0 -use proc_macro::TokenStream; use std::str::FromStr; +use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use quote::quote; use syn::{ - GenericArgument, parse_macro_input, parse_quote, Path, PathArguments, Ident, Item, Type, - TypeArray, TypePath, TypePtr, TypeReference, TypeSlice, TypeTuple, + parse_macro_input, + parse_quote, + GenericArgument, + Ident, + Item, + Path, + PathArguments, + Type, + TypeArray, + TypePath, + TypePtr, + TypeReference, + TypeSlice, + TypeTuple, }; @@ -87,14 +99,14 @@ fn parse_type_array(field_ident: &Ident, tarray: TypeArray, query_generator: Tok } else { unsupported_field_type_error(field_ident, query_generator) } - }, + } Type::Reference(tref) => { if let Type::Path(tpath) = *tref.elem { parse_slice(field_ident, tpath, query_generator) } else { unsupported_field_type_error(field_ident, query_generator) } - }, + } _ => unsupported_field_type_error(field_ident, query_generator), } } @@ -103,11 +115,14 @@ fn parse_type_array(field_ident: &Ident, tarray: TypeArray, query_generator: Tok fn parse_type_path(field_ident: &Ident, tpath: TypePath, query_generator: TokenStream2) -> TokenStream2 { let option_path: Path = parse_quote!(Option); let vec_path: Path = parse_quote!(Vec); - if tpath.path.segments[0].ident == option_path.segments[0].ident { // Option + if tpath.path.segments[0].ident == option_path.segments[0].ident { + // Option parse_option(field_ident, tpath, query_generator) - } else if tpath.path.segments[0].ident == vec_path.segments[0].ident { // Vec + } else if tpath.path.segments[0].ident == vec_path.segments[0].ident { + // Vec parse_vector(field_ident, tpath, query_generator) - } else { // Others + } else { + // Others parse_impl_display(field_ident, query_generator) } } @@ -124,17 +139,17 @@ fn parse_type_ptr(field_ident: &Ident, tptr: TypePtr, query_generator: TokenStre } else { unsupported_field_type_error(field_ident, query_generator) } - }, + } Type::Reference(tref) => { if let Type::Path(tpath) = *tref.elem { parse_ptr_slice(field_ident, tpath, query_generator) } else { unsupported_field_type_error(field_ident, query_generator) } - }, + } _ => unsupported_field_type_error(field_ident, query_generator), } - }, + } Type::Path(tpath) => { if is_option(&tpath) { match get_type_argument(&tpath).unwrap() { @@ -150,41 +165,41 @@ fn parse_type_ptr(field_ident: &Ident, tptr: TypePtr, query_generator: TokenStre } } } - }, - Type::Ptr(tptr) => { - if let Type::Path(tpath) = *tptr.elem { - if is_option(&tpath) || is_vec(&tpath) { - unsupported_field_type_error(field_ident, query_generator) - } else { - quote! { - #query_generator - // query: String - if let Some(val) = self.#field_ident { - query += &format!("{}={}&", stringify!(#field_ident), *val); + } + Type::Ptr(tptr) => { + if let Type::Path(tpath) = *tptr.elem { + if is_option(&tpath) || is_vec(&tpath) { + unsupported_field_type_error(field_ident, query_generator) + } else { + quote! { + #query_generator + // query: String + if let Some(val) = self.#field_ident { + query += &format!("{}={}&", stringify!(#field_ident), *val); + } + } + } + } else { + unsupported_field_type_error(field_ident, query_generator) } } - } - } else { - unsupported_field_type_error(field_ident, query_generator) - } - }, - Type::Reference(tref) => { - if let Type::Path(tpath) = *tref.elem { - if is_option(&tpath) || is_vec(&tpath) { - unsupported_field_type_error(field_ident, query_generator) - } else { - quote! { - #query_generator - // query: String - if let Some(val) = self.#field_ident { - query += &format!("{}={}&", stringify!(#field_ident), val); + Type::Reference(tref) => { + if let Type::Path(tpath) = *tref.elem { + if is_option(&tpath) || is_vec(&tpath) { + unsupported_field_type_error(field_ident, query_generator) + } else { + quote! { + #query_generator + // query: String + if let Some(val) = self.#field_ident { + query += &format!("{}={}&", stringify!(#field_ident), val); + } + } + } + } else { + unsupported_field_type_error(field_ident, query_generator) } } - } - } else { - unsupported_field_type_error(field_ident, query_generator) - } - }, _ => unsupported_field_type_error(field_ident, query_generator), } } else if is_vec(&tpath) { @@ -196,14 +211,14 @@ fn parse_type_ptr(field_ident: &Ident, tptr: TypePtr, query_generator: TokenStre } else { unsupported_field_type_error(&field_ident, query_generator) } - }, + } Type::Reference(tref) => { if let Type::Path(tpath) = *tref.elem { parse_ptr_slice(&field_ident, tpath, query_generator) } else { unsupported_field_type_error(&field_ident, query_generator) } - }, + } _ => unsupported_field_type_error(field_ident, query_generator), } } else { @@ -215,14 +230,14 @@ fn parse_type_ptr(field_ident: &Ident, tptr: TypePtr, query_generator: TokenStre } } } - }, + } Type::Slice(tslice) => { if let Type::Path(tpath) = *tslice.elem { parse_ptr_slice(field_ident, tpath, query_generator) } else { unsupported_field_type_error(field_ident, query_generator) } - }, + } Type::Tuple(ttuple) => { if ttuple.elems.iter().all(|ty: &Type| -> bool { match ty { @@ -233,14 +248,14 @@ fn parse_type_ptr(field_ident: &Ident, tptr: TypePtr, query_generator: TokenStre } else { false } - }, + } Type::Reference(tref) => { if let Type::Path(tpath) = *tref.elem.clone() { !(is_option(&tpath) || is_vec(&tpath)) } else { false } - }, + } _ => false, } }) { @@ -262,7 +277,7 @@ fn parse_type_ptr(field_ident: &Ident, tptr: TypePtr, query_generator: TokenStre } else { unsupported_field_type_error(field_ident, query_generator) } - }, + } _ => unsupported_field_type_error(field_ident, query_generator), } } @@ -356,14 +371,14 @@ fn parse_type_tuple(field_ident: &Ident, ttuple: TypeTuple, query_generator: Tok } else { false } - }, + } Type::Reference(tref) => { if let Type::Path(tpath) = *tref.elem.clone() { !(is_option(&tpath) || is_vec(&tpath)) } else { false } - }, + } _ => false, } }) { @@ -419,7 +434,7 @@ fn parse_option(field_ident: &Ident, tpath: TypePath, query_generator: TokenStre } } } - }, + } Type::Ptr(tptr) => { if let Type::Path(tpath) = *tptr.elem { if is_option(&tpath) || is_vec(&tpath) { @@ -436,7 +451,7 @@ fn parse_option(field_ident: &Ident, tpath: TypePath, query_generator: TokenStre } else { unsupported_field_type_error(field_ident, query_generator) } - }, + } Type::Reference(tref) => { if let Type::Path(tpath) = *tref.elem { if is_option(&tpath) || is_vec(&tpath) { @@ -453,7 +468,7 @@ fn parse_option(field_ident: &Ident, tpath: TypePath, query_generator: TokenStre } else { unsupported_field_type_error(field_ident, query_generator) } - }, + } _ => unsupported_field_type_error(field_ident, query_generator), } } @@ -468,14 +483,14 @@ fn parse_vector(field_ident: &Ident, tpath: TypePath, query_generator: TokenStre } else { unsupported_field_type_error(&field_ident, query_generator) } - }, + } Type::Reference(tref) => { if let Type::Path(tpath) = *tref.elem.clone() { parse_slice(&field_ident, tpath, query_generator) } else { unsupported_field_type_error(&field_ident, query_generator) } - }, + } _ => unsupported_field_type_error(field_ident, query_generator), } } diff --git a/url_parser_derive/tests/query_params.rs b/url_parser_derive/tests/query_params.rs index add3646..84cbdff 100644 --- a/url_parser_derive/tests/query_params.rs +++ b/url_parser_derive/tests/query_params.rs @@ -3,6 +3,7 @@ use std::fmt::{Display, Formatter, Result as FormatterResult}; use std::ptr; + use url_parser_derive::QueryParams; use url_parser_trait::QueryParams; @@ -73,7 +74,9 @@ struct CustomType { impl CustomType { fn new(value: &str) -> Self { - Self {value: value.to_string()} + Self { + value: value.to_string(), + } } } @@ -113,7 +116,8 @@ fn basic_types() { concat!( "?u8=1&f32=1.0&bool=true&string=String&str=str&char=c&array_u8=1,2,3&array_bool=true,false,true", "&array_string=A,B,C&array_str=a,b,c&array_char=a,b,c&tuple=1,true,String, str,c&slice=1,2,3", - ).to_string(), + ) + .to_string(), ); } @@ -173,7 +177,8 @@ fn vector_types() { concat!( "?vec_u8=1,2,3&vec_f32=1.0,2.0,3.0&vec_bool=true,false", "&vec_string=St,ri,ng&vec_str=st,r&vec_char=c,h,a,r", - ).to_string(), + ) + .to_string(), ); }