jiff/shared/util/
error.rs

1macro_rules! err {
2    ($($tt:tt)*) => {{
3        crate::shared::util::error::Error::from_args(format_args!($($tt)*))
4    }}
5}
6
7pub(crate) use err;
8
9/// An error that can be returned when parsing.
10#[derive(Clone, Debug)]
11pub struct Error {
12    #[cfg(feature = "alloc")]
13    message: alloc::boxed::Box<str>,
14    // only-jiff-start
15    #[cfg(not(feature = "alloc"))]
16    message: &'static str,
17    // only-jiff-end
18}
19
20impl Error {
21    pub(crate) fn from_args<'a>(message: core::fmt::Arguments<'a>) -> Error {
22        #[cfg(feature = "alloc")]
23        {
24            use alloc::string::ToString;
25
26            let message = message.to_string().into_boxed_str();
27            Error { message }
28        }
29        // only-jiff-start
30        #[cfg(not(feature = "alloc"))]
31        {
32            let message = message.as_str().unwrap_or(
33                "unknown Jiff error (better error messages require \
34                 enabling the `alloc` feature for the `jiff` crate)",
35            );
36            Error { message }
37        }
38        // only-jiff-end
39    }
40}
41
42impl core::fmt::Display for Error {
43    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
44        core::fmt::Display::fmt(&self.message, f)
45    }
46}