wde_gltf/
error.rs

1//! Error types for glTF loading and parsing.
2
3use std::fmt;
4
5/// Errors that can occur during glTF loading and parsing.
6#[derive(Debug, Clone)]
7pub enum GltfError {
8    /// File I/O error
9    IoError(String),
10    /// JSON parsing error
11    JsonError(String),
12    /// Unsupported glTF version
13    UnsupportedVersion(String),
14    /// Missing required field in JSON
15    MissingField(String),
16    /// Invalid buffer view index
17    InvalidBufferView(i64),
18    /// Buffer overflow (reading beyond buffer size)
19    BufferOverflow {
20        start: usize,
21        end: usize,
22        buffer_size: usize
23    },
24    /// Unsupported accessor component type
25    UnsupportedComponentType(i64),
26    /// Mismatched vertex counts across attributes
27    MismatchedVertexCount {
28        primitive: String,
29        expected: usize,
30        actual: usize
31    },
32    /// Base64 decode error
33    Base64Error(String),
34    /// No buffers found in glTF file
35    NoBuffers,
36    /// Multiple buffers (only single buffer supported)
37    MultipleBuffers(usize),
38    /// Invalid accessor type
39    InvalidAccessorType(String),
40    /// Unsupported accessor type
41    UnsupportedAccessorType(String)
42}
43
44impl fmt::Display for GltfError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            GltfError::IoError(msg) => write!(f, "I/O error: {}", msg),
48            GltfError::JsonError(msg) => write!(f, "JSON parsing error: {}", msg),
49            GltfError::UnsupportedVersion(version) => {
50                write!(f, "Unsupported glTF version: {}", version)
51            }
52            GltfError::MissingField(field) => write!(f, "Missing required field: {}", field),
53            GltfError::InvalidBufferView(index) => {
54                write!(f, "Invalid buffer view index: {}", index)
55            }
56            GltfError::BufferOverflow {
57                start,
58                end,
59                buffer_size
60            } => {
61                write!(
62                    f,
63                    "Buffer overflow: trying to read {}..{} from buffer of size {}",
64                    start, end, buffer_size
65                )
66            }
67            GltfError::UnsupportedComponentType(type_id) => {
68                write!(f, "Unsupported component type: {}", type_id)
69            }
70            GltfError::MismatchedVertexCount {
71                primitive,
72                expected,
73                actual
74            } => {
75                write!(
76                    f,
77                    "Mismatched vertex count in primitive '{}': expected {}, got {}",
78                    primitive, expected, actual
79                )
80            }
81            GltfError::Base64Error(msg) => write!(f, "Base64 decode error: {}", msg),
82            GltfError::NoBuffers => write!(f, "No buffers found in glTF file"),
83            GltfError::MultipleBuffers(count) => write!(
84                f,
85                "Multiple buffers found ({}), only single buffer supported",
86                count
87            ),
88            GltfError::InvalidAccessorType(type_name) => {
89                write!(f, "Invalid accessor type: {}", type_name)
90            }
91            GltfError::UnsupportedAccessorType(type_name) => {
92                write!(f, "Unsupported accessor type: {}", type_name)
93            }
94        }
95    }
96}
97
98impl std::error::Error for GltfError {}
99
100impl From<std::io::Error> for GltfError {
101    fn from(err: std::io::Error) -> Self {
102        GltfError::IoError(err.to_string())
103    }
104}
105
106impl From<serde_json::Error> for GltfError {
107    fn from(err: serde_json::Error) -> Self {
108        GltfError::JsonError(err.to_string())
109    }
110}
111
112impl From<base64::DecodeError> for GltfError {
113    fn from(err: base64::DecodeError) -> Self {
114        GltfError::Base64Error(err.to_string())
115    }
116}