dptree/handler/fallible.rs
1//! The [`Fallible`] trait, used by the fallible handlers
2
3/// A handler `Output` that can represent a failure.
4///
5/// The fallible handlers ([`try_filter`], [`try_map`], [`try_filter_map`])
6/// take a function that returns [`Result`]. On `Ok`, execution continues as
7/// usual; on `Err(e)`, the chain short-circuits and ends the dispatch with the
8/// error as the output; the same way [`endpoint`](crate::endpoint) ends
9/// it with a value.
10///
11/// [`Result`] implements this out of the box. Implement it yourself if your
12/// `Output` is another type that can still represent an error (e.g. an HTTP
13/// response that encodes it in the status code).
14///
15/// [`try_filter`]: crate::try_filter
16/// [`try_map`]: crate::try_map
17/// [`try_filter_map`]: crate::try_filter_map
18pub trait Fallible {
19 /// The error type carried by this output.
20 type Error: 'static;
21
22 /// Constructs an output value representing the given error.
23 fn from_error(error: Self::Error) -> Self;
24}
25
26impl<T, E: 'static> Fallible for Result<T, E> {
27 type Error = E;
28
29 fn from_error(error: E) -> Self {
30 Err(error)
31 }
32}