askama_filters/
error.rs

1//! Custom error type handling all the errors the
2//! filters can throw.
3
4use askama_shared;
5use regex;
6use std::fmt;
7
8#[cfg(feature = "date")]
9use chrono;
10
11pub use std::error::Error as ErrorTrait;
12
13#[derive(Debug)]
14pub enum Error {
15    Fmt(fmt::Error),
16    RegEx(regex::Error),
17
18    #[cfg(feature = "markdown")]
19    Markdown,
20    #[cfg(feature = "date")]
21    ChronoParse(chrono::ParseError),
22
23    /// This error needs to be non-exhaustive as
24    /// variants existence depend on features.
25    #[doc(hidden)]
26    __Nonexhaustive,
27}
28
29impl ErrorTrait for Error {
30    // Here for compatibility reasons
31    fn description(&self) -> &str {
32        match self {
33            Error::Fmt(e) => e.description(),
34            Error::RegEx(e) => e.description(),
35            #[cfg(feature = "markdown")]
36            Error::Markdown => "Markdown Error",
37            #[cfg(feature = "date")]
38            Error::ChronoParse(e) => e.description(),
39            _ => "unknown error: __Nonexhaustive",
40        }
41    }
42
43    fn source(&self) -> Option<&(dyn ErrorTrait + 'static)> {
44        match self {
45            Error::Fmt(ref e) => Some(e),
46            Error::RegEx(ref e) => Some(e),
47            #[cfg(feature = "markdown")]
48            Error::Markdown => None,
49            #[cfg(feature = "date")]
50            Error::ChronoParse(ref e) => Some(e),
51            _ => None,
52        }
53    }
54}
55
56impl fmt::Display for Error {
57    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58        write!(f, "Error in filter")
59    }
60}
61
62impl From<fmt::Error> for Error {
63    fn from(f: fmt::Error) -> Self {
64        Error::Fmt(f)
65    }
66}
67
68impl From<regex::Error> for Error {
69    fn from(f: regex::Error) -> Self {
70        Error::RegEx(f)
71    }
72}
73
74#[cfg(feature = "date")]
75impl From<chrono::ParseError> for Error {
76    fn from(f: chrono::ParseError) -> Self {
77        Error::ChronoParse(f)
78    }
79}
80
81impl From<Error> for askama_shared::Error {
82    fn from(_f: Error) -> Self {
83        askama_shared::Error::__Nonexhaustive
84    }
85}
86
87pub type Result<T> = std::result::Result<T, Error>;