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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
use core::convert::Infallible;
use core::error::Error as StdError;
use core::fmt;
use core::marker::PhantomData;
#[cfg(feature = "std")]
use std::io;

/// The [`Result`](std::result::Result) type with [`Error`] as default error type
pub type Result<I, E = Error> = core::result::Result<I, E>;

/// askama's error type
///
/// Used as error value for e.g. [`Template::render()`][crate::Template::render()]
/// and custom filters.
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
    /// Generic, unspecified formatting error
    Fmt,
    /// Key not present in [`Values`][crate::Values]
    ValueMissing,
    /// Incompatible value type for key in [`Values`][crate::Values]
    ValueType,
    /// An error raised by using `?` in a template
    #[cfg(feature = "alloc")]
    Custom(Box<dyn StdError + Send + Sync>),
    /// JSON conversion error
    #[cfg(feature = "serde_json")]
    Json(serde_json::Error),
}

impl Error {
    /// Capture an [`StdError`]
    #[inline]
    #[cfg(feature = "alloc")]
    pub fn custom(err: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
        Self::Custom(err.into())
    }

    /// Convert this [`Error`] into a
    /// <code>[Box]&lt;dyn [StdError] + [Send] + [Sync]&gt;</code>
    #[cfg(feature = "alloc")]
    pub fn into_box(self) -> Box<dyn StdError + Send + Sync> {
        match self {
            Error::Fmt => fmt::Error.into(),
            Error::ValueMissing => Box::new(Error::ValueMissing),
            Error::ValueType => Box::new(Error::ValueType),
            Error::Custom(err) => err,
            #[cfg(feature = "serde_json")]
            Error::Json(err) => err.into(),
        }
    }

    /// Convert this [`Error`] into an [`io::Error`]
    ///
    /// Not this error itself, but the contained [`source`][StdError::source] is returned.
    #[cfg(feature = "std")]
    pub fn into_io_error(self) -> io::Error {
        io::Error::other(match self {
            Error::Custom(err) => match err.downcast() {
                Ok(err) => return *err,
                Err(err) => err,
            },
            err => err.into_box(),
        })
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Error::Fmt => Some(&fmt::Error),
            Error::ValueMissing => None,
            Error::ValueType => None,
            #[cfg(feature = "alloc")]
            Error::Custom(err) => Some(err.as_ref()),
            #[cfg(feature = "serde_json")]
            Error::Json(err) => Some(err),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Fmt => fmt::Error.fmt(f),
            Error::ValueMissing => f.write_str("key missing in values"),
            Error::ValueType => f.write_str("value has wrong type"),
            #[cfg(feature = "alloc")]
            Error::Custom(err) => err.fmt(f),
            #[cfg(feature = "serde_json")]
            Error::Json(err) => err.fmt(f),
        }
    }
}

impl From<Error> for fmt::Error {
    #[inline]
    fn from(_: Error) -> Self {
        Self
    }
}

#[cfg(feature = "std")]
impl From<Error> for io::Error {
    #[inline]
    fn from(err: Error) -> Self {
        err.into_io_error()
    }
}

impl From<fmt::Error> for Error {
    #[inline]
    fn from(_: fmt::Error) -> Self {
        Error::Fmt
    }
}

/// This conversion inspects the argument and chooses the best fitting [`Error`] variant
#[cfg(feature = "alloc")]
impl From<Box<dyn StdError + Send + Sync>> for Error {
    #[inline]
    fn from(err: Box<dyn StdError + Send + Sync>) -> Self {
        error_from_stderror(err, MAX_ERROR_UNWRAP_COUNT)
    }
}

/// This conversion inspects the argument and chooses the best fitting [`Error`] variant
#[cfg(feature = "std")]
impl From<io::Error> for Error {
    #[inline]
    fn from(err: io::Error) -> Self {
        error_from_io_error(err, MAX_ERROR_UNWRAP_COUNT)
    }
}

#[cfg(feature = "alloc")]
const MAX_ERROR_UNWRAP_COUNT: usize = 5;

#[cfg(feature = "alloc")]
fn error_from_stderror(err: Box<dyn StdError + Send + Sync>, unwraps: usize) -> Error {
    let Some(unwraps) = unwraps.checked_sub(1) else {
        return Error::Custom(err);
    };
    #[cfg(not(feature = "std"))]
    let _ = unwraps;
    match ErrorKind::inspect(err.as_ref()) {
        ErrorKind::Fmt => Error::Fmt,
        ErrorKind::Custom => Error::Custom(err),
        #[cfg(feature = "serde_json")]
        ErrorKind::Json => match err.downcast() {
            Ok(err) => Error::Json(*err),
            Err(_) => Error::Fmt, // unreachable
        },
        #[cfg(feature = "std")]
        ErrorKind::Io => match err.downcast() {
            Ok(err) => error_from_io_error(*err, unwraps),
            Err(_) => Error::Fmt, // unreachable
        },
        ErrorKind::Askama => match err.downcast() {
            Ok(err) => *err,
            Err(_) => Error::Fmt, // unreachable
        },
    }
}

#[cfg(feature = "std")]
fn error_from_io_error(err: io::Error, unwraps: usize) -> Error {
    let Some(inner) = err.get_ref() else {
        return Error::custom(err);
    };
    let Some(unwraps) = unwraps.checked_sub(1) else {
        return match err.into_inner() {
            Some(err) => Error::Custom(err),
            None => Error::Fmt, // unreachable
        };
    };
    match ErrorKind::inspect(inner) {
        ErrorKind::Fmt => Error::Fmt,
        ErrorKind::Askama => match err.downcast() {
            Ok(err) => err,
            Err(_) => Error::Fmt, // unreachable
        },
        #[cfg(feature = "serde_json")]
        ErrorKind::Json => match err.downcast() {
            Ok(err) => Error::Json(err),
            Err(_) => Error::Fmt, // unreachable
        },
        ErrorKind::Custom => match err.into_inner() {
            Some(err) => Error::Custom(err),
            None => Error::Fmt, // unreachable
        },
        ErrorKind::Io => match err.downcast() {
            Ok(inner) => error_from_io_error(inner, unwraps),
            Err(_) => Error::Fmt, // unreachable
        },
    }
}

#[cfg(feature = "alloc")]
enum ErrorKind {
    Fmt,
    Custom,
    #[cfg(feature = "serde_json")]
    Json,
    #[cfg(feature = "std")]
    Io,
    Askama,
}

#[cfg(feature = "alloc")]
impl ErrorKind {
    fn inspect(err: &(dyn StdError + 'static)) -> ErrorKind {
        if err.is::<fmt::Error>() {
            return ErrorKind::Fmt;
        }

        #[cfg(feature = "std")]
        if err.is::<io::Error>() {
            return ErrorKind::Io;
        }

        if err.is::<Error>() {
            return ErrorKind::Askama;
        }

        #[cfg(feature = "serde_json")]
        if err.is::<serde_json::Error>() {
            return ErrorKind::Json;
        }

        ErrorKind::Custom
    }
}

#[cfg(feature = "serde_json")]
impl From<serde_json::Error> for Error {
    #[inline]
    fn from(err: serde_json::Error) -> Self {
        Error::Json(err)
    }
}

impl From<Infallible> for Error {
    #[inline]
    fn from(value: Infallible) -> Self {
        match value {}
    }
}

#[cfg(test)]
const _: () = {
    trait AssertSendSyncStatic: Send + Sync + 'static {}
    impl AssertSendSyncStatic for Error {}
};

/// Helper trait to convert a custom `?` call into a [`crate::Result`]
pub trait ResultConverter {
    /// Okay Value type of the output
    type Value;
    /// Input type
    type Input;

    /// Consume an interior mutable `self`, and turn it into a [`crate::Result`]
    fn askama_conv_result(self, result: Self::Input) -> Result<Self::Value, Error>;
}

/// Helper marker to be used with [`ResultConverter`]
#[derive(Debug, Clone, Copy)]
pub struct ErrorMarker<T>(PhantomData<Result<T>>);

impl<T> ErrorMarker<T> {
    /// Get marker for a [`Result`] type
    #[inline]
    pub fn of(_: &T) -> Self {
        Self(PhantomData)
    }
}

#[cfg(feature = "alloc")]
impl<T, E> ResultConverter for &ErrorMarker<Result<T, E>>
where
    E: Into<Box<dyn StdError + Send + Sync>>,
{
    type Value = T;
    type Input = Result<T, E>;

    #[inline]
    fn askama_conv_result(self, result: Self::Input) -> Result<Self::Value, Error> {
        result.map_err(Error::custom)
    }
}

impl<T, E> ResultConverter for &&ErrorMarker<Result<T, E>>
where
    E: Into<Error>,
{
    type Value = T;
    type Input = Result<T, E>;

    #[inline]
    fn askama_conv_result(self, result: Self::Input) -> Result<Self::Value, Error> {
        result.map_err(Into::into)
    }
}