1use std::borrow::Cow;
4use std::ops::Range;
5
6use crate::Reader;
7
8#[derive(Debug)]
12pub enum Event<'r, 'a> {
13 Start(StartElement<'r, 'a>),
16 End(EndElement<'r, 'a>),
18 Text(Text<'a>),
22 Comment(Comment<'a>),
24 ProcessingInstruction(ProcessingInstruction<'a>),
26}
27
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
30pub struct Name<'r, 'a> {
31 pub prefix: &'a str,
33 pub local: &'a str,
35 pub namespace: Option<&'r str>,
37}
38
39#[derive(Clone, Debug, PartialEq, Eq)]
41pub struct Attribute<'r, 'a> {
42 pub name: Name<'r, 'a>,
44 pub value: &'r Cow<'a, str>,
47 pub raw: &'a str,
49 pub range: Range<usize>,
51}
52
53#[derive(Clone, Copy)]
55pub struct StartElement<'r, 'a> {
56 pub(crate) reader: &'r Reader<'a>,
57}
58
59impl<'r, 'a> StartElement<'r, 'a> {
60 #[must_use]
62 pub fn name(&self) -> Name<'r, 'a> {
63 current_name(self.reader)
64 }
65
66 pub fn attributes(&self) -> impl ExactSizeIterator<Item = Attribute<'r, 'a>> + use<'r, 'a> {
68 let reader = self.reader;
69 reader
70 .attributes
71 .iter()
72 .map(move |data| reader.attribute_view(data))
73 }
74
75 #[must_use]
77 pub fn attribute(&self, namespace: Option<&str>, local: &str) -> Option<&'r Cow<'a, str>> {
78 let reader = self.reader;
79 reader
80 .attributes
81 .iter()
82 .find(|data| data.local == local && reader.namespace(data.ns) == namespace)
83 .map(|data| &data.value)
84 }
85
86 pub fn namespace_declarations(
90 &self,
91 ) -> impl ExactSizeIterator<Item = (&'a str, &'r str)> + use<'r, 'a> {
92 let scope = self.reader.stack.last().map_or(0, |open| open.scope);
93 self.reader.scope.since(scope)
94 }
95
96 #[must_use]
99 pub fn lookup_namespace(&self, prefix: &str) -> Option<&'r str> {
100 self.reader.lookup_namespace(prefix)
101 }
102
103 #[must_use]
105 pub fn is_empty(&self) -> bool {
106 self.reader.empty
107 }
108
109 #[must_use]
111 pub fn range(&self) -> Range<usize> {
112 self.reader.tag.clone()
113 }
114}
115
116impl std::fmt::Debug for StartElement<'_, '_> {
117 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118 f.debug_struct("StartElement")
119 .field("name", &self.name())
120 .field("attributes", &self.attributes().collect::<Vec<_>>())
121 .field("empty", &self.is_empty())
122 .finish()
123 }
124}
125
126#[derive(Clone, Copy)]
128pub struct EndElement<'r, 'a> {
129 pub(crate) reader: &'r Reader<'a>,
130}
131
132impl<'r, 'a> EndElement<'r, 'a> {
133 #[must_use]
135 pub fn name(&self) -> Name<'r, 'a> {
136 current_name(self.reader)
137 }
138
139 #[must_use]
141 pub fn is_empty(&self) -> bool {
142 self.reader.empty
143 }
144
145 #[must_use]
149 pub fn lookup_namespace(&self, prefix: &str) -> Option<&'r str> {
150 self.reader.lookup_namespace(prefix)
151 }
152
153 #[must_use]
155 pub fn range(&self) -> Range<usize> {
156 self.reader.tag.clone()
157 }
158}
159
160impl std::fmt::Debug for EndElement<'_, '_> {
161 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
162 f.debug_struct("EndElement")
163 .field("name", &self.name())
164 .finish()
165 }
166}
167
168fn current_name<'r, 'a>(reader: &'r Reader<'a>) -> Name<'r, 'a> {
169 match reader.stack.last() {
170 Some(open) => reader.name_of(open.prefix, open.local, open.ns),
171 None => Name {
172 prefix: "",
173 local: "",
174 namespace: None,
175 },
176 }
177}
178
179#[derive(Clone, Debug, PartialEq, Eq)]
181#[non_exhaustive]
182pub struct Text<'a> {
183 pub value: Cow<'a, str>,
186 pub range: Range<usize>,
188 pub cdata: bool,
190}
191
192#[derive(Clone, Debug, PartialEq, Eq)]
194#[non_exhaustive]
195pub struct Comment<'a> {
196 pub value: Cow<'a, str>,
198 pub range: Range<usize>,
200}
201
202#[derive(Clone, Debug, PartialEq, Eq)]
204#[non_exhaustive]
205pub struct ProcessingInstruction<'a> {
206 pub target: &'a str,
208 pub value: Option<Cow<'a, str>>,
211 pub range: Range<usize>,
213}