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

#[derive(Debug, Clone)]
pub struct UserError<'a> {
    message: &'a str,
}

impl<'a> UserError<'a> {
    pub fn new(message: &'a str) -> UserError<'a> {
        UserError { message }
    }

    pub fn boxed(message: &'a str) -> Box<UserError<'a>> {
        Box::new(UserError::new(message))
    }
}

impl<'a> std::fmt::Display for UserError<'a> {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "{}", self.message)
    }
}

impl<'a> Error for UserError<'a> {
    fn description(&self) -> &str {
        &self.message
    }

    fn cause(&self) -> Option<&Error> {
        None
    }
}