xso: add support for ignoring unknown children

This commit is contained in:
Jonas Schäfer 2024-10-03 12:51:57 +02:00 committed by Link Mauve
commit 66233b0150
10 changed files with 242 additions and 6 deletions

View file

@ -229,6 +229,44 @@ impl<T: FromXml, E: From<Error>> FromXml for Result<T, E> {
}
}
/// Builder which discards an entire child tree without inspecting the
/// contents.
#[derive(Debug)]
pub struct Discard {
depth: usize,
}
impl Discard {
/// Create a new discarding builder.
pub fn new() -> Self {
Self { depth: 0 }
}
}
impl FromEventsBuilder for Discard {
type Output = ();
fn feed(&mut self, ev: rxml::Event) -> Result<Option<Self::Output>, Error> {
match ev {
rxml::Event::StartElement(..) => {
self.depth = match self.depth.checked_add(1) {
Some(v) => v,
None => return Err(Error::Other("maximum XML nesting depth exceeded")),
};
Ok(None)
}
rxml::Event::EndElement(..) => match self.depth.checked_sub(1) {
None => Ok(Some(())),
Some(v) => {
self.depth = v;
Ok(None)
}
},
_ => Ok(None),
}
}
}
#[cfg(test)]
mod tests {
use super::*;