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
#![doc(html_root_url = "https://docs.rs/rgbds-obj/0.1.0")]
use std::convert::TryInto;
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::io::{self, Read};
mod assertion;
pub use assertion::*;
mod fstack;
pub use fstack::*;
mod patch;
pub use patch::*;
mod rpn;
pub use rpn::*;
mod section;
pub use section::*;
mod symbol;
pub use symbol::*;
mod util;
use util::*;
#[derive(Debug)]
pub struct Object {
revision: u32,
fstack_nodes: Vec<Node>,
symbols: Vec<Symbol>,
sections: Vec<Section>,
assertions: Vec<Assertion>,
}
impl Object {
pub fn read_from(mut input: impl Read) -> Result<Self, io::Error> {
let mut magic = [0; 4];
input.read_exact(&mut magic)?;
if &magic[0..3] != b"RGB" || !magic[3].is_ascii_digit() {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"This does not appear to be a valid RGBDS object",
));
}
if magic[3] != b'9' {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Object file version {} is not supported (only 9 is)",
magic[3]
),
));
}
let revision = read_u32le(&mut input)?;
if !(6..=8).contains(&revision) {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Object file v9 revision {} is not supported (must be 6, 7, or 8)",
revision
),
));
}
let nb_symbols = read_u32le(&mut input)?.try_into().unwrap();
let nb_sections = read_u32le(&mut input)?.try_into().unwrap();
let nb_fstack_nodes = read_u32le(&mut input)?.try_into().unwrap();
let mut obj = Self {
revision,
fstack_nodes: Vec::with_capacity(nb_fstack_nodes),
symbols: Vec::with_capacity(nb_symbols),
sections: Vec::with_capacity(nb_sections),
assertions: Vec::new(),
};
for _ in 0..nb_fstack_nodes {
obj.fstack_nodes.push(Node::read_from(&mut input)?);
}
for _ in 0..nb_symbols {
obj.symbols.push(Symbol::read_from(&mut input)?);
}
for _ in 0..nb_sections {
obj.sections.push(Section::read_from(&mut input)?);
}
let nb_assertions = read_u32le(&mut input)?.try_into().unwrap();
obj.assertions.reserve_exact(nb_assertions);
for _ in 0..nb_assertions {
obj.assertions.push(Assertion::read_from(&mut input)?);
}
Ok(obj)
}
pub fn version(&self) -> u8 {
9
}
pub fn revision(&self) -> u32 {
self.revision
}
pub fn node(&self, id: u32) -> Option<&Node> {
let id: usize = id.try_into().unwrap();
if id < self.fstack_nodes.len() {
Some(&self.fstack_nodes[self.fstack_nodes.len() - 1 - id])
} else {
None
}
}
pub fn walk_nodes<E, F>(&self, id: u32, callback: &mut F) -> Result<(), NodeWalkError<E>>
where
F: FnMut(&Node) -> Result<(), NodeWalkError<E>>,
{
let node = self
.node(id)
.ok_or_else(|| NodeWalkError::bad_id(id, self))?;
if let Some((id, _)) = node.parent() {
self.walk_nodes(id, callback)?;
}
callback(node)
}
pub fn symbols(&self) -> &[Symbol] {
&self.symbols
}
pub fn sections(&self) -> &[Section] {
&self.sections
}
pub fn assertions(&self) -> &[Assertion] {
&self.assertions
}
}
#[derive(Debug)]
pub enum NodeWalkError<E> {
BadId(u32, usize),
Custom(E),
}
impl<E> NodeWalkError<E> {
pub fn bad_id(id: u32, object: &Object) -> Self {
Self::BadId(id, object.fstack_nodes.len())
}
}
impl<E: Display> Display for NodeWalkError<E> {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> {
use NodeWalkError::*;
match self {
BadId(id, len) => write!(fmt, "Requested node #{} of {}", id, len),
Custom(err) => err.fmt(fmt),
}
}
}
impl<E: Error + 'static> Error for NodeWalkError<E> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
use NodeWalkError::*;
if let Custom(err) = self {
Some(err)
} else {
None
}
}
}
impl<E> From<E> for NodeWalkError<E> {
fn from(inner: E) -> Self {
Self::Custom(inner)
}
}