gltf_v1/
error.rs

1use std::result;
2
3use crate::binary;
4
5/// Result type for convenience.
6pub type Result<T> = result::Result<T, Error>;
7
8/// Represents a runtime error.
9#[derive(Debug)]
10pub enum Error {
11    Base64(base64::DecodeError),
12    BufferLength {
13        buffer: String,
14        expected: usize,
15        actual: usize,
16    },
17    Deserialize(json::Error),
18    Io(std::io::Error),
19    Image(image_crate::ImageError),
20    Validation(Vec<(json::Path, json::validation::Error)>),
21    Binary(binary::Error),
22    ExternalReferenceInSliceImport,
23    UnsupportedScheme,
24    MissingBlob,
25    UnsupportedImageEncoding,
26    UnsupportedImageFormat(image_crate::DynamicImage),
27}
28
29impl std::fmt::Display for Error {
30    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
31        match self {
32            Error::Io(e) => e.fmt(f),
33            Error::Deserialize(e) => e.fmt(f),
34            Error::Binary(e) => e.fmt(f),
35            Error::Validation(xs) => {
36                write!(f, "invalid glTF 1.0:")?;
37                for (path, error) in xs {
38                    write!(f, " {}: {};", path, error)?;
39                }
40                Ok(())
41            }
42            Error::Base64(e) => e.fmt(f),
43            Error::ExternalReferenceInSliceImport => {
44                write!(f, "external reference in slice only import")
45            }
46            Error::UnsupportedScheme => write!(f, "unsupported URI scheme"),
47            Error::MissingBlob => write!(f, "missing binary portion of binary glTF"),
48            Error::BufferLength {
49                buffer,
50                expected,
51                actual,
52            } => write!(
53                f,
54                "buffer {}: expected {} bytes but received {} bytes",
55                buffer, expected, actual
56            ),
57            Error::UnsupportedImageEncoding => write!(f, "unsupported image encoding"),
58            Error::UnsupportedImageFormat(image) => {
59                write!(f, "unsupported image format: {:?}", image.color())
60            }
61            Error::Image(e) => e.fmt(f),
62        }
63    }
64}
65impl std::error::Error for Error {}
66impl From<std::io::Error> for Error {
67    fn from(err: std::io::Error) -> Self {
68        Error::Io(err)
69    }
70}
71impl From<json::Error> for Error {
72    fn from(value: json::Error) -> Self {
73        Error::Deserialize(value)
74    }
75}
76impl From<binary::Error> for Error {
77    fn from(err: binary::Error) -> Self {
78        Error::Binary(err)
79    }
80}
81impl From<image_crate::ImageError> for Error {
82    fn from(value: image_crate::ImageError) -> Self {
83        Error::Image(value)
84    }
85}