1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
pub mod attributes;
use std::borrow::Cow;
use std::str::from_utf8;
use std::ops::Deref;
use encoding_rs::Encoding;
use std::io::BufRead;
use escape::unescape;
use self::attributes::{Attributes, Attribute};
use errors::Result;
use reader::Reader;
#[derive(Clone, Debug)]
pub struct BytesStart<'a> {
buf: Cow<'a, [u8]>,
name_len: usize
}
impl<'a> BytesStart<'a> {
#[inline]
pub fn borrowed(content: &'a[u8], name_len: usize) -> BytesStart<'a> {
BytesStart {
buf: Cow::Borrowed(content),
name_len: name_len,
}
}
#[inline]
pub fn owned(content: Vec<u8>, name_len: usize) -> BytesStart<'static> {
BytesStart {
buf: Cow::Owned(content),
name_len: name_len,
}
}
pub fn into_owned(self) -> BytesStart<'static> {
BytesStart {
buf: Cow::Owned(self.buf.into_owned()),
name_len: self.name_len,
}
}
pub fn with_attributes<'b, I>(&mut self, attributes: I) -> &mut Self
where I: IntoIterator,
I::Item: Into<Attribute<'b>>,
{
self.extend_attributes(attributes);
self
}
pub fn name(&self) -> &[u8] {
&self.buf[..self.name_len]
}
pub fn unescaped(&self) -> Result<Cow<[u8]>> {
unescape(&*self.buf)
}
pub fn attributes(&self) -> Attributes {
Attributes::new(&self, self.name_len)
}
pub fn extend_attributes<'b, I>(&mut self, attributes: I) -> &mut BytesStart<'a>
where I: IntoIterator,
I::Item: Into<Attribute<'b>>,
{
for attr in attributes {
self.push_attribute(attr);
}
self
}
pub fn unescape_and_decode<B: BufRead>(&self, reader: &Reader<B>) -> Result<String> {
self.unescaped().map(|e| reader.decode(&*e).into_owned())
}
pub fn push_attribute<'b, A: Into<Attribute<'b>>>(&mut self, attr: A)
{
let a = attr.into();
let bytes = self.buf.to_mut();
bytes.push(b' ');
bytes.extend_from_slice(a.key);
bytes.extend_from_slice(b"=\"");
bytes.extend_from_slice(a.value);
bytes.push(b'"');
}
}
#[derive(Clone, Debug)]
pub struct BytesDecl<'a> {
element: BytesStart<'a>,
}
impl<'a> BytesDecl<'a> {
pub fn from_start(start: BytesStart<'a>) -> BytesDecl<'a> {
BytesDecl { element: start }
}
pub fn version(&self) -> Result<&[u8]> {
match self.element.attributes().next() {
Some(Err(e)) => Err(e),
Some(Ok(Attribute { key: b"version", value: v})) => Ok(v),
Some(Ok(a)) => Err(format!("XmlDecl must start with 'version' attribute, \
found {:?}", from_utf8(a.key)).into()),
None => Err("XmlDecl must start with 'version' attribute, found none".into()),
}
}
pub fn encoding(&self) -> Option<Result<&[u8]>> {
for a in self.element.attributes() {
match a {
Err(e) => return Some(Err(e)),
Ok(Attribute { key: b"encoding", value: v}) => return Some(Ok(v)),
_ => (),
}
}
None
}
pub fn standalone(&self) -> Option<Result<&[u8]>> {
for a in self.element.attributes() {
match a {
Err(e) => return Some(Err(e)),
Ok(Attribute { key: b"standalone", value: v }) => return Some(Ok(v)),
_ => (),
}
}
None
}
pub fn new(version: &[u8], encoding: Option<&[u8]>, standalone: Option<&[u8]>) -> BytesDecl<'static> {
let encoding_attr_len = if let Some(xs) = encoding { 12 + xs.len() } else { 0 };
let standalone_attr_len = if let Some(xs) = standalone { 14 + xs.len() } else { 0 };
let mut buf = Vec::with_capacity(14 + encoding_attr_len + standalone_attr_len);
buf.extend_from_slice(b"xml version=\"");
buf.extend_from_slice(version);
if let Some(encoding_val) = encoding {
buf.extend_from_slice(b"\" encoding=\"");
buf.extend_from_slice(encoding_val);
}
if let Some(standalone_val) = standalone {
buf.extend_from_slice(b"\" standalone=\"");
buf.extend_from_slice(standalone_val);
}
buf.push(b'"');
BytesDecl { element: BytesStart::owned(buf, 3) }
}
pub fn encoder(&self) -> Option<&'static Encoding> {
self.encoding()
.and_then(|e| e.ok())
.and_then(|e| Encoding::for_label(e))
}
}
#[derive(Clone, Debug)]
pub struct BytesEnd<'a> {
name: Cow<'a, [u8]>
}
impl<'a> BytesEnd<'a> {
#[inline]
pub fn borrowed(name: &'a [u8]) -> BytesEnd<'a> {
BytesEnd { name: Cow::Borrowed(name) }
}
#[inline]
pub fn owned(name: Vec<u8>) -> BytesEnd<'static> {
BytesEnd { name: Cow::Owned(name) }
}
#[inline]
pub fn name(&self) -> &[u8] {
&*self.name
}
}
#[derive(Clone, Debug)]
pub struct BytesText<'a> {
content: Cow<'a, [u8]>
}
impl<'a> BytesText<'a> {
#[inline]
pub fn borrowed(content: &'a [u8]) -> BytesText<'a> {
BytesText { content: Cow::Borrowed(content) }
}
#[inline]
pub fn owned(content: Vec<u8>) -> BytesText<'static> {
BytesText { content: Cow::Owned(content) }
}
pub fn unescaped(&self) -> Result<Cow<[u8]>> {
unescape(&self)
}
pub fn unescape_and_decode<B: BufRead>(&self, reader: &Reader<B>) -> Result<String> {
self.unescaped().map(|e| reader.decode(&*e).into_owned())
}
}
#[derive(Clone, Debug)]
pub enum Event<'a> {
Start(BytesStart<'a>),
End(BytesEnd<'a>),
Empty(BytesStart<'a>),
Text(BytesText<'a>),
Comment(BytesText<'a>),
CData(BytesText<'a>),
Decl(BytesDecl<'a>),
PI(BytesText<'a>),
DocType(BytesText<'a>),
Eof,
}
impl<'a> Deref for BytesStart<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
&*self.buf
}
}
impl<'a> Deref for BytesDecl<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
&*self.element
}
}
impl<'a> Deref for BytesEnd<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
&*self.name
}
}
impl<'a> Deref for BytesText<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
&*self.content
}
}
impl<'a> Deref for Event<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
match *self {
Event::Start(ref e) => &*e,
Event::End(ref e) => &*e,
Event::Text(ref e) => &*e,
Event::Empty(ref e) => &*e,
Event::Decl(ref e) => &*e,
Event::PI(ref e) => &*e,
Event::CData(ref e) => &*e,
Event::Comment(ref e) => &*e,
Event::DocType(ref e) => &*e,
Event::Eof => &[],
}
}
}