Skip to content

Commit dc20fcc

Browse files
committed
Polish.
1 parent db88f5b commit dc20fcc

File tree

9 files changed

+130
-23
lines changed

9 files changed

+130
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Added support for env prefixes at the field level when the field is also nested. This will
1919
override the env prefix defined on the nested container:
2020
`#[setting(nested, env_prefix = "OVERRIDE_")]`.
21+
- Updated `#[setting(extend)]` settings to support `Option` wrapped values.
2122
- Updated the methods of `PartialConfig` to all have a default implementation. This helps to greatly
2223
reduce the amount of macro generated code.
2324
- Improved the parse, handling, and validation of container and field attributes.

crates/core/src/container.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl Container {
293293
panic!("Only 1 variant may be marked as default.");
294294
}
295295

296-
match default_variants.get(0) {
296+
match default_variants.first() {
297297
Some(default_variant) => {
298298
let res = default_variant.impl_partial_default_value();
299299

crates/core/src/field.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ impl Field {
202202
return Some(self.get_name().to_uppercase());
203203
}
204204

205+
if self.args.parse_env.is_some() {
206+
panic!("Cannot use `parse_env` without `env` or a parent `env_prefix`.");
207+
}
208+
205209
None
206210
}
207211

@@ -297,15 +301,10 @@ impl Field {
297301
return self.value.impl_partial_env_value(&self.args, "");
298302
}
299303

300-
let Some(env_key) = self.get_env_var() else {
301-
if self.args.parse_env.is_some() {
302-
panic!("Cannot use `parse_env` without `env` or a parent `env_prefix`.");
303-
}
304-
305-
return ImplResult::skipped();
306-
};
307-
308-
self.value.impl_partial_env_value(&self.args, &env_key)
304+
match self.get_env_var() {
305+
Some(env_key) => self.value.impl_partial_env_value(&self.args, &env_key),
306+
None => ImplResult::skipped(),
307+
}
309308
}
310309

311310
pub fn impl_partial_extends_from(&self) -> ImplResult {

crates/core/src/field_value.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,13 @@ impl FieldValue {
239239
}
240240

241241
#[cfg(not(feature = "extends"))]
242-
pub fn impl_partial_extends_from(&self) -> ImplResult {
242+
pub fn impl_partial_extends_from(&self, _field_name: &Ident) -> ImplResult {
243243
ImplResult::skipped()
244244
}
245245

246246
#[cfg(feature = "extends")]
247247
pub fn impl_partial_extends_from(&self, field_name: &Ident) -> ImplResult {
248-
let mut res = ImplResult::default();
249-
250-
res.value = match self.ty_string.as_str() {
248+
let value = match self.ty_string.as_str() {
251249
"String" | "Option<String>" => {
252250
quote! {
253251
self.#field_name
@@ -277,7 +275,10 @@ impl FieldValue {
277275
}
278276
};
279277

280-
res
278+
ImplResult {
279+
value,
280+
..Default::default()
281+
}
281282
}
282283
}
283284

@@ -307,7 +308,6 @@ fn extract_type_information(
307308

308309
if let Some(GenericArgument::Type(inner_ty)) = args.args.last() {
309310
extract_type_information(inner_ty, layers, on_last);
310-
return;
311311
}
312312
}
313313

crates/core/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn is_inheritable_attribute(attr: &Attribute) -> bool {
2727
}
2828

2929
pub fn to_type_string(ts: TokenStream) -> String {
30-
format!("{}", ts)
30+
format!("{ts}")
3131
.replace(" :: ", "::")
3232
.replace(" , ", ", ")
3333
.replace(" < ", "<")
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
source: crates/core/tests/container_partial_test.rs
3+
expression: container.args.partial.as_ref().unwrap()
4+
---
5+
PartialArg {
6+
meta: [
7+
Meta(
8+
Meta::List {
9+
path: Path {
10+
leading_colon: None,
11+
segments: [
12+
PathSegment {
13+
ident: Ident(
14+
derive,
15+
),
16+
arguments: PathArguments::None,
17+
},
18+
],
19+
},
20+
delimiter: MacroDelimiter::Paren(
21+
Paren,
22+
),
23+
tokens: TokenStream [
24+
Ident {
25+
sym: Other,
26+
},
27+
],
28+
},
29+
),
30+
Meta(
31+
Meta::List {
32+
path: Path {
33+
leading_colon: None,
34+
segments: [
35+
PathSegment {
36+
ident: Ident(
37+
serde,
38+
),
39+
arguments: PathArguments::None,
40+
},
41+
],
42+
},
43+
delimiter: MacroDelimiter::Paren(
44+
Paren,
45+
),
46+
tokens: TokenStream [
47+
Ident {
48+
sym: another,
49+
},
50+
],
51+
},
52+
),
53+
],
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
source: crates/core/tests/field_partial_test.rs
3+
expression: field.args.partial.as_ref().unwrap()
4+
---
5+
PartialArg {
6+
meta: [
7+
Meta(
8+
Meta::List {
9+
path: Path {
10+
leading_colon: None,
11+
segments: [
12+
PathSegment {
13+
ident: Ident(
14+
other,
15+
),
16+
arguments: PathArguments::None,
17+
},
18+
],
19+
},
20+
delimiter: MacroDelimiter::Paren(
21+
Paren,
22+
),
23+
tokens: TokenStream [
24+
Ident {
25+
sym: attribute,
26+
},
27+
],
28+
},
29+
),
30+
Meta(
31+
Meta::List {
32+
path: Path {
33+
leading_colon: None,
34+
segments: [
35+
PathSegment {
36+
ident: Ident(
37+
and,
38+
),
39+
arguments: PathArguments::None,
40+
},
41+
],
42+
},
43+
delimiter: MacroDelimiter::Paren(
44+
Paren,
45+
),
46+
tokens: TokenStream [
47+
Ident {
48+
sym: another,
49+
},
50+
],
51+
},
52+
),
53+
],
54+
}

crates/core/tests/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use proc_macro2::TokenStream;
2-
use schematic_core::container::Container;
3-
use schematic_core::field::Field;
2+
// use schematic_core::container::Container;
3+
// use schematic_core::field::Field;
44

55
pub fn pretty(tokens: TokenStream) -> String {
66
prettyplease::unparse(&syn::parse_file(&tokens.to_string()).unwrap())

crates/schematic/src/config/configs.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,10 @@ pub trait Config: Sized + Schematic {
102102
/// Return default values for the partial configuration.
103103
fn default_partial() -> Self::Partial {
104104
let context = <<Self as Config>::Partial as PartialConfig>::Context::default();
105-
let defaults = <<Self as Config>::Partial as PartialConfig>::default_values(&context)
106-
.unwrap()
107-
.unwrap_or_default();
108105

109-
defaults
106+
<<Self as Config>::Partial as PartialConfig>::default_values(&context)
107+
.unwrap()
108+
.unwrap_or_default()
110109
}
111110

112111
/// Convert a partial configuration into a full configuration, with all values populated.

0 commit comments

Comments
 (0)