1use core::fmt;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum ScenixError {
7 Load(LoadError),
9 Gpu(GpuError),
11 Validation(ValidationError),
13}
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub enum LoadError {
19 UnsupportedFormat,
21 UnsupportedFeature,
23 Parse,
25 Decode,
27 Io,
29 NotFound,
31}
32
33#[derive(Clone, Copy, Debug, PartialEq, Eq)]
35#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
36pub enum GpuError {
37 Init,
39 Upload,
41 Unsupported,
43}
44
45#[derive(Clone, Copy, Debug, PartialEq, Eq)]
47#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
48pub enum ValidationError {
49 InvalidId,
51 OutOfRange,
53 InvalidState,
55}
56
57impl fmt::Display for ScenixError {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 match self {
60 Self::Load(err) => write!(f, "load error: {err}"),
61 Self::Gpu(err) => write!(f, "gpu error: {err}"),
62 Self::Validation(err) => write!(f, "validation error: {err}"),
63 }
64 }
65}
66
67impl fmt::Display for LoadError {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 match self {
70 Self::UnsupportedFormat => f.write_str("unsupported format"),
71 Self::UnsupportedFeature => f.write_str("unsupported asset feature"),
72 Self::Parse => f.write_str("parse failed"),
73 Self::Decode => f.write_str("decode failed"),
74 Self::Io => f.write_str("io failed"),
75 Self::NotFound => f.write_str("asset not found"),
76 }
77 }
78}
79
80impl fmt::Display for GpuError {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 match self {
83 Self::Init => f.write_str("initialization failed"),
84 Self::Upload => f.write_str("resource upload failed"),
85 Self::Unsupported => f.write_str("unsupported gpu feature"),
86 }
87 }
88}
89
90impl fmt::Display for ValidationError {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 match self {
93 Self::InvalidId => f.write_str("invalid id"),
94 Self::OutOfRange => f.write_str("value out of range"),
95 Self::InvalidState => f.write_str("invalid state"),
96 }
97 }
98}
99
100impl From<LoadError> for ScenixError {
101 #[inline]
102 fn from(value: LoadError) -> Self {
103 Self::Load(value)
104 }
105}
106
107impl From<GpuError> for ScenixError {
108 #[inline]
109 fn from(value: GpuError) -> Self {
110 Self::Gpu(value)
111 }
112}
113
114impl From<ValidationError> for ScenixError {
115 #[inline]
116 fn from(value: ValidationError) -> Self {
117 Self::Validation(value)
118 }
119}
120
121#[cfg(feature = "std")]
122impl std::error::Error for ScenixError {}
123#[cfg(feature = "std")]
124impl std::error::Error for LoadError {}
125#[cfg(feature = "std")]
126impl std::error::Error for GpuError {}
127#[cfg(feature = "std")]
128impl std::error::Error for ValidationError {}
129
130#[cfg(all(test, feature = "std"))]
131mod tests {
132 use super::*;
133
134 #[test]
135 fn errors_display_without_allocation_payloads() {
136 assert_eq!(
137 ScenixError::from(ValidationError::InvalidId).to_string(),
138 "validation error: invalid id"
139 );
140 }
141}