summaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: 90c894c88fb59b2e3cadd96065e3756a9f40879e (plain)
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
use std::error::Error;
use std::fmt::Error as FmtError;
use std::clone::Clone;
use std::fmt::{Display, Formatter};

/**
 * Kind of store error
 */
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TaskErrorKind {
    ParserError,
    NoStatus,
}

fn store_error_type_as_str(e: &TaskErrorKind) -> &'static str {
    match e {
        &TaskErrorKind::ParserError => "Parser Error",
        &TaskErrorKind::NoStatus    => "Task status is missing",
    }
}

impl Display for TaskErrorKind {

    fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
        try!(write!(fmt, "{}", store_error_type_as_str(self)));
        Ok(())
    }

}

/**
 * Store error type
 */
#[derive(Debug)]
pub struct TaskError {
    err_type: TaskErrorKind,
    cause: Option<Box<Error>>,
}

impl TaskError {

    /**
     * Build a new TaskError from an TaskErrorKind, optionally with cause
     */
    pub fn new(errtype: TaskErrorKind, cause: Option<Box<Error>>)
        -> TaskError
        {
            TaskError {
                err_type: errtype,
                cause: cause,
            }
        }

    /**
     * Get the error type of this TaskError
     */
    pub fn err_type(&self) -> TaskErrorKind {
        self.err_type.clone()
    }

}

impl Display for TaskError {

    fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
        try!(write!(fmt, "[{}]", store_error_type_as_str(&self.err_type.clone())));
        Ok(())
    }

}

impl Error for TaskError {

    fn description(&self) -> &str {
        store_error_type_as_str(&self.err_type.clone())
    }

    fn cause(&self) -> Option<&Error> {
        self.cause.as_ref().map(|e| &**e)
    }

}