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

#[derive(Debug, Default)]
pub struct Error {
    pub write: Option<io::Error>,
    pub render: Vec<Box<::std::error::Error + Send + Sync>>,
}

#[inline]
pub fn is_empty(e: &Error) -> bool {
    e.write.is_none() && e.render.is_empty()
}

impl ::std::error::Error for Error {
    fn description(&self) -> &'static str {
        "Template rendering failed"
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Error {
        Error {
            write: Some(e),
            render: Vec::new(),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut displayed = false;
        if let Some(e) = self.write.as_ref() {
            displayed = true;
            try!(write!(f, "write error: {}", e))
        }
        if !self.render.is_empty() {
            displayed = true;
            try!(write!(f, "render errors: "));
            for i in 0..(self.render.len() - 1) {
                try!(write!(f, "{}, ", self.render[i]));
            }
            try!(write!(f, "{}", self.render.last().unwrap()))
        }
        if !displayed {
            try!(write!(f, "unspecified error"));
        }
        Ok(())
    }
}