Debian Codegraph @

debian/rust-vendor/jiff/src/error/fmt/mod.rs

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