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
use futures::future;
use json;
use validation;
use serde_json;
use std::{self, fmt};
use image_crate::ImageError;
use futures::{Future, Poll};
use gltf::Gltf;
use std::boxed::Box;
use std::fmt::Debug;
use std::path::Path;
mod binary;
mod standard;
pub mod config;
pub mod data;
pub mod from_path;
pub use self::config::Config;
pub use self::data::Data;
pub use self::from_path::FromPath;
pub trait Source: Debug + Sized + 'static {
type Error: std::error::Error;
fn source_gltf(&self) -> Box<Future<Item = Box<[u8]>, Error = Self::Error>>;
fn source_external_data(&self, uri: &str) -> Box<Future<Item = Box<[u8]>, Error = Self::Error>>;
}
#[derive(Debug)]
pub enum Error<S: Source> {
Decode(ImageError),
ExtensionDisabled(String),
ExtensionUnsupported(String),
IncompatibleVersion(String),
Io(std::io::Error),
MalformedGlb(String),
MalformedJson(serde_json::error::Error),
Shared(future::SharedError<Error<S>>),
Source(S::Error),
Validation(Vec<(json::Path, validation::Error)>),
}
pub struct Import<S: Source>(Box<Future<Item = Gltf, Error = Error<S>>>);
impl<S: Source> Import<S> {
pub fn custom(source: S, config: Config) -> Self {
let future = source
.source_gltf()
.map_err(Error::Source)
.and_then(move |data| {
if data.starts_with(b"glTF") {
binary::import(data, source, config)
} else {
standard::import(data, source, config)
}
});
Import(Box::new(future))
}
pub fn sync(self) -> Result<Gltf, Error<S>> {
self.wait()
}
}
impl Import<FromPath> {
pub fn from_path<P: AsRef<Path>>(path: P) -> Self {
Import::custom(FromPath::new(path), Default::default())
}
}
impl<S: Source> Future for Import<S> {
type Item = Gltf;
type Error = Error<S>;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.0.poll()
}
}
impl<S: Source> From<ImageError> for Error<S> {
fn from(err: ImageError) -> Error<S> {
Error::Decode(err)
}
}
impl<S: Source> From<serde_json::Error> for Error<S> {
fn from(err: serde_json::Error) -> Error<S> {
Error::MalformedJson(err)
}
}
impl<S: Source> From<std::io::Error> for Error<S> {
fn from(err: std::io::Error) -> Error<S> {
Error::Io(err)
}
}
impl<S: Source> From<future::SharedError<Error<S>>> for Error<S> {
fn from(err: future::SharedError<Error<S>>) -> Error<S> {
Error::Shared(err)
}
}
impl<S: Source> From<Vec<(json::Path, validation::Error)>> for Error<S> {
fn from(errs: Vec<(json::Path, validation::Error)>) -> Error<S> {
Error::Validation(errs)
}
}
impl<S: Source> fmt::Display for Error<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use std::error::Error;
write!(f, "{}", self.description())
}
}
impl<S: Source> std::error::Error for Error<S> {
fn description(&self) -> &str {
use self::Error::*;
match self {
&Decode(_) => "Image decoding failed",
&ExtensionDisabled(_) => "Asset requires a disabled extension",
&ExtensionUnsupported(_) => "Assets requires an unsupported extension",
&IncompatibleVersion(_) => "Asset is not glTF version 2.0",
&Io(_) => "I/O error",
&MalformedGlb(_) => "Malformed .glb file",
&MalformedJson(_) => "Malformed .gltf / .glb JSON",
&Source(_) => "Data source error",
&Shared(_) => "Shared error",
&Validation(_) => "Asset failed validation tests",
}
}
fn cause(&self) -> Option<&std::error::Error> {
use self::Error::*;
match self {
&MalformedJson(ref err) => Some(err),
&Io(ref err) => Some(err),
&Source(ref err) => Some(err),
_ => None,
}
}
}