tiered-result 0.1.0

A different approach to error handling
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! This crate is an alternate strategy for error handling for libraries.
//!
//! Public functions that might want to keep trying in case of error, or report
//! multiple errors can take an additional parameter `&mut dyn ErrorHandler`. When
//! they encounter an error that they can sort-of recover from, they can invoke the
//! error handler (which calls back into the library user's code) and the error
//! handler gets to decide if the function should keep going or stop early.
//!
//! When the error handler says to return early, this is considered a fatal error
//! and all further processing and recovery should stop, and the library should
//! return control to the caller as quickly as possibly.
//!
//! If the error handler says to continue but the error does not allow the
//! calculation to finish, it should return null. This leads to the four variants in
//! a [`TieredResult`]: [`Ok`], [`Err`], [`Null`], and [`Fatal`].
//!
//! Your library code might look something like this:
//! ```rust
//! use tiered_result::TieredResult;
//! use std::fmt::{Display, Formatter};
//! pub use tiered_result::{ErrorHandler, ClientResponse};
//! use nullable_result::NullableResult;
//!
//! pub fn public_fn(handler: &mut dyn ErrorHandler<Error, Fatality>) -> Option<i64> {
//!     match private_fn(handler) {
//!         TieredResult::Ok(n) => Some(i64::from(n)),
//!         TieredResult::Err(_) => Some(-1),
//!         TieredResult::Null => None,
//!         TieredResult::Fatal(_) => Some(-2)
//!     }
//! }
//!
//! fn private_fn(handler: &mut dyn ErrorHandler<Error, Fatality>) -> TieredResult<u32, Error, Fatality> {//!
//!     let n = another_private_fn(handler)?; // <-- this `?` bubbles up the fatal error
//!                                           //     leaving a nullable result behind
//!     let n = n?; // <-- this `?` bubbles up the null/err.
//!     // the previous two lines could be written as let n = another_private_fn(handler)??;
//!
//!     if n == 42 {
//!         handler.handle_error(Error("we don't like 42".to_owned()))?;
//!     }
//!     TieredResult::Ok(n + 5)
//! }
//!
//! fn another_private_fn(handler: &mut dyn ErrorHandler<Error, Fatality>) -> TieredResult<u32, Error, Fatality> {
//!     // --snip--
//!     # TieredResult::Ok(2)
//! }
//!
//! // a struct to represent fatal errors, can carry additional info if you need it to
//! struct Fatality;
//!
//! #[derive(Clone, Debug)]
//! struct Error(String); // any old error type
//!
//! impl std::error::Error for Error {}
//! impl Display for Error {
//!     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
//!        write!(f, "There was an error: {}", self.0)
//!     }
//! }
//! ```
//!
//! The calling application might look like this:
//! ```rust
//! # mod the_lib_from_before {
//! #     pub use tiered_result::{ErrorHandler, ClientResponse};
//! #     pub struct Error;
//! #     pub struct Fatality;
//! #     pub fn public_fn(handler: &mut dyn ErrorHandler<Error, Fatality>) -> Option<i64> {
//! #         Some(5)
//! #     }
//! # }
//! use the_lib_from_before::*;
//!
//! #[derive(Default)]
//! struct Handler(u8);
//!
//! impl ErrorHandler<Error, Fatality> for Handler {
//!     fn handle_error(&mut self, error: Error) -> ClientResponse<Fatality, ()> {
//!         if self.0 > 2 { // allow two errors before giving up
//!             ClientResponse::Throw(Fatality)
//!         } else {
//!             ClientResponse::Continue(())
//!         }
//!     }
//! }
//!
//! let mut handler = Handler::default();
//! println!("{:?}", public_fn(&mut handler));
//! ```
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(try_trait_v2)]
#![warn(missing_docs)]

mod try_trait;

use self::TieredResult::*;
use core::{convert::Infallible, fmt::Debug, ops::Deref};
use nullable_result::NullableResult;

/// This trait should be part of your public API so that your users can implement
/// it for their own types.
pub trait ErrorHandler<E, F, C = ()> {
    /// Take an error and decide if the library should continue without the result
    /// that was supposed to have been produced.
    fn handle_error(&mut self, error: E) -> ClientResponse<F, C>;
}

/// A result type to be used internally by your library.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[must_use]
pub enum TieredResult<T, E, F> {
    /// Equivalent to [`Result::Ok`] or [`NullableResult::Ok`]
    Ok(T),

    /// Equivalent to [`Result::Err`] or [`NullableResult::Err`]
    Err(E),

    /// Equivalent to [`NullableResult::Null`]
    Null,

    /// This variant will be produced by the `?` operator a a [`ClientResponse`].
    /// It can be used to easily bubble up to your API boundary, then converted so
    /// something your users can handle.
    Fatal(F),
}

/// A result type to be used internally by your library.
///
/// This can be used when one of your functions doesn't produce any errors of its
/// own, but might need to bubble up a fatal error.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[must_use]
pub enum FatalResult<T, F> {
    /// See [`TieredResult::Ok`]
    Ok(T),

    /// See [`TieredResult::Null`]
    Null,

    /// See [`TieredResult::Fatal`]
    Fatal(F),
}

/// A result type to be used internally by your library.
///
/// This can be used when one of your functions doesn't do any fallible operations
/// directly, but still needs to bubble a fatal error up from its callees.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[must_use]
pub enum FatalOrOk<T, F> {
    /// See [`TieredResult::Ok`]
    Ok(T),

    /// See [`TieredResult::Fatal`]
    Fatal(F),
}

/// The return type of [`ErrorHandler::handle_error`]. This type (or a suitable alias)
/// should be part of your library's public interface. Additionally you may want to
/// provide methods or constants for your users' convenience.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[must_use]
pub enum ClientResponse<F, C = ()> {
    /// Stop processing
    Throw(F),

    /// Try to continue
    Continue(C),
}

impl<T, E, F> Default for TieredResult<T, E, F> {
    fn default() -> Self {
        Null
    }
}

impl<T, E: Debug, F> TieredResult<T, E, F> {
    /// Panics if it's not `Ok`, otherwise returns the contained value.
    #[inline]
    #[track_caller]
    pub fn unwrap(self) -> T {
        match self {
            Ok(item) => item,
            Err(err) => panic!(
                "tried to unwrap a tiered result containing Err: {:?}",
                err
            ),
            Null => {
                panic!("tried to unwrap a tiered result containing `Null`")
            }
            Fatal(_) => {
                panic!("tried to unwrap a tiered result containing `Fatal`")
            }
        }
    }
}

impl<T: Default, E, F> TieredResult<T, E, F> {
    /// Returns the contained value if it's `Ok`, otherwise returns the default value
    /// for that type.
    #[inline]
    pub fn unwrap_or_default(self) -> T {
        match self {
            Ok(item) => item,
            _ => T::default(),
        }
    }
}

impl<T: Copy, E, F> TieredResult<&'_ T, E, F> {
    /// Returns a [`TieredResult`] with the [`Ok`] part copied.
    #[inline]
    pub fn copied(self) -> TieredResult<T, E, F> {
        self.map(|&item| item)
    }
}

impl<T: Copy, E, F> TieredResult<&'_ mut T, E, F> {
    /// Returns a [`TieredResult`] with the [`Ok`] part copied.
    #[inline]
    pub fn copied(self) -> TieredResult<T, E, F> {
        self.map(|&mut item| item)
    }
}

impl<T: Clone, E, F> TieredResult<&'_ T, E, F> {
    /// Returns a [`TieredResult`] with the [`Ok`] part cloned.
    #[inline]
    pub fn cloned(self) -> TieredResult<T, E, F> {
        self.map(|item| item.clone())
    }
}

impl<T: Clone, E, F> TieredResult<&'_ mut T, E, F> {
    /// Returns a [`NullableResult`] with the [`Ok`] part cloned.
    #[inline]
    pub fn cloned(self) -> TieredResult<T, E, F> {
        self.map(|item| item.clone())
    }
}

impl<T: Deref, E, F: Clone> TieredResult<T, E, F> {
    /// Coerce the [`Ok`] variant of the original result with [`Deref`] and returns the
    /// new [`TieredResult`]
    #[inline]
    pub fn as_deref(&self) -> TieredResult<&T::Target, &E, F> {
        match self {
            Ok(item) => Ok(item.deref()),
            Err(err) => Err(err),
            Null => Null,
            Fatal(fatality) => Fatal(fatality.clone()),
        }
    }
}

impl<T, E, F: Clone> TieredResult<T, E, F> {
    /// Convert from a `TieredResult<T, E, F>` or `&TieredResult<T, E, F>` to a
    /// `TieredResult<&T, &E, F>`.
    #[inline]
    pub fn as_ref(&self) -> TieredResult<&T, &E, F> {
        match self {
            Ok(item) => Ok(item),
            Err(err) => Err(err),
            Null => Null,
            Fatal(fatality) => Fatal(fatality.clone()),
        }
    }

    /// Convert from a `mut TieredResult<T, E, F>` or `&mut NullableResult<T, E, F>` to a
    /// `TieredResult<&mut T, &mut E, F>`.
    #[inline]
    pub fn as_mut(&mut self) -> TieredResult<&mut T, &mut E, F> {
        match self {
            Ok(item) => Ok(item),
            Err(err) => Err(err),
            Null => Null,
            Fatal(fatality) => Fatal(fatality.clone()),
        }
    }
}

impl<T, E, F> TieredResult<T, E, F> {
    /// Returns `true` if this result is an [`Ok`] value
    #[inline]
    #[must_use]
    pub fn is_ok(&self) -> bool {
        matches!(self, Ok(_))
    }

    /// Returns `true` if this result is an [`Err`] value
    #[inline]
    #[must_use]
    pub fn is_err(&self) -> bool {
        matches!(self, Err(_))
    }

    /// Returns `true` if this result is a [`Null`] value
    #[inline]
    #[must_use]
    pub fn is_null(&self) -> bool {
        matches!(self, Null)
    }

    /// Returns `true` if this result is [`Fatal`] value
    #[inline]
    #[must_use]
    pub fn is_fatal(&self) -> bool {
        matches!(&self, Fatal(_))
    }

    /// Returns the contained [`Ok`] value, consuming `self`.
    ///
    /// # Panics
    /// Panics if the value is not [`Ok`] with the provided message.
    #[inline]
    #[track_caller]
    pub fn expect(self, msg: &str) -> T {
        match self {
            Ok(item) => item,
            _ => panic!("{}", msg),
        }
    }

    /// Returns the contained value if it's [`Ok`], returns `item` otherwise.
    #[inline]
    pub fn unwrap_or(self, item: T) -> T {
        match self {
            Ok(item) => item,
            _ => item,
        }
    }

    /// Returns the contained value if it's [`Ok`], otherwise, it calls `f` and forwards
    /// its return value.
    #[inline]
    pub fn unwrap_or_else<Func: FnOnce() -> T>(self, f: Func) -> T {
        match self {
            Ok(item) => item,
            _ => f(),
        }
    }

    /// Replace a fatal error with [`NullableResult::Null`]
    #[inline]
    pub fn recover_as_null(self) -> NullableResult<T, E> {
        self.optional_nullable_result()
            .unwrap_or(NullableResult::Null)
    }

    /// Replace a fatal error with the provided error
    #[inline]
    pub fn recover_as_err(self, err: E) -> NullableResult<T, E> {
        self.optional_nullable_result()
            .unwrap_or(NullableResult::Err(err))
    }

    /// Replace a fatal error with the error returned by the provided function.
    #[inline]
    pub fn recover_as_err_with<Func: FnOnce() -> E>(
        self,
        f: Func,
    ) -> NullableResult<T, E> {
        self.optional_nullable_result()
            .unwrap_or_else(|| NullableResult::Err(f()))
    }

    #[inline]
    fn optional_nullable_result(self) -> Option<NullableResult<T, E>> {
        match self {
            Ok(item) => Some(NullableResult::Ok(item)),
            Err(err) => Some(NullableResult::Err(err)),
            Null => Some(NullableResult::Null),
            Fatal(_) => None,
        }
    }

    /// Maps to a [`TieredResult`] with a different ok type.
    #[inline]
    pub fn map<U, Func: FnOnce(T) -> U>(
        self,
        f: Func,
    ) -> TieredResult<U, E, F> {
        match self {
            Ok(item) => Ok(f(item)),
            Err(err) => Err(err),
            Null => Null,
            Fatal(fatality) => Fatal(fatality),
        }
    }

    /// Maps to a [`TieredResult`] with a different err type.
    #[inline]
    pub fn map_err<U, Func: FnOnce(E) -> U>(
        self,
        f: Func,
    ) -> TieredResult<T, U, F> {
        match self {
            Ok(item) => Ok(item),
            Err(err) => Err(f(err)),
            Null => Null,
            Fatal(fatality) => Fatal(fatality),
        }
    }

    /// Maps to a [`TieredResult`] with a different fatality type.
    #[inline]
    pub fn map_fatal<U, Func: FnOnce(F) -> U>(
        self,
        f: Func,
    ) -> TieredResult<T, E, U> {
        match self {
            Ok(item) => Ok(item),
            Err(err) => Err(err),
            Null => Null,
            Fatal(fatality) => Fatal(f(fatality)),
        }
    }

    /// If `self` is [`Ok`], returns `res`, keeps the value of `self` otherwise.
    #[inline]
    pub fn and<U>(self, res: TieredResult<U, E, F>) -> TieredResult<U, E, F> {
        match self {
            Ok(_) => res,
            Err(err) => Err(err),
            Null => Null,
            Fatal(fatality) => Fatal(fatality),
        }
    }

    /// Calls `op` if the result is [`Ok`], otherwise returns the value of `self`.
    #[inline]
    pub fn and_then<U, Func>(self, op: Func) -> TieredResult<U, E, F>
    where
        Func: FnOnce(T) -> TieredResult<U, E, F>,
    {
        match self {
            Ok(item) => op(item),
            Err(err) => Err(err),
            Null => Null,
            Fatal(fatality) => Fatal(fatality),
        }
    }
}

impl<T, E, F> TieredResult<TieredResult<T, E, F>, E, F> {
    /// Convert from `TieredResult<TieredResult<T, E>, E>` to
    /// `TieredResult<T, E>`.
    #[inline]
    pub fn flatten(self) -> TieredResult<T, E, F> {
        match self {
            Ok(Ok(item)) => Ok(item),
            Ok(Err(err)) | Err(err) => Err(err),
            Ok(Null) | Null => Null,
            Ok(Fatal(fatality)) | Fatal(fatality) => Fatal(fatality),
        }
    }
}

/// Analogue of [TryFrom] that returns a [`TieredResult`]
pub trait TryFromFatal<T>: Sized {
    /// The type that is returned if conversion fails
    type Error;

    /// The type that is returned if conversion fails fatally
    type Fatality;

    /// Convert a `T` to [`TieredResult<Self, Self::Error, Self::Fatality>`]
    fn try_from_fatal(
        item: T,
    ) -> TieredResult<Self, Self::Error, Self::Fatality>;
}

/// Analogue of [TryInto] that returns a [`TieredResult`]
pub trait TryIntoFatal<T>: Sized {
    /// The type that is returned if conversion fails
    type Error;

    /// The type that is returned if conversion fails fatally
    type Fatality;

    /// Convert a `Self` to [`TieredResult<T, Self::Error, Self::Fatality>`]
    fn try_into_fatal(self) -> TieredResult<T, Self::Error, Self::Fatality>;
}

impl<T, U: nullable_result::MaybeTryFrom<T>> TryFromFatal<T> for U {
    type Error = U::Error;
    type Fatality = Infallible;

    #[inline]
    fn try_from_fatal(
        item: T,
    ) -> TieredResult<Self, Self::Error, Self::Fatality> {
        Ok(U::maybe_try_from(item)?)
    }
}

impl<T, U: TryFromFatal<T>> TryIntoFatal<U> for T {
    type Error = U::Error;
    type Fatality = U::Fatality;

    #[inline]
    fn try_into_fatal(self) -> TieredResult<U, Self::Error, Self::Fatality> {
        U::try_from_fatal(self)
    }
}

impl<T, F> FatalOrOk<T, F> {
    /// Return the contained item
    ///
    /// # Panics
    /// Panics if the value contains a fatal error.
    #[inline]
    #[track_caller]
    pub fn unwrap(self) -> T {
        use FatalOrOk::*;

        match self {
            Ok(item) => item,
            Fatal(_) => {
                panic!("tried to unwrap a tiered result containing `Fatal`")
            }
        }
    }
}