Skip to content

Commit fe41b47

Browse files
authored
new: Add ConfigSetting.nested. (#143)
1 parent 44ac2ee commit fe41b47

File tree

9 files changed

+63
-52
lines changed

9 files changed

+63
-52
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
#### 🚀 Updates
6+
7+
- Added `ConfigSetting.nested` to recursively include nested settings.
8+
39
## 0.17.8
410

511
#### 🚀 Updates

Cargo.lock

+24-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ rpkl = "0.3.5"
1313
rust_decimal = "1.36.0"
1414
semver = "1.0.24"
1515
serde = { version = "1.0.217", features = ["derive"] }
16-
serde_json = "1.0.134"
16+
serde_json = "1.0.135"
1717
serde_yaml = "0.9.34"
18-
starbase_sandbox = "0.8.0"
18+
starbase_sandbox = "0.8.1"
1919
toml = "0.8.19"
2020
tracing = "0.1.41"
2121
url = "2.5.4"

crates/macros/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ all-features = true
1414
proc-macro = true
1515

1616
[dependencies]
17-
convert_case = "0.6.0"
17+
convert_case = "0.7.1"
1818
darling = "0.20.10"
1919
proc-macro2 = "1.0.89"
2020
quote = "1.0.37"

crates/macros/src/common/container.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,21 @@ impl Container<'_> {
3333
};
3434

3535
match self {
36-
Self::NamedStruct { fields } => {
36+
Self::NamedStruct { fields } | Self::UnnamedStruct { fields } => {
3737
for field in fields {
38-
let name = field.get_name(Some(&field.casing_format));
38+
let name = if field.name.is_some() {
39+
field.get_name(Some(&field.casing_format))
40+
} else {
41+
field.index.to_string()
42+
};
3943
let env_key = if let Some(value) = field.get_env_var() {
4044
quote!(Some(#value.into()))
4145
} else {
4246
quote!(None)
4347
};
44-
let type_alias = format_alias(field.value.to_token_stream());
45-
46-
settings.push(quote! {
47-
(#name.into(), schematic::ConfigSetting {
48-
env_key: #env_key,
49-
type_alias: #type_alias.into(),
50-
}),
51-
});
52-
}
53-
}
54-
Self::UnnamedStruct { fields } => {
55-
for field in fields {
56-
let name = field.index.to_string();
57-
let env_key = if let Some(value) = field.get_env_var() {
58-
quote!(Some(#value.into()))
48+
let nested = if field.is_nested() {
49+
let value = field.value_type.get_config_type();
50+
quote!(Some(#value::settings()))
5951
} else {
6052
quote!(None)
6153
};
@@ -64,6 +56,7 @@ impl Container<'_> {
6456
settings.push(quote! {
6557
(#name.into(), schematic::ConfigSetting {
6658
env_key: #env_key,
59+
nested: #nested,
6760
type_alias: #type_alias.into(),
6861
}),
6962
});
@@ -76,16 +69,16 @@ impl Container<'_> {
7669

7770
settings.push(quote! {
7871
(#name.into(), schematic::ConfigSetting {
79-
env_key: None,
8072
type_alias: #type_alias.into(),
73+
..Default::default()
8174
}),
8275
});
8376
}
8477
}
8578
};
8679

8780
quote! {
88-
std::collections::HashMap::from_iter([
81+
std::collections::BTreeMap::from_iter([
8982
#(#settings)*
9083
])
9184
}

crates/macros/src/common/field_value.rs

+9
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ impl<'l> FieldValue<'l> {
181181
}
182182
}
183183

184+
pub fn get_config_type(&self) -> &'l Type {
185+
match self {
186+
Self::NestedList { item, .. } => item,
187+
Self::NestedMap { value, .. } => value,
188+
Self::NestedValue { value, .. } => value,
189+
Self::Value { value, .. } => value,
190+
}
191+
}
192+
184193
pub fn get_inner_type(&self) -> Option<&'l Type> {
185194
match self {
186195
Self::Value { value, .. } => Some(value),

crates/macros/src/config/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl ToTokens for ConfigMacro<'_> {
145145
}
146146

147147
#instrument
148-
fn settings() -> std::collections::HashMap<String, schematic::ConfigSetting> {
148+
fn settings() -> schematic::ConfigSettingMap {
149149
#settings_metadata
150150
}
151151
}

crates/macros/src/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn format_case(format: &str, value: &str, is_variant: bool) -> String {
2424
} else {
2525
Case::Snake
2626
})
27-
.without_boundaries(&[Boundary::UpperDigit, Boundary::LowerDigit])
27+
.without_boundaries(&[Boundary::UPPER_DIGIT, Boundary::LOWER_DIGIT])
2828
.to_case(case)
2929
}
3030

crates/schematic/src/config/configs.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::extender::ExtendsFrom;
55
use super::validator::*;
66
use schematic_types::Schematic;
77
use serde::{de::DeserializeOwned, Serialize};
8-
use std::collections::HashMap;
8+
use std::collections::BTreeMap;
99

1010
/// Represents a partial configuration of the base [`Config`], with all settings marked as optional
1111
/// by wrapping the values in [`Option`].
@@ -86,8 +86,8 @@ pub trait Config: Sized + Schematic {
8686
fn from_partial(partial: Self::Partial) -> Self;
8787

8888
/// Return a map of all settings and their metadata for the configuration.
89-
fn settings() -> HashMap<String, ConfigSetting> {
90-
HashMap::default()
89+
fn settings() -> ConfigSettingMap {
90+
BTreeMap::default()
9191
}
9292
}
9393

@@ -98,8 +98,11 @@ pub trait ConfigEnum: Sized + Schematic {
9898
}
9999

100100
/// Represents metadata about a setting within a configuration.
101-
#[derive(Clone, Debug)]
101+
#[derive(Clone, Debug, Default)]
102102
pub struct ConfigSetting {
103103
pub env_key: Option<String>,
104+
pub nested: Option<ConfigSettingMap>,
104105
pub type_alias: String,
105106
}
107+
108+
pub type ConfigSettingMap = BTreeMap<String, ConfigSetting>;

0 commit comments

Comments
 (0)