From 07dfdff3f1a708aeacb3ced7865b8cc6b0402aa1 Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Tue, 7 Nov 2023 04:58:41 +0100 Subject: [PATCH] fix: do not wrap an envfield via generics_only --- env-field-wrap/Cargo.toml | 2 +- env-field-wrap/src/lib.rs | 31 +++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/env-field-wrap/Cargo.toml b/env-field-wrap/Cargo.toml index 05b7f47..440f6fe 100644 --- a/env-field-wrap/Cargo.toml +++ b/env-field-wrap/Cargo.toml @@ -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" diff --git a/env-field-wrap/src/lib.rs b/env-field-wrap/src/lib.rs index 7b89407..5f4e52e 100644 --- a/env-field-wrap/src/lib.rs +++ b/env-field-wrap/src/lib.rs @@ -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) => { @@ -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::>(); @@ -168,13 +187,9 @@ fn process_fields(fields: impl Iterator) -> 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>)