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
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![deny(clippy::all, clippy::pedantic, missing_docs)]
use std::fs::File;
use std::io::Read;
use crate::command::{Command, Error as CommandError};
use crate::xml::element::{Element, Elements, FromElements};
use crate::xml::parser::{Error as ParserError, Parser};
pub mod command;
mod xml;
#[cfg(feature = "include-xml")]
const GL_REGISTRY_XML: &str = include_str!("../OpenGL-Registry/xml/gl.xml");
const REGISTRY_TAG_NAME: &str = "registry";
pub struct Registry
{
commands: Vec<Command>,
}
impl Registry
{
#[cfg(feature = "include-xml")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "include-xml")))]
pub fn retrieve() -> Result<Registry, RegistryError>
{
Self::retrieve_from_bytes(GL_REGISTRY_XML.as_bytes())
}
pub fn retrieve_from_bytes(xml_bytes: &[u8]) -> Result<Registry, RegistryError>
{
let mut parser = Parser::new(xml_bytes);
let elements = parser.parse().map_err(ParsingError)?;
let registry_element = elements
.get_first_tagged_element(REGISTRY_TAG_NAME)
.ok_or(RegistryError::MissingRegistryElement)?;
let registry = Registry::from_elements(registry_element.child_elements())?;
Ok(registry)
}
pub fn retrieve_from_file(xml_file: &mut File) -> Result<Registry, RegistryError>
{
let mut buf = Vec::new();
xml_file.read_to_end(&mut buf)?;
Self::retrieve_from_bytes(&buf)
}
pub fn new(commands: impl IntoIterator<Item = Command>) -> Self
{
Self {
commands: commands.into_iter().collect(),
}
}
#[must_use]
pub fn commands(&self) -> &[Command]
{
&self.commands
}
}
impl FromElements for Registry
{
type Error = RegistryError;
fn from_elements(elements: &Elements) -> Result<Self, Self::Error>
{
let commands_element = elements
.get_first_tagged_element("commands")
.ok_or(Self::Error::MissingCommandsElement)?;
let command_elements =
commands_element
.child_elements()
.into_iter()
.filter_map(|element| match element {
Element::Tagged(tagged_element)
if tagged_element.name() == "command" =>
{
Some(tagged_element)
}
_ => None,
});
let commands = command_elements
.into_iter()
.map(|command_element| {
Command::from_elements(command_element.child_elements())
})
.collect::<Result<Vec<_>, _>>()?;
Ok(Self { commands })
}
}
#[derive(Debug, thiserror::Error)]
pub enum RegistryError
{
#[error("No 'registry' element was found")]
MissingRegistryElement,
#[error("No 'commands' element was found")]
MissingCommandsElement,
#[error("Invalid command")]
InvalidCommand(#[from] CommandError),
#[error("Parsing failed")]
ParsingFailed(#[from] ParsingError),
#[error("I/O failed")]
IOFailed(#[from] std::io::Error),
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct ParsingError(#[from] ParserError);