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