use core::fmt;
use mynd_math::MathError;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum GeometryError {
ZeroWidth,
ZeroHeight,
ZeroRectangleWidth,
ZeroRectangleHeight,
RectangleOutOfBounds,
ZeroRows,
ZeroRowBytes,
ZeroAlignment,
StrideTooSmall,
OffsetMisaligned,
StrideMisaligned,
NoPlanes,
PlanesOutOfOrder,
PlaneOverlap,
Arithmetic(MathError),
}
impl fmt::Display for GeometryError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ZeroWidth => formatter.write_str("image width must be nonzero"),
Self::ZeroHeight => formatter.write_str("image height must be nonzero"),
Self::ZeroRectangleWidth => formatter.write_str("rectangle width must be nonzero"),
Self::ZeroRectangleHeight => formatter.write_str("rectangle height must be nonzero"),
Self::RectangleOutOfBounds => {
formatter.write_str("rectangle exceeds its image dimensions")
}
Self::ZeroRows => formatter.write_str("plane row count must be nonzero"),
Self::ZeroRowBytes => formatter.write_str("plane row bytes must be nonzero"),
Self::ZeroAlignment => formatter.write_str("plane alignment must be nonzero"),
Self::StrideTooSmall => formatter.write_str("row stride is smaller than row bytes"),
Self::OffsetMisaligned => formatter.write_str("plane offset is not aligned"),
Self::StrideMisaligned => formatter.write_str("row stride is not aligned"),
Self::NoPlanes => formatter.write_str("plane set must be nonempty"),
Self::PlanesOutOfOrder => formatter.write_str("plane offsets are out of order"),
Self::PlaneOverlap => formatter.write_str("plane extents overlap"),
Self::Arithmetic(error) => write!(formatter, "geometry arithmetic failed: {error}"),
}
}
}
impl core::error::Error for GeometryError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Arithmetic(error) => Some(error),
_ => None,
}
}
}
pub type GeometryResult<T> = Result<T, GeometryError>;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum StorageError {
ZeroSampleBits,
PackedSampleTooWide,
SampleBitsExceedStorage,
SignedSampleNeedsValueBit,
InvalidFloatingStorage,
}
impl fmt::Display for StorageError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ZeroSampleBits => formatter.write_str("sample bits must be nonzero"),
Self::PackedSampleTooWide => {
formatter.write_str("packed samples cannot exceed 64 bits")
}
Self::SampleBitsExceedStorage => {
formatter.write_str("sample bits exceed the selected storage unit")
}
Self::SignedSampleNeedsValueBit => {
formatter.write_str("signed samples require a sign bit and a value bit")
}
Self::InvalidFloatingStorage => {
formatter.write_str("floating samples require a full 16-, 32-, or 64-bit word")
}
}
}
}
impl core::error::Error for StorageError {}
pub type StorageResult<T> = Result<T, StorageError>;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum PixelLayoutError {
PlaneCountMismatch,
PlaneRowsMismatch,
PlaneRowBytesMismatch,
Geometry(GeometryError),
Arithmetic(MathError),
}
impl fmt::Display for PixelLayoutError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::PlaneCountMismatch => formatter.write_str("pixel layout plane count mismatch"),
Self::PlaneRowsMismatch => formatter.write_str("pixel layout plane rows mismatch"),
Self::PlaneRowBytesMismatch => {
formatter.write_str("pixel layout plane row bytes mismatch")
}
Self::Geometry(error) => write!(formatter, "pixel layout geometry failed: {error}"),
Self::Arithmetic(error) => write!(formatter, "pixel layout arithmetic failed: {error}"),
}
}
}
impl core::error::Error for PixelLayoutError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Geometry(error) => Some(error),
Self::Arithmetic(error) => Some(error),
Self::PlaneCountMismatch | Self::PlaneRowsMismatch | Self::PlaneRowBytesMismatch => {
None
}
}
}
}
impl From<GeometryError> for PixelLayoutError {
fn from(error: GeometryError) -> Self {
Self::Geometry(error)
}
}
pub type PixelLayoutResult<T> = Result<T, PixelLayoutError>;