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 | /// A unified error type for anything returned by a method in the time crate. |
40 | /// |
41 | /// This can be used when you either don't know or don't care about the exact error returned. |
42 | /// `Result<_, time::Error>` (or its alias `time::Result<_>`) will work in these situations. |
43 | #[allow (missing_copy_implementations, variant_size_differences)] |
44 | #[allow (clippy::missing_docs_in_private_items)] // variants only |
45 | #[non_exhaustive ] |
46 | #[derive (Debug)] |
47 | pub enum Error { |
48 | ConversionRange(ConversionRange), |
49 | ComponentRange(ComponentRange), |
50 | #[cfg (feature = "local-offset" )] |
51 | IndeterminateOffset(IndeterminateOffset), |
52 | #[cfg (feature = "formatting" )] |
53 | Format(Format), |
54 | #[cfg (feature = "parsing" )] |
55 | ParseFromDescription(ParseFromDescription), |
56 | #[cfg (feature = "parsing" )] |
57 | #[non_exhaustive ] |
58 | UnexpectedTrailingCharacters, |
59 | #[cfg (feature = "parsing" )] |
60 | TryFromParsed(TryFromParsed), |
61 | #[cfg (all(any(feature = "formatting" , feature = "parsing" ), feature = "alloc" ))] |
62 | InvalidFormatDescription(InvalidFormatDescription), |
63 | DifferentVariant(DifferentVariant), |
64 | InvalidVariant(InvalidVariant), |
65 | } |
66 | |
67 | impl fmt::Display for Error { |
68 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
69 | match self { |
70 | Self::ConversionRange(e: &ConversionRange) => e.fmt(f), |
71 | Self::ComponentRange(e: &ComponentRange) => e.fmt(f), |
72 | #[cfg (feature = "local-offset" )] |
73 | Self::IndeterminateOffset(e) => e.fmt(f), |
74 | #[cfg (feature = "formatting" )] |
75 | Self::Format(e: &Format) => e.fmt(f), |
76 | #[cfg (feature = "parsing" )] |
77 | Self::ParseFromDescription(e) => e.fmt(f), |
78 | #[cfg (feature = "parsing" )] |
79 | Self::UnexpectedTrailingCharacters => f.write_str("unexpected trailing characters" ), |
80 | #[cfg (feature = "parsing" )] |
81 | Self::TryFromParsed(e) => e.fmt(f), |
82 | #[cfg (all(any(feature = "formatting" , feature = "parsing" ), feature = "alloc" ))] |
83 | Self::InvalidFormatDescription(e: &InvalidFormatDescription) => e.fmt(f), |
84 | Self::DifferentVariant(e: &DifferentVariant) => e.fmt(f), |
85 | Self::InvalidVariant(e: &InvalidVariant) => e.fmt(f), |
86 | } |
87 | } |
88 | } |
89 | |
90 | #[cfg (feature = "std" )] |
91 | impl std::error::Error for Error { |
92 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
93 | match self { |
94 | Self::ConversionRange(err: &ConversionRange) => Some(err), |
95 | Self::ComponentRange(err: &ComponentRange) => Some(err), |
96 | #[cfg (feature = "local-offset" )] |
97 | Self::IndeterminateOffset(err) => Some(err), |
98 | #[cfg (feature = "formatting" )] |
99 | Self::Format(err: &Format) => Some(err), |
100 | #[cfg (feature = "parsing" )] |
101 | Self::ParseFromDescription(err) => Some(err), |
102 | #[cfg (feature = "parsing" )] |
103 | Self::UnexpectedTrailingCharacters => None, |
104 | #[cfg (feature = "parsing" )] |
105 | Self::TryFromParsed(err) => Some(err), |
106 | #[cfg (all(any(feature = "formatting" , feature = "parsing" ), feature = "alloc" ))] |
107 | Self::InvalidFormatDescription(err: &InvalidFormatDescription) => Some(err), |
108 | Self::DifferentVariant(err: &DifferentVariant) => Some(err), |
109 | Self::InvalidVariant(err: &InvalidVariant) => Some(err), |
110 | } |
111 | } |
112 | } |
113 | |