xso: add support for ignoring unknown attributes

This commit is contained in:
Jonas Schäfer 2024-10-03 12:29:10 +02:00 committed by Link Mauve
commit 290460ba9d
9 changed files with 185 additions and 23 deletions

View file

@ -291,6 +291,38 @@ impl<T: AsXmlText> AsOptionalXmlText for Option<T> {
}
}
/// Control how unknown attributes are handled.
///
/// The variants of this enum are referenced in the
/// `#[xml(on_unknown_attribute = ..)]` which can be used on structs and
/// enum variants. The specified variant controls how attributes, which are
/// not handled by any member of the compound, are handled during parsing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum UnknownAttributePolicy {
/// All unknown attributes are discarded.
Discard,
/// The first unknown attribute which is encountered generates a fatal
/// parsing error.
///
/// This is the default policy.
#[default]
Fail,
}
impl UnknownAttributePolicy {
#[doc(hidden)]
/// Implementation of the policy.
///
/// This is an internal API and not subject to semver versioning.
pub fn apply_policy(&self, msg: &'static str) -> Result<(), self::error::Error> {
match self {
Self::Fail => Err(self::error::Error::Other(msg)),
Self::Discard => Ok(()),
}
}
}
/// Attempt to transform a type implementing [`AsXml`] into another
/// type which implements [`FromXml`].
pub fn transform<T: FromXml, F: AsXml>(from: F) -> Result<T, self::error::Error> {