jiff/error/
util.rs

1use crate::{error, util::escape::Byte};
2
3#[derive(Clone, Debug)]
4pub(crate) enum RoundingIncrementError {
5    ForDateTime,
6    ForOffset,
7    ForSignedDuration,
8    ForSpan,
9    ForTime,
10    ForTimestamp,
11}
12
13impl From<RoundingIncrementError> for error::Error {
14    #[cold]
15    #[inline(never)]
16    fn from(err: RoundingIncrementError) -> error::Error {
17        error::ErrorKind::RoundingIncrement(err).into()
18    }
19}
20
21impl error::IntoError for RoundingIncrementError {
22    fn into_error(self) -> error::Error {
23        self.into()
24    }
25}
26
27impl core::fmt::Display for RoundingIncrementError {
28    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
29        use self::RoundingIncrementError::*;
30
31        match *self {
32            ForDateTime => f.write_str("failed rounding datetime"),
33            ForOffset => f.write_str("failed rounding time zone offset"),
34            ForSignedDuration => {
35                f.write_str("failed rounding signed duration")
36            }
37            ForSpan => f.write_str("failed rounding span"),
38            ForTime => f.write_str("failed rounding time"),
39            ForTimestamp => f.write_str("failed rounding timestamp"),
40        }
41    }
42}
43
44#[derive(Clone, Debug)]
45pub(crate) enum ParseIntError {
46    NoDigitsFound,
47    InvalidDigit(u8),
48    TooBig,
49}
50
51impl From<ParseIntError> for error::Error {
52    #[cold]
53    #[inline(never)]
54    fn from(err: ParseIntError) -> error::Error {
55        error::ErrorKind::ParseInt(err).into()
56    }
57}
58
59impl error::IntoError for ParseIntError {
60    fn into_error(self) -> error::Error {
61        self.into()
62    }
63}
64
65impl core::fmt::Display for ParseIntError {
66    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
67        use self::ParseIntError::*;
68
69        match *self {
70            NoDigitsFound => write!(f, "invalid number, no digits found"),
71            InvalidDigit(got) => {
72                write!(f, "invalid digit, expected 0-9 but got {}", Byte(got))
73            }
74            TooBig => {
75                write!(f, "number too big to parse into 64-bit integer")
76            }
77        }
78    }
79}
80
81#[derive(Clone, Debug)]
82pub(crate) enum ParseFractionError {
83    NoDigitsFound,
84    TooManyDigits,
85    InvalidDigit(u8),
86    TooBig,
87}
88
89impl ParseFractionError {
90    pub(crate) const MAX_PRECISION: usize = 9;
91}
92
93impl From<ParseFractionError> for error::Error {
94    #[cold]
95    #[inline(never)]
96    fn from(err: ParseFractionError) -> error::Error {
97        error::ErrorKind::ParseFraction(err).into()
98    }
99}
100
101impl error::IntoError for ParseFractionError {
102    fn into_error(self) -> error::Error {
103        self.into()
104    }
105}
106
107impl core::fmt::Display for ParseFractionError {
108    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
109        use self::ParseFractionError::*;
110
111        match *self {
112            NoDigitsFound => write!(f, "invalid fraction, no digits found"),
113            TooManyDigits => write!(
114                f,
115                "invalid fraction, too many digits \
116                 (at most {max} are allowed)",
117                max = ParseFractionError::MAX_PRECISION,
118            ),
119            InvalidDigit(got) => {
120                write!(
121                    f,
122                    "invalid fractional digit, expected 0-9 but got {}",
123                    Byte(got)
124                )
125            }
126            TooBig => {
127                write!(
128                    f,
129                    "fractional number too big to parse into 64-bit integer"
130                )
131            }
132        }
133    }
134}
135
136#[derive(Clone, Debug)]
137pub(crate) struct OsStrUtf8Error {
138    #[cfg(feature = "std")]
139    value: alloc::boxed::Box<std::ffi::OsStr>,
140}
141
142#[cfg(feature = "std")]
143impl From<&std::ffi::OsStr> for OsStrUtf8Error {
144    #[cold]
145    #[inline(never)]
146    fn from(value: &std::ffi::OsStr) -> OsStrUtf8Error {
147        OsStrUtf8Error { value: value.into() }
148    }
149}
150
151impl From<OsStrUtf8Error> for error::Error {
152    #[cold]
153    #[inline(never)]
154    fn from(err: OsStrUtf8Error) -> error::Error {
155        error::ErrorKind::OsStrUtf8(err).into()
156    }
157}
158
159impl error::IntoError for OsStrUtf8Error {
160    fn into_error(self) -> error::Error {
161        self.into()
162    }
163}
164
165impl core::fmt::Display for OsStrUtf8Error {
166    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
167        #[cfg(feature = "std")]
168        {
169            write!(
170                f,
171                "environment value `{value:?}` is not valid UTF-8",
172                value = self.value
173            )
174        }
175        #[cfg(not(feature = "std"))]
176        {
177            write!(f, "<BUG: SHOULD NOT EXIST>")
178        }
179    }
180}