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
20use core::fmt;
21
22pub use component_range::ComponentRange;
23pub use conversion_range::ConversionRange;
24pub use different_variant::DifferentVariant;
25#[cfg(feature = "formatting")]
26pub use format::Format;
27#[cfg(feature = "local-offset")]
28pub use indeterminate_offset::IndeterminateOffset;
29#[cfg(all(any(feature = "formatting", feature = "parsing"), feature = "alloc"))]
30pub use invalid_format_description::InvalidFormatDescription;
31pub use invalid_variant::InvalidVariant;
32#[cfg(feature = "parsing")]
33pub use parse::Parse;
34#[cfg(feature = "parsing")]
35pub use parse_from_description::ParseFromDescription;
36#[cfg(feature = "parsing")]
37pub 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)]
47pub 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
67impl 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")]
91impl 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