use std::borrow::Cow;
use std::ops::Range;
use crate::Reader;
#[derive(Debug)]
pub enum Event<'r, 'a> {
Start(StartElement<'r, 'a>),
End(EndElement<'r, 'a>),
Text(Text<'a>),
Comment(Comment<'a>),
ProcessingInstruction(ProcessingInstruction<'a>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Name<'r, 'a> {
pub prefix: &'a str,
pub local: &'a str,
pub namespace: Option<&'r str>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Attribute<'r, 'a> {
pub name: Name<'r, 'a>,
pub value: &'r Cow<'a, str>,
pub raw: &'a str,
pub range: Range<usize>,
}
#[derive(Clone, Copy)]
pub struct StartElement<'r, 'a> {
pub(crate) reader: &'r Reader<'a>,
}
impl<'r, 'a> StartElement<'r, 'a> {
#[must_use]
pub fn name(&self) -> Name<'r, 'a> {
current_name(self.reader)
}
pub fn attributes(&self) -> impl ExactSizeIterator<Item = Attribute<'r, 'a>> + use<'r, 'a> {
let reader = self.reader;
reader
.attributes
.iter()
.map(move |data| reader.attribute_view(data))
}
#[must_use]
pub fn attribute(&self, namespace: Option<&str>, local: &str) -> Option<&'r Cow<'a, str>> {
let reader = self.reader;
reader
.attributes
.iter()
.find(|data| data.local == local && reader.namespace(data.ns) == namespace)
.map(|data| &data.value)
}
pub fn namespace_declarations(
&self,
) -> impl ExactSizeIterator<Item = (&'a str, &'r str)> + use<'r, 'a> {
let scope = self.reader.stack.last().map_or(0, |open| open.scope);
self.reader.scope.since(scope)
}
#[must_use]
pub fn lookup_namespace(&self, prefix: &str) -> Option<&'r str> {
self.reader.lookup_namespace(prefix)
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.reader.empty
}
#[must_use]
pub fn range(&self) -> Range<usize> {
self.reader.tag.clone()
}
}
impl std::fmt::Debug for StartElement<'_, '_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StartElement")
.field("name", &self.name())
.field("attributes", &self.attributes().collect::<Vec<_>>())
.field("empty", &self.is_empty())
.finish()
}
}
#[derive(Clone, Copy)]
pub struct EndElement<'r, 'a> {
pub(crate) reader: &'r Reader<'a>,
}
impl<'r, 'a> EndElement<'r, 'a> {
#[must_use]
pub fn name(&self) -> Name<'r, 'a> {
current_name(self.reader)
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.reader.empty
}
#[must_use]
pub fn range(&self) -> Range<usize> {
self.reader.tag.clone()
}
}
impl std::fmt::Debug for EndElement<'_, '_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EndElement")
.field("name", &self.name())
.finish()
}
}
fn current_name<'r, 'a>(reader: &'r Reader<'a>) -> Name<'r, 'a> {
match reader.stack.last() {
Some(open) => reader.name_of(open.prefix, open.local, open.ns),
None => Name {
prefix: "",
local: "",
namespace: None,
},
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Text<'a> {
pub value: Cow<'a, str>,
pub range: Range<usize>,
pub cdata: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Comment<'a> {
pub value: Cow<'a, str>,
pub range: Range<usize>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ProcessingInstruction<'a> {
pub target: &'a str,
pub value: Option<Cow<'a, str>>,
pub range: Range<usize>,
}