mugltf/
error.rs

1//! Error types.
2
3use super::Id;
4use alloc::boxed::Box;
5use core::fmt;
6
7/// Error type.
8#[cfg(feature = "std")]
9pub type Error = dyn std::error::Error;
10/// Error type.
11#[cfg(not(feature = "std"))]
12pub type Error = dyn core::any::Any;
13
14/// Error when parsing a glTF / GLB file.
15#[derive(Debug, Default)]
16pub struct ParseGltfError {
17    kind: ParseGltfErrorKind,
18    #[allow(unused)]
19    error: Option<Box<Error>>,
20}
21
22impl ParseGltfError {
23    /// Creates a new `ParseGLBError`.
24    #[inline]
25    pub fn new<E: Into<Box<Error>>>(kind: ParseGltfErrorKind, error: E) -> Self {
26        Self {
27            kind,
28            error: Some(error.into()),
29        }
30    }
31}
32
33impl fmt::Display for ParseGltfError {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self.kind {
36            ParseGltfErrorKind::InvalidHeader => write!(f, "invalid GLB header"),
37            ParseGltfErrorKind::UnsupportedVersion => write!(f, "unsupported glTF version"),
38            ParseGltfErrorKind::InvalidChunkHeader => write!(f, "invalid GLB chunk header"),
39            ParseGltfErrorKind::InvalidChunk => write!(f, "invalid GLB chunk data"),
40            ParseGltfErrorKind::MissingJson => write!(f, "missing glTF JSON content"),
41            _ => write!(f, "invalid GLB"),
42        }
43    }
44}
45
46#[cfg(feature = "std")]
47impl std::error::Error for ParseGltfError {
48    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
49        Some(self.error.as_ref()?.as_ref())
50    }
51}
52
53/// The kind of glTF parsing error.
54#[derive(Clone, Copy, Debug)]
55pub enum ParseGltfErrorKind {
56    InvalidHeader,
57    UnsupportedVersion,
58    InvalidChunkHeader,
59    InvalidChunk,
60    InvalidJson,
61    MissingJson,
62    Other,
63}
64
65impl Default for ParseGltfErrorKind {
66    fn default() -> Self {
67        Self::Other
68    }
69}
70
71impl From<ParseGltfErrorKind> for ParseGltfError {
72    fn from(kind: ParseGltfErrorKind) -> Self {
73        Self { kind, error: None }
74    }
75}
76
77/// Error when loading resources for a glTF file.
78#[derive(Debug, Default)]
79pub struct LoadGltfResourceError {
80    kind: LoadGltfResourceErrorKind,
81    #[allow(unused)]
82    error: Option<Box<Error>>,
83}
84
85impl LoadGltfResourceError {
86    /// Creates a new `LoadGltfResourceError`.
87    #[inline]
88    pub fn new<E: Into<Box<Error>>>(kind: LoadGltfResourceErrorKind, error: E) -> Self {
89        Self {
90            kind,
91            error: Some(error.into()),
92        }
93    }
94}
95
96impl fmt::Display for LoadGltfResourceError {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        match self.kind {
99            LoadGltfResourceErrorKind::LoadBufferError(id) => write!(f, "failed to load buffer {}", id),
100            LoadGltfResourceErrorKind::LoadImageError(id) => write!(f, "failed to load image {}", id),
101            LoadGltfResourceErrorKind::ParseGltfError => write!(f, "failed to parse glTF or GLB file"),
102            _ => write!(f, "failed to load resource"),
103        }
104    }
105}
106
107#[cfg(feature = "std")]
108impl std::error::Error for LoadGltfResourceError {
109    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
110        Some(self.error.as_ref()?.as_ref())
111    }
112}
113
114/// The kind of glTF resource loading error.
115#[derive(Clone, Copy, Debug)]
116pub enum LoadGltfResourceErrorKind {
117    LoadImageError(Id),
118    LoadBufferError(Id),
119    LoadError,
120    ParseGltfError,
121}
122
123impl Default for LoadGltfResourceErrorKind {
124    fn default() -> Self {
125        Self::LoadError
126    }
127}
128
129impl From<LoadGltfResourceErrorKind> for LoadGltfResourceError {
130    fn from(kind: LoadGltfResourceErrorKind) -> Self {
131        Self { kind, error: None }
132    }
133}