jiff/error/fmt/
mod.rs

1use crate::{error, util::escape};
2
3pub(crate) mod friendly;
4pub(crate) mod offset;
5pub(crate) mod rfc2822;
6pub(crate) mod rfc9557;
7pub(crate) mod strtime;
8pub(crate) mod temporal;
9pub(crate) mod util;
10
11#[derive(Clone, Debug)]
12pub(crate) enum Error {
13    HybridDurationEmpty,
14    HybridDurationPrefix {
15        sign: u8,
16    },
17    IntoFull {
18        #[cfg(feature = "alloc")]
19        value: alloc::boxed::Box<str>,
20        #[cfg(feature = "alloc")]
21        unparsed: alloc::boxed::Box<[u8]>,
22    },
23    StdFmtWriteAdapter,
24}
25
26impl Error {
27    pub(crate) fn into_full_error(
28        _value: &dyn core::fmt::Display,
29        _unparsed: &[u8],
30    ) -> Error {
31        Error::IntoFull {
32            #[cfg(feature = "alloc")]
33            value: alloc::string::ToString::to_string(_value).into(),
34            #[cfg(feature = "alloc")]
35            unparsed: _unparsed.into(),
36        }
37    }
38}
39
40impl error::IntoError for Error {
41    fn into_error(self) -> error::Error {
42        self.into()
43    }
44}
45
46impl From<Error> for error::Error {
47    #[cold]
48    #[inline(never)]
49    fn from(err: Error) -> error::Error {
50        error::ErrorKind::Fmt(err).into()
51    }
52}
53
54impl core::fmt::Display for Error {
55    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
56        use self::Error::*;
57
58        match *self {
59            HybridDurationEmpty => f.write_str(
60                "an empty string is not a valid duration in either \
61                 the ISO 8601 format or Jiff's \"friendly\" format",
62            ),
63            HybridDurationPrefix { sign } => write!(
64                f,
65                "found nothing after sign `{sign}`, \
66                 which is not a valid duration in either \
67                 the ISO 8601 format or Jiff's \"friendly\" format",
68                sign = escape::Byte(sign),
69            ),
70            #[cfg(not(feature = "alloc"))]
71            IntoFull { .. } => f.write_str(
72                "parsed value, but unparsed input remains \
73                 (expected no unparsed input)",
74            ),
75            #[cfg(feature = "alloc")]
76            IntoFull { ref value, ref unparsed } => write!(
77                f,
78                "parsed value '{value}', but unparsed input {unparsed:?} \
79                 remains (expected no unparsed input)",
80                unparsed = escape::Bytes(unparsed),
81            ),
82            StdFmtWriteAdapter => {
83                f.write_str("an error occurred when formatting an argument")
84            }
85        }
86    }
87}