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
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Errors generated when rendering templates.
use crate::error::{HelperError, IoError};
use std::fmt;

pub enum RenderError {
    PartialNameResolve(String),
    PartialNotFound(String),
    Helper(HelperError),
    Io(IoError),
    Json(serde_json::Error),
}

impl From<HelperError> for RenderError {
    fn from(err: HelperError) -> Self {
        Self::Helper(err)
    }
}

impl From<std::io::Error> for RenderError {
    fn from(err: std::io::Error) -> Self {
        Self::Io(IoError::Io(err))
    }
}

impl From<serde_json::Error> for RenderError {
    fn from(err: serde_json::Error) -> Self {
        Self::Json(err)
    }
}

impl fmt::Display for RenderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::PartialNameResolve(ref name) => {
                write!(f, "Unable to resolve partial name from '{}'", name)
            }
            Self::PartialNotFound(ref name) => {
                write!(f, "Partial '{}' not found", name)
            }
            Self::Helper(ref e) => fmt::Display::fmt(e, f),
            Self::Io(ref e) => fmt::Debug::fmt(e, f),
            Self::Json(ref e) => fmt::Debug::fmt(e, f),
        }
    }
}

impl fmt::Debug for RenderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::PartialNameResolve(_) => fmt::Display::fmt(self, f),
            Self::PartialNotFound(_) => fmt::Display::fmt(self, f),
            Self::Helper(ref e) => fmt::Display::fmt(e, f),
            Self::Io(ref e) => fmt::Debug::fmt(e, f),
            Self::Json(ref e) => fmt::Debug::fmt(e, f),
        }
    }
}

impl PartialEq for RenderError {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::PartialNotFound(ref s), Self::PartialNotFound(ref o)) => {
                s == o
            }
            _ => false,
        }
    }
}

impl Eq for RenderError {}