Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions foundations-macros/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,20 @@ struct SerdeArgs {

impl SerdeArgs {
fn parse_from_attrs(attrs: &[Attribute]) -> Result<Option<Self>> {
let Some(attr) = attrs.iter().find(|a| a.path().is_ident("serde")) else {
let mut serde_attrs = attrs
.iter()
.filter(|a| a.path().is_ident("serde"))
.peekable();
if serde_attrs.peek().is_none() {
return Ok(None);
};
}

let mut meta_list = Vec::new();
for attr in serde_attrs {
meta_list.extend(attr.parse_args_with(|input: ParseStream| parse_meta_list(&input))?);
}

Ok(Some(Self::from_meta(&attr.meta)?))
Ok(Some(Self::from_list(&meta_list)?))
}
}

Expand Down
5 changes: 5 additions & 0 deletions foundations/tests/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct NestedStruct {
a: usize,
/// multi-line
/// doc comment
#[serde(alias = "legacy_b")]
#[serde(default = "NestedStruct::default_b")]
b: u32,
// no doc comment at all
Expand Down Expand Up @@ -42,6 +43,7 @@ struct NestedDup {
struct FlattenedStruct {
/// Doc comments on flattened members itself
/// should not appear on the output.
#[serde(default)]
#[serde(flatten)]
inner: NestedStruct,
/// Another field that isn't flattened.
Expand Down Expand Up @@ -262,6 +264,9 @@ fn defaults() {
assert_eq!(simple_struct.b, 0xb);
assert_eq!(simple_struct.c, 0);

let aliased_struct = from_yaml_str::<NestedStruct>("legacy_b: 123").unwrap();
assert_eq!(aliased_struct.b, 123);

let nested_struct = from_yaml_str::<SimpleStruct>("---\nx: 1").unwrap();

assert_eq!(nested_struct.inner.b, 0xb);
Expand Down