1mod name;
11mod parse;
12mod text;
13mod utf16;
14mod write;
15
16use core::ops::Range;
17use std::borrow::Cow;
18
19pub use parse::read;
20pub use write::{Edit, NewElement, write};
21
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub enum Encoding {
26 Utf8,
28 ShiftJis,
30 Utf16Le,
32 Utf16Be,
34}
35
36#[derive(Clone, Debug, PartialEq, Eq)]
38pub enum Namespace {
39 Declared(Range<usize>),
41 Xml,
43 Xmlns,
45}
46
47#[derive(Clone, Debug, PartialEq, Eq)]
49pub struct Name {
50 raw: Range<usize>,
51 local: usize,
52 prefix_end: Option<usize>,
53 namespace: Option<Namespace>,
54}
55
56impl Name {
57 pub fn raw(&self) -> Range<usize> {
59 self.raw.clone()
60 }
61
62 pub fn prefix(&self) -> Option<Range<usize>> {
64 self.prefix_end.map(|end| self.raw.start..end)
65 }
66
67 pub fn local(&self) -> Range<usize> {
69 self.local..self.raw.end
70 }
71
72 pub fn namespace(&self) -> Option<&Namespace> {
74 self.namespace.as_ref()
75 }
76}
77
78#[derive(Clone, Debug, PartialEq, Eq)]
80pub struct Attribute {
81 name: Name,
82 value: Range<usize>,
83}
84
85impl Attribute {
86 pub fn name(&self) -> &Name {
88 &self.name
89 }
90
91 pub fn raw(&self) -> Range<usize> {
93 self.value.clone()
94 }
95}
96
97#[derive(Clone, Debug, PartialEq, Eq)]
99pub enum Node {
100 Element(usize),
102 Text(Range<usize>),
104 CData(Range<usize>),
106}
107
108#[derive(Clone, Debug, PartialEq, Eq)]
110pub struct Element {
111 span: Range<usize>,
112 name: Name,
113 attributes: Vec<Attribute>,
114 children: Vec<Node>,
115 parent: Option<usize>,
116}
117
118impl Element {
119 pub fn span(&self) -> Range<usize> {
121 self.span.clone()
122 }
123
124 pub fn name(&self) -> &Name {
126 &self.name
127 }
128
129 pub fn attributes(&self) -> &[Attribute] {
131 &self.attributes
132 }
133
134 pub fn children(&self) -> &[Node] {
136 &self.children
137 }
138
139 pub fn parent(&self) -> Option<usize> {
141 self.parent
142 }
143}
144
145#[derive(Clone, Debug, PartialEq, Eq)]
147pub struct Document {
148 src: Vec<u8>,
149 encoding: Encoding,
150 elements: Vec<Element>,
151 doctype: Option<Range<usize>>,
152 decoded: Option<utf16::Decoded>,
153}
154
155impl Document {
156 pub fn encoding(&self) -> Encoding {
158 self.encoding
159 }
160
161 pub fn src(&self) -> &[u8] {
163 &self.src
164 }
165
166 pub fn bytes(&self, range: Range<usize>) -> &[u8] {
168 self.src.get(range).unwrap_or_default()
169 }
170
171 pub fn root(&self) -> usize {
173 0
174 }
175
176 pub fn element(&self, index: usize) -> Option<&Element> {
178 self.elements.get(index)
179 }
180
181 pub fn elements(&self) -> &[Element] {
183 &self.elements
184 }
185
186 pub fn doctype(&self) -> Option<Range<usize>> {
188 self.doctype.clone()
189 }
190
191 pub fn local_name(&self, name: &Name) -> &[u8] {
193 self.bytes(name.local())
194 }
195
196 pub fn parsed_local_name(&self, name: &Name) -> &[u8] {
199 match &self.decoded {
200 Some(decoded) => decoded
201 .bytes
202 .get(decoded.decoded_range(name.local()))
203 .unwrap_or_default(),
204 None => self.local_name(name),
205 }
206 }
207
208 pub fn local_name_text(&self, name: &Name) -> Cow<'_, str> {
210 if let Some(decoded) = &self.decoded {
211 return String::from_utf8_lossy(
212 decoded
213 .bytes
214 .get(decoded.decoded_range(name.local()))
215 .unwrap_or_default(),
216 );
217 }
218 match self.encoding {
219 Encoding::Utf8 => String::from_utf8_lossy(self.local_name(name)),
220 Encoding::ShiftJis => {
221 encoding_rs::SHIFT_JIS
222 .decode_without_bom_handling(self.local_name(name))
223 .0
224 }
225 Encoding::Utf16Le | Encoding::Utf16Be => Cow::Owned(self.text(name.local())),
226 }
227 }
228
229 pub fn namespace(&self, name: &Name) -> Option<String> {
231 match name.namespace()? {
232 Namespace::Declared(range) => Some(self.value_of(range.clone())),
233 Namespace::Xml => Some(String::from("http://www.w3.org/XML/1998/namespace")),
234 Namespace::Xmlns => Some(String::from("http://www.w3.org/2000/xmlns/")),
235 }
236 }
237
238 pub fn is(&self, name: &Name, namespace: Option<&str>, local: &[u8]) -> bool {
241 self.parsed_local_name(name) == local && self.namespace(name).as_deref() == namespace
242 }
243
244 pub fn text(&self, range: Range<usize>) -> String {
247 text::decode(self.encoding, self.bytes(range), text::Mode::Text)
248 }
249
250 pub fn cdata(&self, range: Range<usize>) -> String {
252 text::decode(self.encoding, self.bytes(range), text::Mode::CData)
253 }
254
255 pub fn value(&self, attribute: &Attribute) -> String {
257 self.value_of(attribute.raw())
258 }
259
260 fn value_of(&self, range: Range<usize>) -> String {
261 text::decode(self.encoding, self.bytes(range), text::Mode::Attribute)
262 }
263
264 pub fn attribute<'e>(
266 &self,
267 element: &'e Element,
268 namespace: Option<&str>,
269 local: &[u8],
270 ) -> Option<&'e Attribute> {
271 element
272 .attributes
273 .iter()
274 .find(|attribute| self.is(&attribute.name, namespace, local))
275 }
276}