use std::borrow::Cow;
use crate::trace;
use crate::{DomDeserializeError, DomEvent, DomParser};
pub trait DomParserExt<'de>: DomParser<'de> {
fn next_event_or_eof(
&mut self,
expected: &'static str,
) -> Result<DomEvent<'de>, DomDeserializeError<Self::Error>> {
let event = self
.next_event()
.map_err(DomDeserializeError::Parser)?
.ok_or(DomDeserializeError::UnexpectedEof { expected })?;
trace!(event = %event.trace(), kind = %"next");
Ok(event)
}
fn peek_event_or_eof(
&mut self,
expected: &'static str,
) -> Result<&DomEvent<'de>, DomDeserializeError<Self::Error>> {
let event = self
.peek_event()
.map_err(DomDeserializeError::Parser)?
.ok_or(DomDeserializeError::UnexpectedEof { expected })?;
trace!(event = %event.trace(), kind = %"peek");
Ok(event)
}
fn expect_node_start(&mut self) -> Result<Cow<'de, str>, DomDeserializeError<Self::Error>> {
match self.next_event_or_eof("NodeStart")? {
DomEvent::NodeStart { tag, .. } => Ok(tag),
other => Err(DomDeserializeError::TypeMismatch {
expected: "NodeStart",
got: format!("{other:?}"),
}),
}
}
fn expect_children_start(&mut self) -> Result<(), DomDeserializeError<Self::Error>> {
match self.next_event_or_eof("ChildrenStart")? {
DomEvent::ChildrenStart => Ok(()),
other => Err(DomDeserializeError::TypeMismatch {
expected: "ChildrenStart",
got: format!("{other:?}"),
}),
}
}
fn expect_children_end(&mut self) -> Result<(), DomDeserializeError<Self::Error>> {
match self.next_event_or_eof("ChildrenEnd")? {
DomEvent::ChildrenEnd => Ok(()),
other => Err(DomDeserializeError::TypeMismatch {
expected: "ChildrenEnd",
got: format!("{other:?}"),
}),
}
}
fn expect_node_end(&mut self) -> Result<(), DomDeserializeError<Self::Error>> {
match self.next_event_or_eof("NodeEnd")? {
DomEvent::NodeEnd => Ok(()),
other => Err(DomDeserializeError::TypeMismatch {
expected: "NodeEnd",
got: format!("{other:?}"),
}),
}
}
fn expect_text(&mut self) -> Result<Cow<'de, str>, DomDeserializeError<Self::Error>> {
match self.next_event_or_eof("Text")? {
DomEvent::Text(text) => Ok(text),
other => Err(DomDeserializeError::TypeMismatch {
expected: "Text",
got: format!("{other:?}"),
}),
}
}
fn expect_attribute(
&mut self,
) -> Result<AttributeRecord<'de>, DomDeserializeError<Self::Error>> {
match self.next_event_or_eof("Attribute")? {
DomEvent::Attribute {
name,
value,
namespace,
} => Ok(AttributeRecord {
name,
value,
namespace,
}),
other => Err(DomDeserializeError::TypeMismatch {
expected: "Attribute",
got: format!("{other:?}"),
}),
}
}
fn expect_comment(&mut self) -> Result<Cow<'de, str>, DomDeserializeError<Self::Error>> {
match self.next_event_or_eof("Comment")? {
DomEvent::Comment(text) => Ok(text),
other => Err(DomDeserializeError::TypeMismatch {
expected: "Comment",
got: format!("{other:?}"),
}),
}
}
}
pub struct AttributeRecord<'de> {
pub name: Cow<'de, str>,
pub value: Cow<'de, str>,
pub namespace: Option<Cow<'de, str>>,
}
impl<'de, P: DomParser<'de>> DomParserExt<'de> for P {}