Skip to content

Commit

Permalink
fix: do not wrap an envfield via generics_only
Browse files Browse the repository at this point in the history
  • Loading branch information
mrshiposha committed Nov 7, 2023
1 parent 8b08402 commit 07dfdff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion env-field-wrap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde-env-field-wrap"
version = "0.1.1"
version = "0.3.0"
edition = "2021"
description = "An attribute that wraps all the fields of a struct or an enum with the EnvField type"
license = "MIT"
Expand Down
31 changes: 23 additions & 8 deletions env-field-wrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ fn is_type(ty: &syn::Type, ty_paths: &[&str]) -> bool {
}
}

fn is_option(ty: &syn::Type) -> bool {
is_type(
ty,
&["Option", "std::option::Option", "core::option::Option"],
)
}

fn is_vec(ty: &syn::Type) -> bool {
is_type(ty, &["Vec", "std::vec::Vec", "alloc::vec::Vec"])
}

fn is_env_field(ty: &syn::Type) -> bool {
is_type(ty, &["EnvField", "serde_env_field::EnvField"])
}

fn wrap_generics_only(ty: &syn::Type) -> TokenStream2 {
match ty {
syn::Type::Path(ty) => {
Expand All @@ -121,9 +136,13 @@ fn wrap_generics_only(ty: &syn::Type) -> TokenStream2 {
.iter()
.map(|arg| match arg {
GenericArgument::Type(generic) => {
quote!(::serde_env_field::EnvField<#generic>)
if is_env_field(generic) {
quote!(#generic)
} else {
quote!(::serde_env_field::EnvField<#generic>)
}
}
_ => abort!(angle_args.args, "generics_only: a type is expected"),
non_ty_generic => quote!(#non_ty_generic),
})
.collect::<Punctuated<_, Token![,]>>();

Expand Down Expand Up @@ -168,13 +187,9 @@ fn process_fields(fields: impl Iterator<Item = syn::Field>) -> TokenStream2 {
Some(WrapAttr::Skip) => quote!(#ty),
Some(WrapAttr::GenericsOnly(_)) => wrap_generics_only(&ty),
None => {
if is_type(
&ty,
&["Option", "std::option::Option", "core::option::Option"],
) || is_type(&ty, &["Vec", "std::vec::Vec", "alloc::vec::Vec"])
{
if is_option(&ty) || is_vec(&ty) {
wrap_generics_only(&ty)
} else if is_type(&ty, &["EnvField", "serde_env_field::EnvField"]) {
} else if is_env_field(&ty) {
quote!(#ty)
} else {
quote!(::serde_env_field::EnvField<#ty>)
Expand Down

0 comments on commit 07dfdff

Please sign in to comment.