xso: add support for ignoring unknown children
This commit is contained in:
parent
290460ba9d
commit
66233b0150
10 changed files with 242 additions and 6 deletions
|
|
@ -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::*;
|
||||
|
|
|
|||
Loading…
Reference in a new issue