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

use std::fmt;
use std::error;

#[derive(Debug, PartialEq, Clone, Copy)]
/// Kinds of nccl errors.
pub enum ErrorKind {
    KeyNotFound,
    IndentationError,
    IntoError,
    NameError,
    NoValue,
    MultipleValues,
    ParseError,
    FromStrError,
    FileError,
}

#[derive(Debug, PartialEq)]
/// nccl error type.
pub struct NcclError {
    kind: ErrorKind,
    line: u64,
    message: String,
}

impl NcclError {
    /// Creates a new NcclError.
    pub fn new(kind: ErrorKind, message: &str, line: u64) -> Self {
        NcclError {
            kind: kind,
            message: match kind {
                ErrorKind::ParseError | ErrorKind::IndentationError
                    => format!("An error has ocurred: {:?} on line {}\n\t{}", kind, line, message),
                _ => format!("An error has ocurred: {:?}\n\t{}", kind, message)
            },
            line: line,
        }
    }
}

impl fmt::Display for NcclError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.kind {
            ErrorKind::ParseError | ErrorKind::IndentationError
                => write!(f, "An error has ocurred: {:?} on line {}\n\t{}", self.kind, self.line, self.message),
            _ => write!(f, "An error has ocurred: {:?}\n\t{}", self.kind, self.message)
        }
    }
}

impl error::Error for NcclError {
    fn description(&self) -> &str {
        &self.message
    }
}