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
//! A simple XML parser based on quick-xml.
//!
//! This is basically an interface on top of quick-xml. It provides an
//! API to let the user write callback functions for specific parts of
//! the document, and hides the reader from the user.
//!
//! Because of its simple nature, it has a set of limitations:
//!
//! * It only parses from a byte buffer. If the XML is from a file,
//! the user needs to read the file into a buffer before using the
//! parser.
//!
//! * The XML has to be encoded in UTF-8.
//!
//! ## Example
//!
//! ```
//! # use std::str;
//! # use data::xml::Parser;
//! struct A { a: String }
//!
//! let mut aaa: A = A { a: String::new() };
//! let mut iin = 0;
//! let mut out = 0;
//! let mut d = String::new();
//!
//! let mut p = Parser::new();
//! p.addBeginHandler("a", |_, _| {
//!     iin += 1;
//!     Ok(())
//! });
//! p.addEndHandler("a", |_, _| {
//!     out += 1;
//!     Ok(())
//! });
//! p.addTextHandler("a", |_, t| {
//!     aaa.a = t.to_owned();
//!     Ok(())
//! });
//! p.addTagHandler("d", |_, t| {
//!     d = str::from_utf8(t).unwrap().to_owned();
//!     Ok(())
//! });
//! p.parse(r#"
//! <c>
//!     <a>aaa</a>
//!     <b/>
//!     <d>
//!         <e/>
//!     </d>
//! </c>
//! "#.as_bytes()).unwrap();
//! drop(p);
//! assert_eq!(iin, 1);
//! assert_eq!(out, 1);
//! assert_eq!(aaa.a, "aaa".to_owned());
//! assert_eq!(d, r#"<d>
//!         <e/>
//!     </d>"#);
//! ```

use std::str;
use std::collections::HashMap;
use std::str::FromStr;

use quick_xml::events::{Event, BytesEnd, BytesStart};

use error::Error;

type BeginHandler<'a> = Box<dyn FnMut(&[String], &BytesStart) ->
                            Result<(), Error> + 'a>;
type EndHandler<'a> = Box<dyn FnMut(&[String], &BytesEnd) ->
                          Result<(), Error> + 'a>;
type TextHandler<'a> = Box<dyn FnMut(&[String], &str) ->
                           Result<(), Error> + 'a>;
type TagHandler<'a> = Box<dyn FnMut(&[String], &[u8]) ->
                           Result<(), Error> + 'a>;
type BeginHandlerMap<'a> = HashMap<&'static str, BeginHandler<'a>>;
type EndHandlerMap<'a> = HashMap<&'static str, EndHandler<'a>>;
type TextHandlerMap<'a> = HashMap<&'static str, TextHandler<'a>>;
type TagHandlerMap<'a> = HashMap<&'static str, TagHandler<'a>>;

/// A simple event callback-based XML parser.
pub struct Parser<'a>
{
    begin_handlers: BeginHandlerMap<'a>,
    end_handlers: EndHandlerMap<'a>,
    text_handlers: TextHandlerMap<'a>,
    tag_handlers: TagHandlerMap<'a>,
}

impl<'a> Parser<'a>
{
    /// Create a parser with no callbacks.
    pub fn new() -> Self
    {
        Self
        {
            begin_handlers: HashMap::new(),
            end_handlers: HashMap::new(),
            text_handlers: HashMap::new(),
            tag_handlers: HashMap::new(),
        }
    }

    /// Add a callback for an opening tag. If the parser encounters an
    /// opening tag whose name coincides with the value of `tag`, it
    /// calls `handler` with the opening tag event. Self-closing tags
    /// also trigger begin handlers.
    pub fn addBeginHandler<F>(&mut self, tag: &'static str, handler: F)
        where F: FnMut(&[String], &BytesStart) -> Result<(), Error> + 'a
    {
        self.begin_handlers.insert(tag, Box::new(handler));
    }

    /// Add a callback for an end tag. If the parser encounters an end
    /// tag whose name coincides with the value of `tag` (not
    /// including the starting `/`), it calls `handler` with the end
    /// tag event. Self-closing tags also trigger end handlers.
    pub fn addEndHandler<F>(&mut self, tag: &'static str, handler: F)
        where F: FnMut(&[String], &BytesEnd) -> Result<(), Error> + 'a
    {
        self.end_handlers.insert(tag, Box::new(handler));
    }

    /// Add a callback for text element directly inside some tag. If
    /// the parser encounters a text element where it’s enclosing tag
    /// coincides with the value of `tag`, it calls `handler` with the
    /// decoded text string.
    ///
    /// Note that this does not mean the text needs to be the only or
    /// the last element in the enclosing tag.
    pub fn addTextHandler<F>(&mut self, tag: &'static str, handler: F)
        where F: FnMut(&[String], &str) -> Result<(), Error> + 'a
    {
        self.text_handlers.insert(tag, Box::new(handler));
    }

    /// Add a callback for a whole tag. If the parser encounters an
    /// opening element (including self-closing tags) whose name
    /// coincides with the value of `tag`, it calls `handler` with the
    /// content of the whole tag, including the opening and the
    /// closing tag. The parser then skips the whole tag.
    ///
    /// This is useful if the user wants to delegate the parsing of a
    /// tag to another parser.
    pub fn addTagHandler<F>(&mut self, tag: &'static str, handler: F)
        where F: FnMut(&[String], &[u8]) -> Result<(), Error> + 'a
    {
        self.tag_handlers.insert(tag, Box::new(handler));
    }

    /// Parse the XML in the byte buffer `x`, triggering the callbacks
    /// in the process. It is important to note that *this buffer
    /// should only contains one root tag*.
    pub fn parse(&mut self, x: &[u8]) -> Result<(), Error>
    {
        let mut reader = quick_xml::Reader::from_str(unsafe {
            str::from_utf8_unchecked(x)
        });
        let mut path: Vec<String> = Vec::new();
        let mut stop: bool = false;

        while !stop
        {
            let pos_before = reader.buffer_position();
            match reader.read_event() {
                Ok(Event::Start(e)) =>
                {
                    let tag: &str = str::from_utf8(e.name().into_inner())
                        .map_err(
                            |_| xmlerr!("Failed to decode UTF-8 from XML"))?;
                    path.push(tag.to_owned());
                    if let Some(f) = self.begin_handlers.get_mut(tag)
                    {
                        f(&path, &e)?;
                    }

                    if let Some(f) = self.tag_handlers.get_mut(tag)
                    {
                        reader.read_to_end(e.to_end().name()).map_err(
                            |_| xmlerr!("Failed to find end tag of {}.", tag))?;
                        f(&path, &x[pos_before..reader.buffer_position()])?;
                        path.pop();
                    }
                },
                Ok(Event::Empty(e)) =>
                {
                    let tag: &str = str::from_utf8(e.name().into_inner())
                        .map_err(
                            |_| xmlerr!("Failed to decode UTF-8 from XML"))?;
                    path.push(tag.to_owned());
                    if let Some(f) = self.begin_handlers.get_mut(tag)
                    {
                        f(&path, &e)?;
                    }
                    if let Some(f) = self.end_handlers.get_mut(tag)
                    {
                        f(&path, &e.to_end())?;
                    }
                    path.pop();
                    if path.is_empty()
                    {
                        stop = true;
                    }
                },
                Ok(Event::End(e)) =>
                {
                    let tag: &str = str::from_utf8(e.name().into_inner())
                        .map_err(
                            |_| xmlerr!("Failed to decode UTF-8 from XML"))?;
                    if let Some(name) = path.last()
                    {
                        if *name != tag
                        {
                            return Err(
                                xmlerr!("Invalid XML. Expecting {}, got {}.",
                                       name, tag));
                        }
                    }
                    else
                    {
                        return Err(
                            xmlerr!("Invalid XML. XML should end, got {}.",
                                   tag));
                    }

                    if let Some(f) = self.end_handlers.get_mut(tag)
                    {
                        f(&path, &e)?;
                    }
                    path.pop();
                    if path.is_empty()
                    {
                        stop = true;
                    }
                },
                Ok(Event::Text(inner)) =>
                {
                    if let Some(tag) = path.last()
                    {
                        let tag: &str = tag;
                        if let Some(f) = self.text_handlers.get_mut(tag)
                        {
                            let t = str::from_utf8(inner.as_ref()).map_err(
                                |_| xmlerr!("Failed to decode text element in \
                                            {}", tag))?;
                            f(&path, t)?;
                        }
                    }
                    else
                    {
                        // If the XML ends in whitespace, this branch
                        // will trigger, which is fine.
                    }
                },
                Ok(_) => {},
                Err(e) =>
                {
                    return Err(xmlerr!("Failed to parse XML: {}", e));
                },
            }
        }
        Ok(())
    }
}

/// Return the value of the attribute `attr` from the opening tag
/// `tag`.
pub fn getTagAttr<T: FromStr>(tag: &BytesStart, attr: &str) ->
    Result<Option<T>, Error>
{
    if let Some(at) = tag.try_get_attribute(attr)
        .map_err(|_| xmlerr!("Failed to get attribute '{}'.", attr))?
    {
        let value: T = str::from_utf8(at.value.as_ref()).map_err(
            |_| xmlerr!("Failed to decode value of attribute '{}'.", attr))?
            .parse().map_err(
                |_| xmlerr!("Invalid value of attirbute '{}'.", attr))?;
        Ok(Some(value))
    }
    else
    {
        Ok(None)
    }
}

// ========== Tests =================================================>

#[cfg(test)]
mod tests {
    use super::*;

    struct A { a: String }

    #[test]
    fn parsing() -> Result<(), Error>
    {
        let mut aaa: A = A { a: String::new() };
        let mut iin = 0;
        let mut out = 0;
        let mut d = String::new();

        let mut p = Parser::new();
        p.addBeginHandler("a", |_, _| {
            iin += 1;
            Ok(())
        });
        p.addEndHandler("a", |_, _| {
            out += 1;
            Ok(())
        });
        p.addTextHandler("a", |_, t| {
            aaa.a = t.to_owned();
            Ok(())
        });
        p.addTagHandler("d", |_, t| {
            d = str::from_utf8(t).unwrap().to_owned();
            Ok(())
        });
        p.parse(r#"
<c>
    <a>aaa</a>
    <b/>
    <d>
        <e/>
    </d>
</c>
"#.as_bytes())?;
        drop(p);
        assert_eq!(iin, 1);
        assert_eq!(out, 1);
        assert_eq!(aaa.a, "aaa".to_owned());
        assert_eq!(d, r#"<d>
        <e/>
    </d>"#);
        Ok(())
    }
}