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
use std::fmt;
use std::path::PathBuf;
use error::{ Error, Caused };

#[derive(Clone, Debug)]
pub enum EngineError {
    TemplateNotFound { name: String, search_paths: Vec<PathBuf> },
}

impl EngineError {
    pub fn caused_by<I: Into<Error>>(self, cause: I) -> Caused<EngineError> {
        Caused::new(self, Some(cause.into()))
    }
}

impl fmt::Display for EngineError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            EngineError::TemplateNotFound { ref name, ref search_paths } => {
                if search_paths.len() == 0 {
                    write!(f, "Template \"{}\" was not found", name)
                } else {
                    try!(write!(f, "Template \"{}\" was not found, looked in ", name));
                    write!(f, "{:?}", search_paths)
                }
            }
        }
    }
}

impl From<EngineError> for Error {
    fn from(inner: EngineError) -> Error {
        Error::Engine(Caused::new(inner, None))
    }
}

impl From<Caused<EngineError>> for Error {
    fn from(inner: Caused<EngineError>) -> Error {
        Error::Engine(inner)
    }
}