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
//! # Functions to expect exactly one event
use {
    quick_xml::{
        events::{BytesEnd as XmlBytesEnd, BytesStart as XmlBytesStart, Event as XmlEvent},
        Error as XmlError, Reader as XmlReader,
    },
    std::{error::Error as StdError, io::BufRead, str::FromStr},
};

use displaydoc::Display;
use quick_xml::events::attributes::AttrError;
use thiserror::Error;

/// The kind of an XML event
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum XmlEventKind {
    /// Start Tag `<tag>`
    Start,
    /// End Tag `</tag>`
    End,
    /// Empty tag `<tag/>`
    Empty,
    /// Plain Text
    Text,
    /// Comment `<!-- ... -->`
    Comment,
    /// Literal Character data <![CDATA[ ... ]]>
    CData,
    /// XML Declaration `<?xml ... ?>`
    Decl,
    /// Processing instruction `<?xyz ... ?>`
    PI,
    /// Document type declaration `<!DOCTYPE xyz>`
    DocType,
    /// End of file
    Eof,
}

impl From<&XmlEvent<'_>> for XmlEventKind {
    fn from(e: &XmlEvent) -> Self {
        match e {
            XmlEvent::Start(_) => Self::Start,
            XmlEvent::End(_) => Self::End,
            XmlEvent::Empty(_) => Self::Empty,
            XmlEvent::Text(_) => Self::Text,
            XmlEvent::Comment(_) => Self::Comment,
            XmlEvent::CData(_) => Self::CData,
            XmlEvent::Decl(_) => Self::Decl,
            XmlEvent::PI(_) => Self::PI,
            XmlEvent::DocType(_) => Self::DocType,
            XmlEvent::Eof => Self::Eof,
        }
    }
}

/// The errors for this module
#[derive(Debug, Display, Error)]
#[non_exhaustive]
pub enum Error {
    /// Malformed XML
    Xml(#[from] XmlError),
    /// Malformed XML Attribute
    XmlAttr(#[from] AttrError),
    /// Generic
    Generic(#[from] Box<dyn StdError + Send + Sync>),
    /// Expected tag `{0}`, found `{1:?}`
    ExpectedTag(String, String),
    /// Expected end tag `{0}`, found `{1:?}`
    ExpectedEndTag(String, String),
    /// Expected <{0}> or </{1}>, but found {2:?}
    ExpectedStartEndTag(&'static str, &'static str, XmlEventKind),
    /// Missing tag `{0}`
    MissingTag(String),
    /// Missing end tag `{0}`
    MissingEndTag(String),
    /// Missing text or end tag `{0}`
    MissingTextOrEndTag(String),
    /// Missing text
    MissingText,
    /// Missing Attribute `{0}`
    MissingAttribute(String),
    /// Expected Attribute `{0}`, found `{1:?}`
    ExpectedAttribute(String, String),
}

/// The result type for this module
pub type Result<T> = std::result::Result<T, Error>;

/// Check whether a start tag matches the provided `key`
fn check_start<'a, B: BufRead>(
    e: XmlBytesStart<'a>,
    key: &str,
    reader: &XmlReader<B>,
) -> Result<XmlBytesStart<'a>> {
    if e.name() == key.as_bytes() {
        Ok(e)
    } else {
        Err(Error::ExpectedTag(
            key.to_owned(),
            reader.decode(e.name()).into_owned(),
        ))
    }
}

/// Check whether an end tag matches the provided `key`
fn check_end<'a, B: BufRead>(
    e: XmlBytesEnd<'a>,
    key: &str,
    reader: &XmlReader<B>,
) -> Result<XmlBytesEnd<'a>> {
    if e.name() == key.as_bytes() {
        Ok(e)
    } else {
        Err(Error::ExpectedEndTag(
            key.to_owned(),
            reader.decode(e.name()).into_owned(),
        ))
    }
}

/// Expect an opening tag and return it
pub fn expect_start<'c, B: BufRead>(
    key: &str,
    reader: &mut XmlReader<B>,
    buf: &'c mut Vec<u8>,
) -> Result<XmlBytesStart<'c>> {
    if let Ok(XmlEvent::Start(e)) = reader.read_event(buf) {
        check_start(e, key, reader)
    } else {
        Err(Error::MissingTag(key.to_owned()))
    }
}

/// Expect a closing tag and return it
pub fn expect_end<'c, B: BufRead>(
    key: &str,
    reader: &mut XmlReader<B>,
    buf: &'c mut Vec<u8>,
) -> Result<XmlBytesEnd<'c>> {
    if let Ok(XmlEvent::End(e)) = reader.read_event(buf) {
        check_end(e, key, reader)
    } else {
        Err(Error::MissingEndTag(key.to_owned()))
    }
}

/// Expect an
pub fn expect_child_or_end<'a, B: BufRead>(
    start_key: &'static str,
    end_key: &'static str,
    reader: &mut XmlReader<B>,
    buf: &'a mut Vec<u8>,
) -> Result<Option<XmlBytesStart<'a>>> {
    match reader.read_event(buf)? {
        XmlEvent::Start(s) => check_start(s, start_key, reader).map(Some),
        XmlEvent::End(e) => {
            check_end(e, end_key, reader)?;
            Ok(None)
        }
        e => Err(Error::ExpectedStartEndTag(
            start_key,
            end_key,
            XmlEventKind::from(&e),
        )),
    }
}

/// Expect some text and return it
pub fn expect_text<B: BufRead>(reader: &mut XmlReader<B>, buf: &mut Vec<u8>) -> Result<String> {
    if let Ok(XmlEvent::Text(e)) = reader.read_event(buf) {
        let text = e.unescape_and_decode(reader)?;
        Ok(text)
    } else {
        Err(Error::MissingText)
    }
}

/// Expect either a text node or an end tag
pub fn expect_text_or_end<B: BufRead>(
    key: &str,
    reader: &mut XmlReader<B>,
    buf: &mut Vec<u8>,
) -> Result<String> {
    match reader.read_event(buf)? {
        XmlEvent::Text(t) => {
            let text = t.unescape_and_decode(reader)?;
            buf.clear();
            expect_end(key, reader, buf)?;
            Ok(text)
        }
        XmlEvent::End(e) => {
            check_end(e, key, reader)?;
            Ok(String::new())
        }
        _ => Err(Error::MissingTextOrEndTag(key.to_string())),
    }
}

/// Expect an attribute on an opening tag and return a parsed value
pub fn expect_attribute<T: FromStr, B: BufRead>(
    key: &str,
    reader: &XmlReader<B>,
    event: &XmlBytesStart,
) -> Result<T>
where
    <T as FromStr>::Err: std::error::Error + Send + Sync + Sized + 'static,
{
    let attr = event
        .attributes()
        .next()
        .ok_or_else(|| Error::MissingAttribute(key.to_owned()))??;

    if attr.key == key.as_bytes() {
        let attr_unesc = attr.unescaped_value()?;
        let attr_str = reader.decode(&attr_unesc);
        let value = attr_str.parse().map_err(|e| {
            let b: Box<dyn StdError + Sync + Send> = Box::new(e);
            b
        })?;
        Ok(value)
    } else {
        Err(Error::ExpectedAttribute(
            key.to_owned(),
            reader.decode(attr.key).into_owned(),
        ))
    }
}