time/error/
mod.rs

1//! Various error types returned by methods in the time crate.
2
3mod component_range;
4mod conversion_range;
5mod different_variant;
6#[cfg(feature = "formatting")]
7mod format;
8#[cfg(feature = "local-offset")]
9mod indeterminate_offset;
10#[cfg(all(any(feature = "formatting", feature = "parsing"), feature = "alloc"))]
11mod invalid_format_description;
12mod invalid_variant;
13#[cfg(feature = "parsing")]
14mod parse;
15#[cfg(feature = "parsing")]
16mod parse_from_description;
17#[cfg(feature = "parsing")]
18mod try_from_parsed;
19
20#[cfg(feature = "parsing")]
21use core::convert::Infallible;
22use core::fmt;
23
24pub use component_range::ComponentRange;
25pub use conversion_range::ConversionRange;
26pub use different_variant::DifferentVariant;
27#[cfg(feature = "formatting")]
28pub use format::Format;
29#[cfg(feature = "local-offset")]
30pub use indeterminate_offset::IndeterminateOffset;
31#[cfg(all(any(feature = "formatting", feature = "parsing"), feature = "alloc"))]
32pub use invalid_format_description::InvalidFormatDescription;
33pub use invalid_variant::InvalidVariant;
34#[cfg(feature = "parsing")]
35pub use parse::Parse;
36#[cfg(feature = "parsing")]
37pub use parse_from_description::ParseFromDescription;
38#[cfg(feature = "parsing")]
39pub use try_from_parsed::TryFromParsed;
40
41/// A unified error type for anything returned by a method in the time crate.
42///
43/// This can be used when you either don't know or don't care about the exact error returned.
44/// `Result<_, time::Error>` (or its alias `time::Result<_>`) will work in these situations.
45#[non_exhaustive]
46#[derive(Debug)]
47pub enum Error {
48    #[expect(missing_docs)]
49    ConversionRange(ConversionRange),
50    #[expect(missing_docs)]
51    ComponentRange(ComponentRange),
52    #[cfg(feature = "local-offset")]
53    #[expect(missing_docs)]
54    IndeterminateOffset(IndeterminateOffset),
55    #[cfg(feature = "formatting")]
56    #[expect(missing_docs)]
57    Format(Format),
58    #[cfg(feature = "parsing")]
59    #[expect(missing_docs)]
60    ParseFromDescription(ParseFromDescription),
61    #[cfg(feature = "parsing")]
62    #[expect(missing_docs)]
63    #[non_exhaustive]
64    #[deprecated(
65        since = "0.3.28",
66        note = "no longer output. moved to the `ParseFromDescription` variant"
67    )]
68    UnexpectedTrailingCharacters {
69        #[doc(hidden)]
70        never: Infallible,
71    },
72    #[cfg(feature = "parsing")]
73    #[expect(missing_docs)]
74    TryFromParsed(TryFromParsed),
75    #[cfg(all(any(feature = "formatting", feature = "parsing"), feature = "alloc"))]
76    #[expect(missing_docs)]
77    InvalidFormatDescription(InvalidFormatDescription),
78    #[expect(missing_docs)]
79    DifferentVariant(DifferentVariant),
80    #[expect(missing_docs)]
81    InvalidVariant(InvalidVariant),
82}
83
84impl fmt::Display for Error {
85    #[inline]
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        match self {
88            Self::ConversionRange(e) => e.fmt(f),
89            Self::ComponentRange(e) => e.fmt(f),
90            #[cfg(feature = "local-offset")]
91            Self::IndeterminateOffset(e) => e.fmt(f),
92            #[cfg(feature = "formatting")]
93            Self::Format(e) => e.fmt(f),
94            #[cfg(feature = "parsing")]
95            Self::ParseFromDescription(e) => e.fmt(f),
96            #[cfg(feature = "parsing")]
97            #[allow(deprecated)]
98            Self::UnexpectedTrailingCharacters { never } => match *never {},
99            #[cfg(feature = "parsing")]
100            Self::TryFromParsed(e) => e.fmt(f),
101            #[cfg(all(any(feature = "formatting", feature = "parsing"), feature = "alloc"))]
102            Self::InvalidFormatDescription(e) => e.fmt(f),
103            Self::DifferentVariant(e) => e.fmt(f),
104            Self::InvalidVariant(e) => e.fmt(f),
105        }
106    }
107}
108
109impl core::error::Error for Error {
110    #[inline]
111    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
112        match self {
113            Self::ConversionRange(err) => Some(err),
114            Self::ComponentRange(err) => Some(err),
115            #[cfg(feature = "local-offset")]
116            Self::IndeterminateOffset(err) => Some(err),
117            #[cfg(feature = "formatting")]
118            Self::Format(err) => Some(err),
119            #[cfg(feature = "parsing")]
120            Self::ParseFromDescription(err) => Some(err),
121            #[cfg(feature = "parsing")]
122            #[allow(deprecated)]
123            Self::UnexpectedTrailingCharacters { never } => match *never {},
124            #[cfg(feature = "parsing")]
125            Self::TryFromParsed(err) => Some(err),
126            #[cfg(all(any(feature = "formatting", feature = "parsing"), feature = "alloc"))]
127            Self::InvalidFormatDescription(err) => Some(err),
128            Self::DifferentVariant(err) => Some(err),
129            Self::InvalidVariant(err) => Some(err),
130        }
131    }
132}