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
|
//! OpenGL command.
use crate::xml::element::{Elements, FromElements};
/// A command.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Command
{
prototype: Prototype,
parameters: Vec<Parameter>,
}
impl Command
{
/// Returns a new `Command`.
pub fn new(
prototype: Prototype,
parameters: impl IntoIterator<Item = Parameter>,
) -> Self
{
Self {
prototype,
parameters: parameters.into_iter().collect(),
}
}
/// Returns the command prototype.
#[must_use]
pub fn prototype(&self) -> &Prototype
{
&self.prototype
}
/// Returns the command parameters.
#[must_use]
pub fn parameters(&self) -> &[Parameter]
{
&self.parameters
}
}
impl FromElements for Command
{
type Error = Error;
fn from_elements(
elements: &crate::xml::element::Elements,
) -> Result<Self, Self::Error>
{
let proto_element = elements
.get_first_tagged_element("proto")
.ok_or(Self::Error::MissingPrototype)?;
let prototype = Prototype::from_elements(proto_element.child_elements())?;
let parameters = elements
.get_all_tagged_elements_with_name("param")
.into_iter()
.map(|param_element| Parameter::from_elements(param_element.child_elements()))
.collect::<Result<Vec<_>, _>>()?;
Ok(Self {
prototype,
parameters,
})
}
}
/// [`Command`] error.
#[derive(Debug, thiserror::Error)]
pub enum Error
{
/// No 'proto' element was found.
#[error("No 'proto' element was found")]
MissingPrototype,
/// Invalid prototype.
#[error("Invalid prototype")]
InvalidPrototype(#[from] PrototypeError),
/// Invalid parameter.
#[error("Invalid parameter")]
InvalidParameter(#[from] ParameterError),
}
/// A command prototype.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Prototype
{
name: String,
return_type: String,
}
impl Prototype
{
/// Returns a new `Prototype`.
pub fn new(name: impl Into<String>, return_type: impl Into<String>) -> Self
{
Self {
name: name.into(),
return_type: return_type.into(),
}
}
/// Returns the command prototype name.
#[must_use]
pub fn name(&self) -> &str
{
&self.name
}
/// Returns the command prototype return type.
#[must_use]
pub fn return_type(&self) -> &str
{
&self.return_type
}
}
impl FromElements for Prototype
{
type Error = PrototypeError;
fn from_elements(
elements: &crate::xml::element::Elements,
) -> Result<Self, Self::Error>
{
let name = elements
.get_first_tagged_element("name")
.ok_or(Self::Error::MissingName)?
.child_elements()
.get_first_text_element()
.cloned()
.unwrap_or_default();
let return_type = find_type(elements);
Ok(Self { name, return_type })
}
}
/// [`Prototype`] error.
#[derive(Debug, thiserror::Error)]
pub enum PrototypeError
{
/// No 'name' element was found.
#[error("No 'name' element was found")]
MissingName,
}
/// A command parameter.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Parameter
{
name: String,
ty: String,
}
impl Parameter
{
/// Returns a new `Parameter`.
pub fn new(name: impl Into<String>, ty: impl Into<String>) -> Self
{
Self {
name: name.into(),
ty: ty.into(),
}
}
/// Returns the name of the command parameter.
#[must_use]
pub fn name(&self) -> &str
{
&self.name
}
/// Returns the type of the command parameter.
#[must_use]
pub fn get_type(&self) -> &str
{
&self.ty
}
}
impl FromElements for Parameter
{
type Error = ParameterError;
fn from_elements(elements: &Elements) -> Result<Self, Self::Error>
{
let name = elements
.get_first_tagged_element("name")
.ok_or(Self::Error::MissingName)?
.child_elements()
.get_first_text_element()
.cloned()
.unwrap_or_default();
let ty = find_type(elements);
Ok(Self { name, ty })
}
}
/// [`Parameter`] error.
#[derive(Debug, thiserror::Error)]
pub enum ParameterError
{
/// No 'name' element was found.
#[error("No 'name' element was found")]
MissingName,
}
fn find_type(elements: &Elements) -> String
{
let text_type_parts = elements
.get_all_text_elements()
.into_iter()
.map(|text_type_part| text_type_part.trim())
.filter(|text_type_part| !text_type_part.is_empty())
.collect::<Vec<_>>();
let opt_ptype_text = get_ptype_text(elements);
opt_ptype_text.map_or_else(
|| join_space_strs(text_type_parts.iter()),
|ptype_text| {
let Some(first_part) = text_type_parts.first() else {
return ptype_text.clone();
};
let before = if *first_part == "const" { "const " } else { "" };
let after_start_index = usize::from(*first_part == "const");
format!(
"{before}{ptype_text} {}",
text_type_parts
.get(after_start_index..)
.map(|parts| join_space_strs(parts.iter()))
.unwrap_or_default()
)
},
)
}
fn get_ptype_text(elements: &Elements) -> Option<&String>
{
let ptype_element = elements.get_first_tagged_element("ptype")?;
ptype_element.child_elements().get_first_text_element()
}
fn join_space_strs<Strings, StrItem>(strings: Strings) -> String
where
Strings: Iterator<Item = StrItem>,
StrItem: ToString,
{
strings
.into_iter()
.map(|string| string.to_string())
.collect::<Vec<_>>()
.join(" ")
}
|