Handle empty elements

This commit is contained in:
Dirkjan Ochtman 2022-11-23 15:37:29 -08:00
parent 53d882e067
commit 2f587240cf
2 changed files with 39 additions and 2 deletions

View File

@ -14,7 +14,9 @@ pub struct Deserializer<'cx, 'xml> {
impl<'cx, 'xml> Deserializer<'cx, 'xml> { impl<'cx, 'xml> Deserializer<'cx, 'xml> {
pub(crate) fn new(element: Element<'xml>, context: &'cx mut Context<'xml>) -> Self { pub(crate) fn new(element: Element<'xml>, context: &'cx mut Context<'xml>) -> Self {
let level = context.stack.len(); let level = context.stack.len();
context.stack.push(element.level); if !element.empty {
context.stack.push(element.level);
}
Self { Self {
local: element.local, local: element.local,
@ -233,6 +235,7 @@ impl<'xml> Iterator for Context<'xml> {
prefix: level.prefix, prefix: level.prefix,
default_ns: level.default_ns, default_ns: level.default_ns,
level, level,
empty: false,
}; };
return Some(Ok(Node::Open(element))); return Some(Ok(Node::Open(element)));
@ -261,7 +264,29 @@ impl<'xml> Iterator for Context<'xml> {
} }
} }
ElementEnd::Empty => { ElementEnd::Empty => {
todo!(); let level = match current {
Some(level) => level,
None => {
return Some(Err(Error::UnexpectedState(
"opening element with no parent",
)))
}
};
self.records.push_back(Node::Close {
prefix: level.prefix,
local: level.local,
});
let element = Element {
local: level.local,
prefix: level.prefix,
default_ns: level.default_ns,
level,
empty: true,
};
return Some(Ok(Node::Open(element)));
} }
}, },
Ok(Token::Attribute { Ok(Token::Attribute {
@ -331,6 +356,7 @@ pub struct Element<'xml> {
default_ns: Option<&'xml str>, default_ns: Option<&'xml str>,
prefix: Option<&'xml str>, prefix: Option<&'xml str>,
level: Level<'xml>, level: Level<'xml>,
empty: bool,
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -20,3 +20,14 @@ fn basic() {
"<Basic flag=\"true\"></Basic>" "<Basic flag=\"true\"></Basic>"
); );
} }
#[derive(Debug, Eq, FromXml, PartialEq)]
struct Empty;
#[test]
fn empty() {
assert_eq!(
from_str::<Empty>("<?xml version=\"1.0\" ?><Empty />"),
Ok(Empty)
);
}