Skip to content

Commit

Permalink
style: Fix code styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Awayume committed Nov 1, 2023
1 parent aea2067 commit 73ce878
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 59 deletions.
127 changes: 71 additions & 56 deletions url_parser_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
// SPDX-FileCopyrightText: 2023 Awayume <[email protected]>
// 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,
};


Expand Down Expand Up @@ -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),
}
}
Expand All @@ -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)
}
}
Expand All @@ -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() {
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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,
}
}) {
Expand All @@ -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),
}
}
Expand Down Expand Up @@ -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,
}
}) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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),
}
}
Expand All @@ -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),
}
}
Expand Down
11 changes: 8 additions & 3 deletions url_parser_derive/tests/query_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -73,7 +74,9 @@ struct CustomType {

impl CustomType {
fn new(value: &str) -> Self {
Self {value: value.to_string()}
Self {
value: value.to_string(),
}
}
}

Expand Down Expand Up @@ -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(),
);
}

Expand Down Expand Up @@ -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(),
);
}

Expand Down

0 comments on commit 73ce878

Please sign in to comment.