devela/code/result/error/
all_error.rs

1// devela::code::result::error::all_error
2//
3//! Defines the [`AllError`] enum.
4//
5// TOC
6// - individual errors:
7//   - InvalidErrorConversion
8//   - NotImplemented
9//   - NotSupported
10// - composite errors:
11//   - AllResult
12//   - AllError
13//
14// RETHINK: make all errors flat?
15
16use crate::impl_error;
17
18impl_error![individual: pub struct FailedErrorConversion;
19    DOC_FAILED_CONVERSION = "A failed conversion between two error types.",
20    self+f => write!(f, "Failed to convert between error types"),
21];
22impl_error![individual: pub struct NotImplemented;
23    DOC_NOT_IMPLEMENTED = "The requested functionality is not implemented.",
24    self+f => write!(f, "The requested functionality is not implemented."),
25];
26impl_error![individual: pub struct NotSupported;
27    DOC_NOT_SUPPORTED = "The requested functionality is not supported by this type.",
28    self+f => write!(f, "The requested functionality is not supported by this type."),
29];
30
31/* composite errors: */
32
33impl_error! { composite: fmt(f)
34    /// An error composite of [`NotImplemented`] + [`NotSupported`].
35    ///
36    /// Used in methods of:
37    /// - [`DataCollection`][crate::DataCollection].
38    pub enum NotAvailable {
39        DOC_NOT_IMPLEMENTED: NotImplemented => NotImplemented,
40        DOC_NOT_SUPPORTED: NotSupported => NotSupported,
41    }
42}
43
44#[cfg(feature = "error")]
45pub use full_composite::*;
46#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "error")))]
47#[cfg(feature = "error")]
48mod full_composite {
49    use super::super::reexports::crate_errors::*;
50    // use super::*;
51
52    #[doc = crate::TAG_RESULT!()]
53    /// The root result type, aggregating all module-specific results.
54    pub type AllResult<T> = crate::Result<T, AllError>;
55
56    #[doc = crate::TAG_ERROR_COMPOSITE!()]
57    /// The root error type, aggregating all module-specific errors.
58    ///
59    /// This error is designed to encompass all possible errors within the library's domain,
60    /// providing a unified interface for error handling across modules.
61    ///
62    /// See also: [`AllErrorKind`].
63    #[derive(Debug)]
64    pub enum AllError {
65        /// A data-related error.
66        #[cfg(data··)]
67        #[cfg_attr(feature = "nightly_doc", doc(cfg(data··)))]
68        Data(DataError),
69
70        /// A media-related error.
71        #[cfg(media··)]
72        #[cfg_attr(feature = "nightly_doc", doc(cfg(media··)))]
73        Media(MediaError),
74
75        /// A numeric-related error.
76        Num(NumError),
77
78        // IMPROVE Sys
79        /// An I/O-related error.
80        #[cfg(feature = "io")]
81        #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "io")))]
82        Io(IoError),
83
84        /// A text-related error.
85        #[cfg(text··)]
86        #[cfg_attr(feature = "nightly_doc", doc(cfg(text··)))]
87        Text(TextError),
88
89        // IMPROVE Phys
90        /// A time-related error.
91        #[cfg(feature = "time")]
92        #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "time")))]
93        Time(TimeError),
94
95        /// Other static error.
96        Other(&'static str),
97    }
98
99    #[doc = crate::TAG_ERROR_COMPOSITE!()]
100    /// The kind of root error type, aggregating all module-specific error kinds.
101    ///
102    /// See also: [`AllError`].
103    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
104    pub enum AllErrorKind {
105        /// A data-related error.
106        #[cfg(data··)]
107        #[cfg_attr(feature = "nightly_doc", doc(cfg(data··)))]
108        Data(()), // TODO
109
110        /// A media-related error.
111        #[cfg(media··)]
112        #[cfg_attr(feature = "nightly_doc", doc(cfg(media··)))]
113        Media(()), // TODO
114        //
115        /// A numeric-related error.
116        Num(()), // TODO
117
118        /// An I/O error.
119        #[cfg(feature = "io")]
120        #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "io")))]
121        Io(IoErrorKind),
122
123        /// A time error.
124        #[cfg(feature = "time")]
125        #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "time")))]
126        Time(()), // TODO
127
128        /// A text-related error.
129        #[cfg(text··)]
130        #[cfg_attr(feature = "nightly_doc", doc(cfg(text··)))]
131        Text(()), // TODO
132
133        /// Other static error.
134        Other,
135        // /// No error kind.
136        // #[default]
137        // None, // MAYBE
138    }
139
140    mod core_impls {
141        use super::*;
142        use crate::impl_trait;
143
144        impl crate::Error for AllError {}
145        impl crate::ExtError for AllError {
146            type Kind = AllErrorKind;
147
148            fn error_eq(&self, other: &Self) -> bool {
149                use AllError as E;
150                match (self, other) {
151                    #[cfg(data··)]
152                    (E::Data(e1), E::Data(e2)) => e1.error_eq(e2),
153                    #[cfg(media··)]
154                    (E::Media(e1), E::Media(e2)) => e1.error_eq(e2),
155                    (E::Num(e1), E::Num(e2)) => e1.error_eq(e2),
156                    #[cfg(feature = "io")]
157                    (E::Io(e1), E::Io(e2)) => e1.error_eq(e2),
158                    #[cfg(feature = "time")]
159                    (E::Time(e1), E::Time(e2)) => e1.error_eq(e2),
160                    #[cfg(text··)]
161                    (E::Text(e1), E::Text(e2)) => e1.error_eq(e2),
162                    (E::Other(s1), E::Other(s2)) => s1 == s2,
163
164                    _ => false, // Different variants cannot be equal.
165                }
166            }
167            fn error_kind(&self) -> Self::Kind {
168                use {AllError as E, AllErrorKind as K};
169                #[expect(clippy::unit_arg, reason = "WIP () placeholder")]
170                match self {
171                    #[cfg(data··)]
172                    E::Data(e) => K::Data(e.error_kind()),
173                    #[cfg(media··)]
174                    E::Media(e) => K::Media(e.error_kind()),
175                    E::Num(e) => K::Num(e.error_kind()),
176                    #[cfg(feature = "io")]
177                    E::Io(e) => K::Io(e.error_kind()),
178                    #[cfg(feature = "time")]
179                    E::Time(e) => K::Time(e.error_kind()),
180                    #[cfg(text··)]
181                    E::Text(e) => K::Text(e.error_kind()),
182                    E::Other(_s) => K::Other,
183                }
184            }
185        }
186
187        impl_trait! { fmt::Display for AllError |self, f| {
188            use AllError as E;
189            match self {
190                #[cfg(data··)]
191                E::Data(e) => write!(f, "{e:?}"),
192                #[cfg(media··)]
193                E::Media(e) => write!(f, "{e:?}"),
194                E::Num(e) => write!(f, "{e:?}"),
195                #[cfg(feature = "io")]
196                E::Io(e) => write!(f, "{e:?}"),
197                #[cfg(feature = "time")]
198                E::Time(e) => write!(f, "{e:?}"),
199                #[cfg(text··)]
200                E::Text(e) => write!(f, "{e:?}"),
201                E::Other(s) => write!(f, "{s}"),
202            }
203        }}
204    }
205}