Skip to content

Commit 6c543d3

Browse files
committed
fix: 🐛 Fix bad api design
Use normal Option instead of enum values
1 parent c529417 commit 6c543d3

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

envconfig_derive/src/lib.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,26 @@ fn gen_field_assign(field: &Field, source: &Source) -> proc_macro2::TokenStream
9898
// If nested attribute is present
9999
let nested_value_opt = find_item_in_list(&list, "nested");
100100
match nested_value_opt {
101-
MatchingItem::NoValue => return gen_field_assign_for_struct_type(field, source),
102-
MatchingItem::WithValue(_) => {
101+
Some(MatchingItem::NoValue) => return gen_field_assign_for_struct_type(field, source),
102+
Some(MatchingItem::WithValue(_)) => {
103103
panic!("`nested` attribute must not have a value")
104104
}
105-
MatchingItem::None => {}
105+
None => {}
106106
}
107107

108108
// Default value for the field
109109
let opt_default = match find_item_in_list(&list, "default") {
110-
MatchingItem::WithValue(v) => Some(v),
111-
MatchingItem::NoValue => panic!("`default` attribute must have a value"),
112-
MatchingItem::None => None,
110+
Some(MatchingItem::WithValue(v)) => Some(v),
111+
Some(MatchingItem::NoValue) => panic!("`default` attribute must have a value"),
112+
None => None,
113113
};
114114

115115
// Environment variable name
116116
let from_opt = find_item_in_list(&list, "from");
117117
let env_var = match from_opt {
118-
MatchingItem::WithValue(v) => quote! { #v },
119-
MatchingItem::NoValue => panic!("`from` attribute must have a value"),
120-
MatchingItem::None => field_to_env_var_name(field),
118+
Some(MatchingItem::WithValue(v)) => quote! { #v },
119+
Some(MatchingItem::NoValue) => panic!("`from` attribute must have a value"),
120+
None => field_to_env_var_name(field),
121121
};
122122

123123
gen(field, &env_var, opt_default, source)
@@ -263,22 +263,21 @@ fn fetch_args_from_attr(field: &Field, attr: &Attribute) -> Vec<Meta> {
263263
enum MatchingItem<'a> {
264264
WithValue(&'a Lit),
265265
NoValue,
266-
None,
267266
}
268267

269268
/// Tries to find the first matching item in the provided list
270269
///
271270
/// # Returns
272271
///
273-
/// - `Some(Some(Lit))` if a name-value pair is found
274-
/// - `Some(None)` if a path is found
272+
/// - `MatchingItem::WithValue(&Lit)` if a name-value pair is found
273+
/// - `MatchingItem::NoValue` if a path is found
275274
/// - `None` if no matching item is found
276275
///
277276
/// # Panics
278277
///
279278
/// - Multiple items with the same name exist
280279
/// - The item is not a name-value pair or a path
281-
fn find_item_in_list<'l>(list: &'l [Meta], item_name: &str) -> MatchingItem<'l> {
280+
fn find_item_in_list<'l>(list: &'l [Meta], item_name: &str) -> Option<MatchingItem<'l>> {
282281
// Find all items with the provided name
283282
let matching_items = list
284283
.iter()
@@ -298,13 +297,13 @@ fn find_item_in_list<'l>(list: &'l [Meta], item_name: &str) -> MatchingItem<'l>
298297
Meta::NameValue(MetaNameValue {
299298
value: Expr::Lit(value),
300299
..
301-
}) => MatchingItem::WithValue(&value.lit),
302-
Meta::Path(_) => MatchingItem::NoValue,
300+
}) => Some(MatchingItem::WithValue(&value.lit)),
301+
Meta::Path(_) => Some(MatchingItem::NoValue),
303302
_ => panic!("Expected `{item_name}` to be a name-value pair or a path"),
304303
};
305304
}
306305

307-
MatchingItem::None
306+
None
308307
}
309308

310309
/// Returns the name of the field as a string

0 commit comments

Comments
 (0)