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
//! # Code that's used in all XML modules

use std::{io::BufRead, todo};

use displaydoc::Display;
use quick_xml::{
    events::{attributes::AttrError, Event},
    Reader,
};
use thiserror::Error;

pub mod exact;

/// A general error type
#[derive(Debug, Display, Error)]
pub enum XmlError {
    /// Failed to read the next XML event
    Reader(#[from] quick_xml::Error),
    /// XML Attribute parsing error
    XmlAttr(#[from] AttrError),
    /// Reached EOF while searching for {0}
    EofWhileExpecting(&'static str),
    /// Expected <?xml declaration
    ExpectedDecl,
}

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

/// Expect an `<?xml …` declaration
pub fn expect_decl<B: BufRead>(xml: &mut Reader<B>, buf: &mut Vec<u8>) -> Result<()> {
    if let Event::Decl(_) = xml.read_event(buf)? {
        buf.clear();
        Ok(())
    } else {
        Err(XmlError::ExpectedDecl)
    }
}

/// Expect an opening tag `<{key} name="…">`
pub fn expect_named_elem<B: BufRead>(
    xml: &mut Reader<B>,
    buf: &mut Vec<u8>,
    key: &'static str,
    parent: Option<&'static str>,
) -> Result<Option<String>> {
    match xml.read_event(buf)? {
        Event::Start(start) => {
            if start.name() == key.as_bytes() {
                let mut name = String::new();
                for attr in start.attributes() {
                    let attr = attr?;
                    if attr.key == b"name" {
                        name = xml.decode(&attr.value).into_owned();
                        break;
                    }
                }
                buf.clear();
                Ok(Some(name))
            } else {
                todo!();
            }
        }
        Event::End(e) => {
            assert_eq!(e.name(), parent.unwrap().as_bytes());
            buf.clear();
            Ok(None)
        }
        Event::Eof => Err(XmlError::EofWhileExpecting(key)),
        _ => panic!(),
    }
}

/// Expect an opening tag `<{key}>`
pub fn expect_elem<B: BufRead>(
    xml: &mut Reader<B>,
    buf: &mut Vec<u8>,
    key: &'static str,
) -> Result<()> {
    if let Event::Start(start) = xml.read_event(buf)? {
        if start.name() == key.as_bytes() {
            buf.clear();
            Ok(())
        } else {
            todo!();
        }
    } else {
        todo!()
    }
}

/// Expect a closing tag `</{key}>`
pub fn expect_end<B: BufRead>(
    xml: &mut Reader<B>,
    buf: &mut Vec<u8>,
    key: &'static str,
) -> Result<()> {
    if let Event::End(end) = xml.read_event(buf)? {
        #[allow(clippy::branches_sharing_code)]
        if end.name() == key.as_bytes() {
            buf.clear();
            Ok(())
        } else {
            buf.clear();
            todo!()
        }
    } else {
        todo!()
    }
}