Skip to main content

mynd_core/
error.rs

1use core::fmt;
2
3use mynd_math::MathError;
4
5/// A failure while validating format-neutral image geometry or storage.
6#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
7#[non_exhaustive]
8pub enum GeometryError {
9    /// An image width was zero.
10    ZeroWidth,
11    /// An image height was zero.
12    ZeroHeight,
13    /// A rectangle width was zero.
14    ZeroRectangleWidth,
15    /// A rectangle height was zero.
16    ZeroRectangleHeight,
17    /// A rectangle was not fully contained by its declared dimensions.
18    RectangleOutOfBounds,
19    /// A plane row count was zero.
20    ZeroRows,
21    /// The number of bytes used by a plane row was zero.
22    ZeroRowBytes,
23    /// A plane alignment was zero.
24    ZeroAlignment,
25    /// A row stride was smaller than the bytes used by one row.
26    StrideTooSmall,
27    /// A plane offset was not divisible by its numeric alignment.
28    OffsetMisaligned,
29    /// A row stride was not divisible by its numeric alignment.
30    StrideMisaligned,
31    /// A plane set was empty.
32    NoPlanes,
33    /// Plane offsets were not in nondecreasing order.
34    PlanesOutOfOrder,
35    /// Two adjacent ordered plane extents overlapped.
36    PlaneOverlap,
37    /// Checked arithmetic or platform-width conversion failed.
38    Arithmetic(MathError),
39}
40
41impl fmt::Display for GeometryError {
42    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Self::ZeroWidth => formatter.write_str("image width must be nonzero"),
45            Self::ZeroHeight => formatter.write_str("image height must be nonzero"),
46            Self::ZeroRectangleWidth => formatter.write_str("rectangle width must be nonzero"),
47            Self::ZeroRectangleHeight => formatter.write_str("rectangle height must be nonzero"),
48            Self::RectangleOutOfBounds => {
49                formatter.write_str("rectangle exceeds its image dimensions")
50            }
51            Self::ZeroRows => formatter.write_str("plane row count must be nonzero"),
52            Self::ZeroRowBytes => formatter.write_str("plane row bytes must be nonzero"),
53            Self::ZeroAlignment => formatter.write_str("plane alignment must be nonzero"),
54            Self::StrideTooSmall => formatter.write_str("row stride is smaller than row bytes"),
55            Self::OffsetMisaligned => formatter.write_str("plane offset is not aligned"),
56            Self::StrideMisaligned => formatter.write_str("row stride is not aligned"),
57            Self::NoPlanes => formatter.write_str("plane set must be nonempty"),
58            Self::PlanesOutOfOrder => formatter.write_str("plane offsets are out of order"),
59            Self::PlaneOverlap => formatter.write_str("plane extents overlap"),
60            Self::Arithmetic(error) => write!(formatter, "geometry arithmetic failed: {error}"),
61        }
62    }
63}
64
65impl core::error::Error for GeometryError {
66    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
67        match self {
68            Self::Arithmetic(error) => Some(error),
69            _ => None,
70        }
71    }
72}
73
74/// The result type returned by validated geometry primitives.
75pub type GeometryResult<T> = Result<T, GeometryError>;
76
77/// A failure while validating sample storage.
78#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
79#[non_exhaustive]
80pub enum StorageError {
81    /// A sample declared zero significant bits.
82    ZeroSampleBits,
83    /// A packed sample exceeded the supported 64-bit storage domain.
84    PackedSampleTooWide,
85    /// Significant bits exceeded the selected physical storage unit.
86    SampleBitsExceedStorage,
87    /// A signed sample had no bit left for a value after its sign bit.
88    SignedSampleNeedsValueBit,
89    /// A floating sample did not occupy an entire 16-, 32-, or 64-bit word.
90    InvalidFloatingStorage,
91}
92
93impl fmt::Display for StorageError {
94    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
95        match self {
96            Self::ZeroSampleBits => formatter.write_str("sample bits must be nonzero"),
97            Self::PackedSampleTooWide => {
98                formatter.write_str("packed samples cannot exceed 64 bits")
99            }
100            Self::SampleBitsExceedStorage => {
101                formatter.write_str("sample bits exceed the selected storage unit")
102            }
103            Self::SignedSampleNeedsValueBit => {
104                formatter.write_str("signed samples require a sign bit and a value bit")
105            }
106            Self::InvalidFloatingStorage => {
107                formatter.write_str("floating samples require a full 16-, 32-, or 64-bit word")
108            }
109        }
110    }
111}
112
113impl core::error::Error for StorageError {}
114
115/// The result type returned by sample-storage validation.
116pub type StorageResult<T> = Result<T, StorageError>;
117
118/// A failure while relating a pixel layout to concrete planes.
119#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
120#[non_exhaustive]
121pub enum PixelLayoutError {
122    /// The concrete plane count did not match the pixel layout.
123    PlaneCountMismatch,
124    /// A plane row count did not match its logical sampled height.
125    PlaneRowsMismatch,
126    /// A plane's used row bytes did not match its exact packed width.
127    PlaneRowBytesMismatch,
128    /// Geometry validation failed.
129    Geometry(GeometryError),
130    /// Checked row-size arithmetic failed.
131    Arithmetic(MathError),
132}
133
134impl fmt::Display for PixelLayoutError {
135    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
136        match self {
137            Self::PlaneCountMismatch => formatter.write_str("pixel layout plane count mismatch"),
138            Self::PlaneRowsMismatch => formatter.write_str("pixel layout plane rows mismatch"),
139            Self::PlaneRowBytesMismatch => {
140                formatter.write_str("pixel layout plane row bytes mismatch")
141            }
142            Self::Geometry(error) => write!(formatter, "pixel layout geometry failed: {error}"),
143            Self::Arithmetic(error) => write!(formatter, "pixel layout arithmetic failed: {error}"),
144        }
145    }
146}
147
148impl core::error::Error for PixelLayoutError {
149    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
150        match self {
151            Self::Geometry(error) => Some(error),
152            Self::Arithmetic(error) => Some(error),
153            Self::PlaneCountMismatch | Self::PlaneRowsMismatch | Self::PlaneRowBytesMismatch => {
154                None
155            }
156        }
157    }
158}
159
160impl From<GeometryError> for PixelLayoutError {
161    fn from(error: GeometryError) -> Self {
162        Self::Geometry(error)
163    }
164}
165
166/// The result type returned by pixel-layout validation.
167pub type PixelLayoutResult<T> = Result<T, PixelLayoutError>;