Skip to main content

scenix_core/
error.rs

1use core::fmt;
2
3/// Top-level scenix error type.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum ScenixError {
7    /// Asset loading failed.
8    Load(LoadError),
9    /// GPU setup or upload failed.
10    Gpu(GpuError),
11    /// Validation failed.
12    Validation(ValidationError),
13}
14
15/// Asset loading errors.
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub enum LoadError {
19    /// The input format is unsupported.
20    UnsupportedFormat,
21    /// The source uses a known format feature that scenix does not support yet.
22    UnsupportedFeature,
23    /// The input bytes or text could not be parsed.
24    Parse,
25    /// The input could not be decoded.
26    Decode,
27    /// An IO or network request failed.
28    Io,
29    /// The requested asset was not found.
30    NotFound,
31}
32
33/// GPU-related errors.
34#[derive(Clone, Copy, Debug, PartialEq, Eq)]
35#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
36pub enum GpuError {
37    /// GPU backend initialization failed.
38    Init,
39    /// GPU resource upload failed.
40    Upload,
41    /// GPU feature or limit is unsupported.
42    Unsupported,
43}
44
45/// Validation errors.
46#[derive(Clone, Copy, Debug, PartialEq, Eq)]
47#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
48pub enum ValidationError {
49    /// An ID did not refer to a known object.
50    InvalidId,
51    /// A numeric value was outside the accepted range.
52    OutOfRange,
53    /// A value combination is invalid.
54    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}