devela/media/color/error.rs
1// devela::media::color::error
2//
3//!
4//
5
6#[doc = crate::TAG_RESULT!()]
7/// A color-related result.
8pub type ColorResult<T> = crate::Result<T, ColorError>;
9
10#[doc = crate::TAG_ERROR_COMPOSITE!()]
11/// A color-related error.
12#[non_exhaustive]
13#[derive(Clone, Copy, Debug, PartialEq, Eq)] // Hash
14pub enum ColorError {
15 /// The requested chromatic functionality is not implemented.
16 ///
17 /// This is the default implementation of every `Color` method.
18 NotImplemented,
19
20 /// The requested chromatic functionality is not supported by this color type.
21 NotSupported,
22 // General validation errors
23 // EmptyData,
24 // InvalidRange { lower: f64, upper: f64 },
25
26 // Numeric representation issues
27 // InvalidComponent { value: f64 },
28 // ConversionError { from: &'static str, to: &'static str },
29 // OutOfBounds { index: usize, size: usize },
30
31 // FUTURE:
32
33 // NumericOverflow { max: f64, value: f64 },
34 // NumericUnderflow { min: f64, value: f64 },
35 //
36 // // Color space conversion errors
37 // UnsupportedColorSpace { from: &'static str, to: &'static str },
38 // MetamerismLoss,
39 //
40 // // Spectral-specific errors
41 // InvalidWavelength { lambda: f64, range: Interval<f64> },
42 // SamplingError { lambda: f64 },
43 //
44 // // Blending and arithmetic errors
45 // MismatchedDimensions { expected: usize, found: usize },
46 // DivisionByZero,
47 //
48 // // Allocation errors
49 // InsufficientBufferSize { required: usize, provided: usize },
50 //
51 // // Miscellaneous
52 // UnimplementedOperation { operation: &'static str },
53 // Other { description: String },
54}
55
56#[allow(dead_code)]
57impl ColorError {
58 pub(crate) const fn ni<T>() -> ColorResult<T> {
59 Err(ColorError::NotImplemented)
60 }
61 pub(crate) const fn ns<T>() -> ColorResult<T> {
62 Err(ColorError::NotSupported)
63 }
64}
65
66mod core_impls {
67 use crate::{ColorError, Display, FmtResult, Formatter};
68
69 impl crate::Error for ColorError {}
70 impl crate::ExtError for ColorError {
71 type Kind = ();
72 fn error_eq(&self, other: &Self) -> bool {
73 self == other
74 }
75 fn error_kind(&self) -> Self::Kind {}
76 }
77
78 impl Display for ColorError {
79 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult<()> {
80 use ColorError as E;
81 match self {
82 E::NotImplemented => write!(f, "Not implemented."),
83 E::NotSupported => write!(f, "Not supported."),
84 }
85 }
86 }
87}
88
89// TODO: make conversions from-to NumError, DataError...
90// DECIDE: TryFrom? or From with a fallback? NotImplemented? ConversionError?
91// (it should not be ambiguous, so that fallback error shouldn't be expected otherwise)