1use core::fmt;
2
3use mynd_math::MathError;
4
5#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
7#[non_exhaustive]
8pub enum GeometryError {
9 ZeroWidth,
11 ZeroHeight,
13 ZeroRectangleWidth,
15 ZeroRectangleHeight,
17 RectangleOutOfBounds,
19 ZeroRows,
21 ZeroRowBytes,
23 ZeroAlignment,
25 StrideTooSmall,
27 OffsetMisaligned,
29 StrideMisaligned,
31 NoPlanes,
33 PlanesOutOfOrder,
35 PlaneOverlap,
37 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
74pub type GeometryResult<T> = Result<T, GeometryError>;
76
77#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
79#[non_exhaustive]
80pub enum StorageError {
81 ZeroSampleBits,
83 PackedSampleTooWide,
85 SampleBitsExceedStorage,
87 SignedSampleNeedsValueBit,
89 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
115pub type StorageResult<T> = Result<T, StorageError>;
117
118#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
120#[non_exhaustive]
121pub enum PixelLayoutError {
122 PlaneCountMismatch,
124 PlaneRowsMismatch,
126 PlaneRowBytesMismatch,
128 Geometry(GeometryError),
130 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
166pub type PixelLayoutResult<T> = Result<T, PixelLayoutError>;