use alloc::borrow::Cow;
use alloc::string::String;
use alloc::vec::Vec;
use crate::tree::ExpandedName;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Event {
#[non_exhaustive]
StartElement {
name: ExpandedName,
prefix: Option<String>,
attributes: Vec<(ExpandedName, String)>,
namespaces: Vec<(Option<String>, String)>,
},
#[non_exhaustive]
EndElement {
name: ExpandedName,
prefix: Option<String>,
},
Text(String),
Comment(String),
ProcessingInstruction {
target: String,
data: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum BorrowedEvent<'a> {
#[non_exhaustive]
StartElement {
name: ExpandedName,
prefix: Option<Cow<'a, str>>,
attributes: Vec<(ExpandedName, Cow<'a, str>)>,
namespaces: Vec<(Option<String>, String)>,
},
#[non_exhaustive]
EndElement {
name: ExpandedName,
prefix: Option<Cow<'a, str>>,
},
Text(Cow<'a, str>),
Comment(Cow<'a, str>),
ProcessingInstruction {
target: Cow<'a, str>,
data: Cow<'a, str>,
},
}
impl BorrowedEvent<'_> {
#[must_use]
pub fn into_owned(self) -> Event {
match self {
Self::StartElement {
name,
prefix,
attributes,
namespaces,
} => Event::StartElement {
name,
prefix: prefix.map(Cow::into_owned),
attributes: attributes
.into_iter()
.map(|(n, v)| (n, v.into_owned()))
.collect(),
namespaces,
},
Self::EndElement { name, prefix } => Event::EndElement {
name,
prefix: prefix.map(Cow::into_owned),
},
Self::Text(text) => Event::Text(text.into_owned()),
Self::Comment(comment) => Event::Comment(comment.into_owned()),
Self::ProcessingInstruction { target, data } => {
Event::ProcessingInstruction {
target: target.into_owned(),
data: data.into_owned(),
}
}
}
}
}
impl From<BorrowedEvent<'_>> for Event {
fn from(b: BorrowedEvent<'_>) -> Self {
b.into_owned()
}
}