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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use serde;
use std;
use std::fmt;
use json::*;
use validation::{Error, Validate};
pub trait Get<T> {
fn get(&self, id: &Index<T>) -> Option<&T>;
}
#[derive(Clone, Copy)]
pub struct Index<T>(u32, std::marker::PhantomData<T>);
#[derive(Clone, Debug, Default, Deserialize, Validate)]
pub struct Root {
#[serde(default)]
pub accessors: Vec<accessor::Accessor>,
#[serde(default)]
pub animations: Vec<animation::Animation>,
pub asset: asset::Asset,
#[serde(default)]
pub buffers: Vec<buffer::Buffer>,
#[serde(default, rename = "bufferViews")]
pub buffer_views: Vec<buffer::View>,
pub scene: Option<Index<scene::Scene>>,
#[serde(default)]
pub extensions: extensions::root::Root,
#[serde(default)]
pub extras: Extras,
#[serde(default, rename = "extensionsUsed")]
pub extensions_used: Vec<String>,
#[serde(default, rename = "extensionsRequired")]
pub extensions_required: Vec<String>,
#[serde(default)]
pub cameras: Vec<camera::Camera>,
#[serde(default)]
pub images: Vec<image::Image>,
#[serde(default)]
pub materials: Vec<material::Material>,
#[serde(default)]
pub meshes: Vec<mesh::Mesh>,
#[serde(default)]
pub nodes: Vec<scene::Node>,
#[serde(default)]
pub samplers: Vec<texture::Sampler>,
#[serde(default)]
pub scenes: Vec<scene::Scene>,
#[serde(default)]
pub skins: Vec<skin::Skin>,
#[serde(default)]
pub textures: Vec<texture::Texture>,
}
impl Root {
pub fn accessor(&self, index: Index<accessor::Accessor>) -> &accessor::Accessor {
&self.accessors[index.0 as usize]
}
pub fn accessors(&self) -> &[accessor::Accessor] {
&self.accessors
}
pub fn animation(&self, index: Index<animation::Animation>) -> &animation::Animation {
&self.animations[index.0 as usize]
}
pub fn animations(&self) -> &[animation::Animation] {
&self.animations
}
pub fn asset(&self) -> &asset::Asset {
&self.asset
}
pub fn buffer(&self, index: Index<buffer::Buffer>) -> &buffer::Buffer {
&self.buffers[index.0 as usize]
}
pub fn buffers(&self) -> &[buffer::Buffer] {
&self.buffers
}
pub fn buffer_view(&self, index: Index<buffer::View>) -> &buffer::View {
&self.buffer_views[index.0 as usize]
}
pub fn buffer_views(&self) -> &[buffer::View] {
&self.buffer_views
}
pub fn camera(&self, index: Index<camera::Camera>) -> &camera::Camera {
&self.cameras[index.0 as usize]
}
pub fn cameras(&self) -> &[camera::Camera] {
&self.cameras
}
pub fn extensions_used(&self) -> &[String] {
&self.extensions_used
}
pub fn extensions_required(&self) -> &[String] {
&self.extensions_required
}
pub fn get<T>(&self, index: &Index<T>) -> Option<&T>
where Self: Get<T>
{
(self as &Get<T>).get(index)
}
pub fn image(&self, index: Index<image::Image>) -> &image::Image {
&self.images[index.0 as usize]
}
pub fn images(&self) -> &[image::Image] {
&self.images
}
pub fn material(&self, index: Index<material::Material>) -> &material::Material {
&self.materials[index.0 as usize]
}
pub fn materials(&self) -> &[material::Material] {
&self.materials
}
pub fn mesh(&self, index: Index<mesh::Mesh>) -> &mesh::Mesh {
&self.meshes[index.0 as usize]
}
pub fn meshes(&self) -> &[mesh::Mesh] {
&self.meshes
}
pub fn node(&self, index: Index<scene::Node>) -> &scene::Node {
&self.nodes[index.0 as usize]
}
pub fn nodes(&self) -> &[scene::Node] {
&self.nodes
}
pub fn sampler(&self, index: Index<texture::Sampler>) -> &texture::Sampler {
&self.samplers[index.0 as usize]
}
pub fn samplers(&self) -> &[texture::Sampler] {
&self.samplers
}
pub fn scene(&self, index: Index<scene::Scene>) -> &scene::Scene {
&self.scenes[index.0 as usize]
}
pub fn scenes(&self) -> &[scene::Scene] {
&self.scenes
}
pub fn skin(&self, index: Index<skin::Skin>) -> &skin::Skin {
&self.skins[index.0 as usize]
}
pub fn skins(&self) -> &[skin::Skin] {
&self.skins
}
pub fn texture(&self, index: Index<texture::Texture>) -> &texture::Texture {
&self.textures[index.0 as usize]
}
pub fn textures(&self) -> &[texture::Texture] {
&self.textures
}
}
impl<T> Index<T> {
fn new(value: u32) -> Self {
Index(value, std::marker::PhantomData)
}
pub fn value(&self) -> usize {
self.0 as usize
}
}
impl<T> serde::Serialize for Index<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: ::serde::Serializer
{
serializer.serialize_u64(self.value() as u64)
}
}
impl<'de, T> serde::Deserialize<'de> for Index<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: serde::Deserializer<'de>
{
struct Visitor<T>(std::marker::PhantomData<T>);
impl<'de, T> serde::de::Visitor<'de> for Visitor<T> {
type Value = Index<T>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("index into child of root")
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where E: serde::de::Error
{
Ok(Index::new(value as u32))
}
}
deserializer.deserialize_u64(Visitor::<T>(std::marker::PhantomData))
}
}
impl<T> fmt::Debug for Index<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<T> fmt::Display for Index<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<T: Validate> Validate for Index<T>
where Root: Get<T>
{
fn validate_minimally<P, R>(&self, root: &Root, path: P, mut report: &mut R)
where P: Fn() -> Path, R: FnMut(&Fn() -> Path, Error)
{
if root.get(self).is_none() {
report(&path, Error::IndexOutOfBounds);
}
}
}
macro_rules! impl_get {
($ty:ty, $field:ident) => {
impl<'a> Get<$ty> for Root {
fn get(&self, index: &Index<$ty>) -> Option<&$ty> {
self.$field.get(index.value())
}
}
}
}
impl_get!(accessor::Accessor, accessors);
impl_get!(animation::Animation, animations);
impl_get!(buffer::Buffer, buffers);
impl_get!(buffer::View, buffer_views);
impl_get!(camera::Camera, cameras);
impl_get!(image::Image, images);
impl_get!(material::Material, materials);
impl_get!(mesh::Mesh, meshes);
impl_get!(scene::Node, nodes);
impl_get!(texture::Sampler, samplers);
impl_get!(scene::Scene, scenes);
impl_get!(skin::Skin, skins);
impl_get!(texture::Texture, textures);