xso-proc: Add the default flag to the element meta

This allows the payload to be absent, and requires the field type to be
Option<minidom::Element>.
This commit is contained in:
Emmanuel Gil Peyrot 2024-12-20 18:41:39 +01:00 committed by Jonas Schäfer
commit 1823afbc71
6 changed files with 107 additions and 6 deletions

View file

@ -22,7 +22,8 @@ Version NEXT:
structs.
- Support for collecting all unknown children in a single field as
collection of `minidom::Element`, or one unknown child as a
`minidom::Element`.
`minidom::Element`, or zero or one unknown children as an
`Option<minidom::Element>`.
- Support for "transparent" structs (newtype-like patterns for XSO).
- FromXmlText and AsXmlText are now implemented for jid::NodePart,
jid::DomainPart, and jid::ResourcePart (!485)

View file

@ -433,6 +433,7 @@ The following keys can be used inside the `#[xml(extract(..))]` meta:
| Key | Value type | Description |
| --- | --- | --- |
| `default` | flag | If present, an absent child will substitute the default value instead of raising an error. |
| `n` | `1` or `..` | If `1`, a single element is parsed. If `..`, a collection is parsed. Defaults to `1`. |
When parsing a single child element (i.e. `n = 1` or no `n` value set at all),
@ -446,6 +447,16 @@ addition, the field's type must implement
field's reference type must implement
`IntoIterator<Item = &'_ minidom::Element>` to derive `AsXml`.
If `default` is specified and the child is absent in the source, the value
is generated using [`core::default::Default`]. `default` has no influence on
`AsXml`. Combining `default` and `n` where `n` is not set to `1` is not
supported and will cause a compile-time error.
Using `default` with a type other than `Option<T>` will cause the
serialisation to mismatch the deserialisation (i.e. the struct is then not
roundtrip-safe), because the deserialisation does not compare the value
against `default` (but has special provisions to work with `Option<T>`).
Fields with the `element` meta are deserialised with the lowest priority.
While other fields are processed in the order they are declared, `element`
fields may capture arbitrary child elements, so they are considered as the