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
//! Parsing of IrpTransmogrifier's IrpProtocols.xml.

use serde::Deserialize;
use std::{
    fs::File,
    io::{self, BufReader},
    path::Path,
    str::FromStr,
};
use xml::reader::{EventReader, XmlEvent};

/// Entry in IrpTransmogrifier's IrpProtocols.xml.
#[derive(Debug, Deserialize, PartialEq, Default)]
pub struct Protocol {
    pub name: String,
    pub alt_name: Vec<String>,
    pub irp: String,
    pub prefer_over: Vec<String>,
    pub absolute_tolerance: u32,
    pub relative_tolerance: f32,
    pub minimum_leadout: u32,
    pub decode_only: bool,
    pub decodable: bool,
    pub reject_repeatess: bool,
}

enum Element {
    None,
    Irp,
    AbsoluteTolerance,
    RelativeTolerance,
    AlternateName,
    DecodeOnly,
    Decodable,
    PreferOver,
    MinimumLeadout,
    RejectRepeatLess,
}

impl Protocol {
    /// Parse IrpTransmogrifier's IrpProtocols.xml.
    pub fn parse(path: &Path) -> io::Result<Vec<Protocol>> {
        let file = File::open(path)?;
        let file = BufReader::new(file);

        let parser = EventReader::new(file);
        let mut protocols: Vec<Protocol> = Vec::new();
        let mut protocol = None;
        let mut element = Element::None;

        for e in parser {
            match e {
                Ok(XmlEvent::StartElement {
                    name, attributes, ..
                }) => match name.local_name.as_ref() {
                    "protocol" => {
                        if attributes.len() == 1 && attributes[0].name.local_name == "name" {
                            protocol = Some(Protocol {
                                name: attributes[0].value.to_owned(),
                                decodable: true,
                                absolute_tolerance: 100,
                                relative_tolerance: 0.3,
                                minimum_leadout: 20000,
                                ..Default::default()
                            });
                        } else {
                            panic!("missing name attribute");
                        }
                    }
                    "irp" => {
                        element = Element::Irp;
                    }
                    "parameter" => {
                        for attr in attributes {
                            match attr.name.local_name.as_ref() {
                                "prefer_over" => {
                                    element = Element::PreferOver;
                                }
                                "absolute-tolerance" => {
                                    element = Element::AbsoluteTolerance;
                                }
                                "relative-tolerance" => {
                                    element = Element::RelativeTolerance;
                                }
                                "decodable" => {
                                    element = Element::Decodable;
                                }
                                "decode-only" => {
                                    element = Element::DecodeOnly;
                                }
                                "alt_name" => {
                                    element = Element::AlternateName;
                                }
                                "minimum-leadout" => {
                                    element = Element::MinimumLeadout;
                                }
                                "reject_repeatless" => {
                                    element = Element::RejectRepeatLess;
                                }
                                _ => (),
                            }
                        }
                    }
                    _ => (),
                },
                Ok(XmlEvent::CData(data)) => {
                    if let Some(protocol) = &mut protocol {
                        match element {
                            Element::Irp => {
                                protocol.irp = data;
                            }
                            Element::AlternateName => {
                                protocol.alt_name.push(data);
                            }
                            Element::PreferOver => {
                                protocol.prefer_over.push(data);
                            }
                            Element::Decodable => {
                                protocol.decodable = bool::from_str(&data).unwrap();
                            }
                            Element::DecodeOnly => {
                                protocol.decode_only = bool::from_str(&data).unwrap();
                            }
                            Element::RejectRepeatLess => {
                                protocol.reject_repeatess = bool::from_str(&data).unwrap();
                            }
                            Element::AbsoluteTolerance => {
                                protocol.absolute_tolerance = u32::from_str(&data).unwrap();
                            }
                            Element::RelativeTolerance => {
                                protocol.relative_tolerance = f32::from_str(&data).unwrap();
                            }
                            Element::MinimumLeadout => {
                                protocol.minimum_leadout = u32::from_str(&data).unwrap();
                            }
                            Element::None => (),
                        }
                    }

                    element = Element::None;
                }
                Ok(XmlEvent::EndElement { name }) => {
                    if name.local_name == "protocol" {
                        if let Some(protocol) = protocol {
                            protocols.push(protocol);
                        }
                        protocol = None;
                    }
                }
                Err(e) => {
                    panic!("Error: {e}");
                }
                _ => {}
            }
        }

        Ok(protocols)
    }
}