jiff/error/tz/
db.rs

1use crate::error;
2
3#[derive(Clone, Debug)]
4pub(crate) enum Error {
5    #[cfg(feature = "tzdb-concatenated")]
6    ConcatenatedMissingIanaIdentifiers,
7    #[cfg(all(feature = "std", not(feature = "tzdb-concatenated")))]
8    DisabledConcatenated,
9    #[cfg(all(feature = "std", not(feature = "tzdb-zoneinfo")))]
10    DisabledZoneInfo,
11    FailedTimeZone {
12        #[cfg(feature = "alloc")]
13        name: alloc::boxed::Box<str>,
14    },
15    FailedTimeZoneNoDatabaseConfigured {
16        #[cfg(feature = "alloc")]
17        name: alloc::boxed::Box<str>,
18    },
19    #[cfg(feature = "tzdb-zoneinfo")]
20    ZoneInfoNoTzifFiles,
21    #[cfg(feature = "tzdb-zoneinfo")]
22    ZoneInfoStripPrefix,
23}
24
25impl Error {
26    pub(crate) fn failed_time_zone(_time_zone_name: &str) -> Error {
27        Error::FailedTimeZone {
28            #[cfg(feature = "alloc")]
29            name: _time_zone_name.into(),
30        }
31    }
32
33    pub(crate) fn failed_time_zone_no_database_configured(
34        _time_zone_name: &str,
35    ) -> Error {
36        Error::FailedTimeZoneNoDatabaseConfigured {
37            #[cfg(feature = "alloc")]
38            name: _time_zone_name.into(),
39        }
40    }
41}
42
43impl From<Error> for error::Error {
44    #[cold]
45    #[inline(never)]
46    fn from(err: Error) -> error::Error {
47        error::ErrorKind::TzDb(err).into()
48    }
49}
50
51impl error::IntoError for Error {
52    fn into_error(self) -> error::Error {
53        self.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            #[cfg(feature = "tzdb-concatenated")]
63            ConcatenatedMissingIanaIdentifiers => f.write_str(
64                "found no IANA time zone identifiers in \
65                 concatenated tzdata file",
66            ),
67            #[cfg(all(feature = "std", not(feature = "tzdb-concatenated")))]
68            DisabledConcatenated => {
69                f.write_str("system concatenated tzdb unavailable")
70            }
71            #[cfg(all(feature = "std", not(feature = "tzdb-zoneinfo")))]
72            DisabledZoneInfo => {
73                f.write_str("system zoneinfo tzdb unavailable")
74            }
75            FailedTimeZone {
76                #[cfg(feature = "alloc")]
77                ref name,
78            } => {
79                #[cfg(feature = "alloc")]
80                {
81                    write!(
82                        f,
83                        "failed to find time zone `{name}` \
84                         in time zone database",
85                    )
86                }
87                #[cfg(not(feature = "alloc"))]
88                {
89                    f.write_str(
90                        "failed to find time zone in time zone database",
91                    )
92                }
93            }
94            FailedTimeZoneNoDatabaseConfigured {
95                #[cfg(feature = "alloc")]
96                ref name,
97            } => {
98                #[cfg(feature = "std")]
99                {
100                    write!(
101                        f,
102                        "failed to find time zone `{name}` since there is no \
103                         time zone database configured",
104                    )
105                }
106                #[cfg(all(not(feature = "std"), feature = "alloc"))]
107                {
108                    write!(
109                        f,
110                        "failed to find time zone `{name}`, since there is no \
111                         global time zone database configured (and is \
112                         currently impossible to do so without Jiff's `std` \
113                         feature enabled, if you need this functionality, \
114                         please file an issue on Jiff's tracker with your \
115                         use case)",
116                    )
117                }
118                #[cfg(all(not(feature = "std"), not(feature = "alloc")))]
119                {
120                    f.write_str(
121                        "failed to find time zone, since there is no \
122                         global time zone database configured (and is \
123                         currently impossible to do so without Jiff's `std` \
124                         feature enabled, if you need this functionality, \
125                         please file an issue on Jiff's tracker with your \
126                         use case)",
127                    )
128                }
129            }
130            #[cfg(feature = "tzdb-zoneinfo")]
131            ZoneInfoNoTzifFiles => f.write_str(
132                "did not find any TZif files in zoneinfo time zone database",
133            ),
134            #[cfg(feature = "tzdb-zoneinfo")]
135            ZoneInfoStripPrefix => f.write_str(
136                "failed to strip zoneinfo time zone database directory \
137                 path from path to TZif file",
138            ),
139        }
140    }
141}