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

#[derive(Debug)]
pub enum Error {
    ZeroTotalWeights,
    Internal { text: String },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::ZeroTotalWeights => write!(f, "Total of weights is 0."),
            Error::Internal { ref text } => write!(f, "Internal error: {}", text),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::ZeroTotalWeights => "Total of weights is 0.",
            Error::Internal { ref text } => text,
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            Error::ZeroTotalWeights => None,
            Error::Internal { text: _ } => None,
        }
    }
}