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
use std::convert::TryFrom;
use xml::{reader::XmlEvent, EventReader};
use crate::gbx::*;
use super::ParseError;
/// Parses the xml included in GBX file for challenges
pub(crate) fn parse_challenge_header_xml(buf: &[u8]) -> Result<ChallengeXMLHeader, ParseError> {
let xmlp = EventReader::new(buf);
let mut header = ChallengeXMLHeader::default();
for e in xmlp {
match e {
Ok(XmlEvent::StartElement {
name, attributes, ..
}) => match name.local_name.as_str() {
"header" => {
for attr in attributes {
match attr.name.local_name.as_str() {
"type" => {
header.maptype = MapType::try_from(attr.value.as_str())
.map_err(ParseError::HeaderTryIntoEnumError)?
}
"version" => {
header.mapversion = GBXVersion::try_from(attr.value.as_str())
.map_err(ParseError::HeaderTryIntoEnumError)?
}
"exever" => header.exever = attr.value,
_ => (),
}
}
}
"ident" => {
for attr in attributes {
match attr.name.local_name.as_str() {
"uid" => header.uid = attr.value,
"name" => header.name = attr.value,
"author" => header.author = attr.value,
_ => (),
}
}
}
"desc" => {
for attr in attributes {
match attr.name.local_name.as_str() {
"envir" => {
header.envir = Environment::try_from(attr.value.as_str())
.map_err(ParseError::HeaderTryIntoEnumError)?
}
"mood" => {
header.mood = Mood::try_from(attr.value.as_str())
.map_err(ParseError::HeaderTryIntoEnumError)?
}
"type" => {
header.desctype = DescType::try_from(attr.value.as_str())
.map_err(ParseError::HeaderTryIntoEnumError)?
}
"nblaps" => {
header.nblaps =
attr.value.parse().map_err(ParseError::HeaderValueError)?
}
"price" => {
header.price =
attr.value.parse().map_err(ParseError::HeaderValueError)?
}
_ => (),
}
}
}
"times" => {
let mut times = Times::default();
for attr in attributes {
match attr.name.local_name.as_str() {
"bronze" => {
if let Ok(s) = attr.value.parse() {
times.bronze = Some(s)
} else if attr.value == "-1" {
times.bronze = None
}
}
"silver" => {
if let Ok(s) = attr.value.parse() {
times.silver = Some(s)
} else if attr.value == "-1" {
times.silver = None
}
}
"gold" => {
if let Ok(s) = attr.value.parse() {
times.gold = Some(s)
} else if attr.value == "-1" {
times.gold = None
}
}
"authortime" => {
if let Ok(s) = attr.value.parse() {
times.authortime = Some(s)
} else if attr.value == "-1" {
times.authortime = None
}
}
"authorscore" => {
if let Ok(s) = attr.value.parse() {
times.authorscore = Some(s)
} else if attr.value == "-1" {
times.authorscore = None
}
}
_ => (),
}
}
header.times = Some(times)
}
"deps" => continue,
"dep" => {
for attr in attributes {
if attr.name.local_name == "file" {
header.dependencies.push(Dependency { file: attr.value })
} else {
println!(
"Encountered unknown deps field {}={}",
attr.name.local_name, attr.value
)
}
}
}
_ => (),
},
Err(e) => return Err(ParseError::XMLParseError(e)),
_ => {}
}
}
Ok(header)
}