1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use std::error;
use std::fmt;

use crate::AdhocContext;
use crate::Chain;
use crate::Context;
use crate::InternalStatus;
use crate::Kind;
use crate::StdError;
use crate::StrictError;
use crate::Unkind;

/// A container for use in `Result<_, Status>`..
///
/// Goals:
/// - Easy crate inter-op while maintaining programmatic processing.
/// - User-friendly without losing helpful debug information.
///
/// Note: this is optimized for the happy-path.  When failing frequently inside of an inner loop,
/// consider using your [`Kind`] to convey your status.
///
/// # Example
///
/// ```rust
/// use status::Kind;
///
/// #[derive(Copy, Clone, Debug, derive_more::Display)]
/// enum ErrorKind {
///   #[display(fmt = "Failed to read file")]
///   Read,
///   #[display(fmt = "Failed to parse")]
///   Parse,
/// }
/// type Status = status::Status<ErrorKind>;
/// type Result<T, E = Status> = std::result::Result<T, E>;
///
/// fn read_file() -> Result<()> {
///     return ErrorKind::Read.into_err();
/// }
/// ```
#[derive(Debug)]
pub struct Status<K: Kind = Unkind, C: Context = AdhocContext> {
    pub(crate) inner: Box<StatusDetails<K, C>>,
}

#[derive(Debug)]
pub(crate) struct StatusDetails<K: Kind, C: Context> {
    pub(crate) kind: K,
    pub(crate) source: Source,
    pub(crate) data: C,
}

impl<K: Kind, C: Context> Status<K, C> {
    /// Create a container for the specified status [`Kind`].
    ///
    /// # Example
    ///
    /// ```
    /// fn read_file() -> Result<(), status::Status> {
    ///     return Err(status::Status::new("Failed to read file"));
    /// }
    /// ```
    pub fn new<U>(kind: U) -> Self
    where
        U: Into<K>,
    {
        Self {
            inner: Box::new(StatusDetails {
                kind: kind.into(),
                source: Source::Empty,
                data: Default::default(),
            }),
        }
    }

    /// Add a public error.
    #[cfg(feature = "send_sync")]
    pub fn with_source<E>(mut self, error: E) -> Self
    where
        E: error::Error + Send + Sync + 'static,
    {
        self.inner.source = Source::Public(Box::new(error));
        self
    }
    /// Add a public error.
    #[cfg(not(feature = "send_sync"))]
    pub fn with_source<E>(mut self, error: E) -> Self
    where
        E: error::Error + 'static,
    {
        self.inner.source = Source::Public(Box::new(error));
        self
    }

    #[cfg(feature = "send_sync")]
    /// Add an internal error.
    pub fn with_internal<E>(mut self, error: E) -> Self
    where
        E: error::Error + Send + Sync + 'static,
    {
        self.inner.source = Source::Private(Box::new(error));
        self
    }
    #[cfg(not(feature = "send_sync"))]
    /// Add an internal error.
    pub fn with_internal<E>(mut self, error: E) -> Self
    where
        E: error::Error + 'static,
    {
        self.inner.source = Source::Private(Box::new(error));
        self
    }

    /// Extend the [`Context`].
    pub fn context_with<F>(mut self, context: F) -> Self
    where
        F: Fn(C) -> C,
    {
        let mut data: C = Default::default();
        std::mem::swap(&mut data, &mut self.inner.data);
        let mut data = context(data);
        std::mem::swap(&mut data, &mut self.inner.data);
        self
    }

    /// Access the [`Context`] for programmatic usage.
    pub fn context(&self) -> &C {
        &self.inner.data
    }

    /// Programmatic identifier for which error occurred.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use status::Kind;
    /// #
    /// #[derive(Copy, Clone, Debug, derive_more::Display)]
    /// enum ErrorKind {
    ///   #[display(fmt = "Failed to read file")]
    ///   Read,
    ///   #[display(fmt = "Failed to parse")]
    ///   Parse,
    /// }
    /// # type Status = status::Status<ErrorKind>;
    /// # type Result<T, E = Status> = std::result::Result<T, E>;
    ///
    /// fn read_file() -> Result<()> {
    /// #     return ErrorKind::Read.into_err();
    /// }
    ///
    /// fn process_file() -> Result<()> {
    ///     match read_file() {
    ///         Ok(_) => Ok(()),
    ///         Err(e) => match e.kind() {
    ///           ErrorKind::Read => Err(e),
    ///           ErrorKind::Parse => Ok(()),
    ///         }
    ///     }
    /// }
    /// ```
    pub fn kind(&self) -> K {
        self.inner.kind
    }

    /// An iterator for the chain of sources.
    ///
    /// When debugging, to display internal sources, run [`Status::into_internal`].
    ///
    /// # Example
    ///
    /// ```
    /// use status::Status;
    /// use std::io;
    ///
    /// pub fn underlying_io_error_kind(error: &Status) -> Option<io::ErrorKind> {
    ///     for cause in error.sources() {
    ///         if let Some(io_error) = cause.downcast_ref::<io::Error>() {
    ///             return Some(io_error.kind());
    ///         }
    ///     }
    ///     None
    /// }
    /// ```
    pub fn sources(&self) -> Chain {
        Chain::new(error::Error::source(self))
    }

    /// The lowest level cause of this error &mdash; this error's cause's
    /// cause's cause etc.
    ///
    /// The root cause is the last error in the iterator produced by
    /// [`sources()`][Status::sources].
    ///
    /// # Example
    ///
    /// ```
    /// use status::Status;
    /// use std::io;
    ///
    /// pub fn underlying_io_error_kind(error: &Status) -> Option<io::ErrorKind> {
    ///     let cause = error.root_source()?;
    ///     if let Some(io_error) = cause.downcast_ref::<io::Error>() {
    ///         return Some(io_error.kind());
    ///     }
    ///     None
    /// }
    /// ```
    pub fn root_source(&self) -> Option<&StdError> {
        self.sources().last()
    }

    /// View of [`Status`], exposing implementation details.
    ///
    /// `Error::source` and [`InternalStatus::sources`] are for debug / display purposes only and
    /// relying on them programmatically is likely to break across minor releases.
    ///
    /// # Example
    ///
    /// ```
    /// fn display_status(status: status::Status) {
    ///     if std::env::var("DEBUG").as_ref().map(|s| s.as_ref()).unwrap_or("0") == "1" {
    ///         let status = status.into_internal();
    ///         println!("{}", status);
    ///     } else {
    ///         println!("{}", status);
    ///     }
    /// }
    /// ```
    pub fn into_internal(self) -> InternalStatus<K, C> {
        InternalStatus::new(self)
    }

    /// Convenience for returning an error.
    pub fn into_err<T>(self) -> Result<T, Self> {
        Err(self)
    }
}

impl<K: Kind, C: Context> fmt::Display for Status<K, C> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "{}", self.inner.kind)?;
        if !self.inner.data.is_empty() {
            writeln!(f)?;
            writeln!(f, "{}", self.inner.data)?;
        }
        Ok(())
    }
}

impl<K: Kind, C: Context> std::ops::Deref for Status<K, C> {
    type Target = C;

    fn deref(&self) -> &Self::Target {
        &self.inner.data
    }
}

impl<K: Kind, C: Context> std::ops::DerefMut for Status<K, C> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner.data
    }
}

impl<K: Kind, C: Context> error::Error for Status<K, C> {
    fn cause(&self) -> Option<&dyn error::Error> {
        self.inner.source.public()
    }

    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        self.inner.source.public()
    }
}

// impl From<Kind> is waiting on specialization

// impl From<Error> is waiting on specialization

#[derive(Debug)]
pub(crate) enum Source {
    Public(Box<StrictError>),
    Private(Box<StrictError>),
    Empty,
}

impl Source {
    pub(crate) fn public(&self) -> Option<&StdError> {
        match self {
            Self::Public(e) => Some(e.as_ref()),
            _ => None,
        }
    }

    pub(crate) fn any(&self) -> Option<&StdError> {
        match self {
            Self::Public(e) | Self::Private(e) => Some(e.as_ref()),
            _ => None,
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use static_assertions::*;

    #[test]
    fn source() {
        assert_impl_all!(Source: fmt::Debug);
        #[cfg(feature = "send_sync")]
        assert_impl_all!(Source: Send, Sync);
    }

    #[test]
    fn status() {
        assert_impl_all!(Status: fmt::Debug, fmt::Display, error::Error);
        #[cfg(feature = "send_sync")]
        assert_impl_all!(Status: Send, Sync);
    }
}