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
use std::fmt;
use std::fmt::Display;

use failure::{Backtrace, Context, Fail};

#[derive(Debug)]
pub struct TodoError {
    inner: Context<TodoErrorKind>,
}

#[derive(Clone, Eq, PartialEq, Debug, Fail)]
pub enum TodoErrorKind {
    #[fail(display = "invalid value {} for {}", value, name)]
    InvalidValue { value: String, name: String },
    #[fail(display = "failed to save todo list")]
    SaveFailed,
    #[fail(display = "failed to load todo list")]
    LoadFailed,
    #[fail(display = "failed to append to file")]
    AppendFailed,
    #[fail(display = "failed to write todo list")]
    FileWriteFailed,
    #[fail(display = "I/O Error: {}", err)]
    IOError { err: String },
}

impl Fail for TodoError {
    fn cause(&self) -> Option<&Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl Display for TodoError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        Display::fmt(&self.inner, f)
    }
}

impl TodoError {
    pub fn kind(&self) -> TodoErrorKind {
        self.inner.get_context().clone()
    }
}

impl From<TodoErrorKind> for TodoError {
    fn from(kind: TodoErrorKind) -> TodoError {
        TodoError {
            inner: Context::new(kind),
        }
    }
}

impl From<Context<TodoErrorKind>> for TodoError {
    fn from(inner: Context<TodoErrorKind>) -> TodoError {
        TodoError { inner }
    }
}