11use heck:: ToPascalCase ;
22use proc_macro2:: TokenStream ;
33use quote:: quote;
4- use syn:: { Ident , LitStr , spanned:: Spanned } ;
4+ use syn:: { spanned:: Spanned , Ident , LitStr } ;
55
66/// A parsed Field of a struct
77pub ( crate ) struct Field {
@@ -11,6 +11,7 @@ pub(crate) struct Field {
1111
1212 optional : bool ,
1313 infallible : bool ,
14+ skip : bool ,
1415 desc : Option < String > ,
1516
1617 _attrs : Vec < syn:: Attribute > ,
@@ -26,13 +27,18 @@ impl TryFrom<&syn::Field> for Field {
2627 let mut env_var = None ;
2728 let mut infallible = false ;
2829 let mut desc = None ;
30+ let mut skip = false ;
2931
3032 field
3133 . attrs
3234 . iter ( )
3335 . filter ( |attr| attr. path ( ) . is_ident ( "from_env" ) )
3436 . for_each ( |attr| {
3537 let _ = attr. parse_nested_meta ( |meta| {
38+ if meta. path . is_ident ( "skip" ) {
39+ skip = true ;
40+ return Ok ( ( ) ) ;
41+ }
3642 if meta. path . is_ident ( "optional" ) {
3743 optional = true ;
3844 return Ok ( ( ) ) ;
@@ -68,6 +74,7 @@ impl TryFrom<&syn::Field> for Field {
6874 field_name,
6975 field_type,
7076 optional,
77+ skip,
7178 infallible,
7279 desc,
7380 _attrs : field
@@ -115,7 +122,7 @@ impl Field {
115122
116123 /// Produces the name of the enum variant for the field
117124 pub ( crate ) fn enum_variant_name ( & self , idx : usize ) -> Option < TokenStream > {
118- if self . infallible {
125+ if self . skip || self . infallible {
119126 return None ;
120127 }
121128
@@ -143,10 +150,16 @@ impl Field {
143150
144151 /// Produces the a line for the `inventory` function
145152 /// of the form
146- /// items.push(...);
153+ /// items.push(...); // (if this is a FromEnvVar)
154+ /// or
155+ /// items.extend(...); // (if this is a FromEnv)
147156 /// or
148- /// items.extend(...);
157+ /// // nothing if this is a skip
149158 pub ( crate ) fn expand_env_item_info ( & self ) -> TokenStream {
159+ if self . skip {
160+ return quote ! { } ;
161+ }
162+
150163 let description = self . desc . clone ( ) . unwrap_or_default ( ) ;
151164 let optional = self . optional ;
152165
@@ -197,10 +210,20 @@ impl Field {
197210
198211 // // OR
199212 // let field_name = FromEnv::from_env().map_err()?;
213+
214+ // // OR
215+ // let field_name = Default::default();
216+
200217 //```
201218 let variant = self . enum_variant_name ( idx) ;
202219 let field_name = self . field_name ( idx) ;
203220
221+ if self . skip {
222+ return quote ! {
223+ let #field_name = Default :: default ( ) ;
224+ } ;
225+ }
226+
204227 let fn_invoc = if let Some ( ref env_var) = self . env_var {
205228 quote ! { FromEnvVar :: from_env_var( #env_var) }
206229 } else {
0 commit comments