caldyn/
error.rs

1use std::error;
2use std::fmt::{self, Display, Formatter};
3
4/// Error type for the caldyn crate
5#[derive(Debug, Clone, PartialEq)]
6pub enum Error {
7    /// Error while parsing an expression
8    ParseError(String),
9    /// Unknown variable during evaluation
10    NameError(String),
11}
12
13impl Display for Error {
14    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
15        match *self {
16            Error::ParseError(ref message) => write!(fmt, "ParseError: {}", message),
17            Error::NameError(ref message) => write!(fmt, "NameError: {}", message),
18        }
19    }
20}
21
22#[allow(match_same_arms)]
23impl error::Error for Error {
24    fn description(&self) -> &str {
25        match *self {
26            Error::ParseError(ref message) => message,
27            Error::NameError(ref message) => message,
28        }
29    }
30
31    fn cause(&self) -> Option<&error::Error> {
32        match *self {
33            Error::ParseError(_) => None,
34            Error::NameError(_) => None,
35        }
36    }
37}