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
use std::error::Error;

/// An error returned by handler
pub type HandlerError = Box<dyn Error + Send>;

/// Result of a handler
#[derive(Debug)]
pub enum HandlerResult {
    /// Continue propagation
    ///
    /// Next handler (if exists) will run after current has finished
    Continue,
    /// Stop propagation
    ///
    /// Next handler (if exists) will not run after current has finished
    Stop,
    /// An error has occurred
    ///
    /// This error will be passed to [ErrorHandler](trait.ErrorHandler.html).
    /// If error handler returned [ErrorPolicy::Continue](enum.ErrorPolicy.html),
    /// next handler will run after current has finished
    /// For `ErrorPolicy::Stop` next handler will not run (default behavior).
    Error(HandlerError),
}

impl HandlerResult {
    /// Creates an error result
    pub fn error<E>(err: E) -> Self
    where
        E: Error + Send + 'static,
    {
        HandlerResult::Error(Box::new(err))
    }
}

impl From<()> for HandlerResult {
    fn from(_: ()) -> Self {
        HandlerResult::Continue
    }
}

impl<T, E> From<Result<T, E>> for HandlerResult
where
    T: Into<HandlerResult>,
    E: Error + Send + Sync + 'static,
{
    fn from(result: Result<T, E>) -> Self {
        match result {
            Ok(res) => res.into(),
            Err(err) => HandlerResult::error(err),
        }
    }
}