xso: reject duplicate children

This was an oversight. Even though we apparently don't have tests for
this anywhere, it is what the old functional macros do.
This commit is contained in:
Jonas Schäfer 2024-08-03 13:28:07 +02:00 committed by Link Mauve
commit 6440209f95
5 changed files with 92 additions and 1 deletions

View file

@ -87,3 +87,15 @@ pub(super) fn on_missing_attribute(parent_name: &ParentRef, field: &Member) -> S
pub(super) fn on_missing_child(parent_name: &ParentRef, field: &Member) -> String {
format!("Missing child {} in {}.", FieldName(&field), parent_name)
}
/// Create a string error message for a duplicate child element.
///
/// `parent_name` should point at the compound which is being parsed and
/// `field` should be the field to which the child belongs.
pub(super) fn on_duplicate_child(parent_name: &ParentRef, field: &Member) -> String {
format!(
"{} must not have more than one child in {}.",
parent_name,
FieldName(&field)
)
}

View file

@ -393,6 +393,8 @@ impl FieldDef {
AmountConstraint::FixedSingle(_) => {
let missing_msg =
error_message::on_missing_child(container_name, &self.member);
let duplicate_msg =
error_message::on_duplicate_child(container_name, &self.member);
let on_absent = match default_ {
Flag::Absent => quote! {
@ -411,7 +413,16 @@ impl FieldDef {
init: quote! { ::std::option::Option::None },
ty: option_ty(self.ty.clone()),
},
matcher,
matcher: quote! {
match #matcher {
::core::result::Result::Ok(v) => if #field_access.is_some() {
::core::result::Result::Err(::xso::error::FromEventsError::Invalid(::xso::error::Error::Other(#duplicate_msg)))
} else {
::core::result::Result::Ok(v)
},
::core::result::Result::Err(e) => ::core::result::Result::Err(e),
}
},
builder,
collect: quote! {
#field_access = ::std::option::Option::Some(#substate_result);