Skip to main content

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