| 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) => e.fmt(f), |
| 87 |
Self::ComponentRange(e) => e.fmt(f), |
| 88 |
#[cfg(feature = "local-offset")] |
| 89 |
Self::IndeterminateOffset(e) => e.fmt(f), |
| 90 |
#[cfg(feature = "formatting")] |
| 91 |
Self::Format(e) => 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) => e.fmt(f), |
| 101 |
Self::DifferentVariant(e) => e.fmt(f), |
| 102 |
Self::InvalidVariant(e) => e.fmt(f), |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
#[cfg(feature = "std")] |
| 108 |
impl std::error::Error for Error { |
| 109 |
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 110 |
match self { |
| 111 |
Self::ConversionRange(err) => Some(err), |
| 112 |
Self::ComponentRange(err) => Some(err), |
| 113 |
#[cfg(feature = "local-offset")] |
| 114 |
Self::IndeterminateOffset(err) => Some(err), |
| 115 |
#[cfg(feature = "formatting")] |
| 116 |
Self::Format(err) => Some(err), |
| 117 |
#[cfg(feature = "parsing")] |
| 118 |
Self::ParseFromDescription(err) => Some(err), |
| 119 |
#[cfg(feature = "parsing")] |
| 120 |
#[allow(deprecated)] |
| 121 |
Self::UnexpectedTrailingCharacters => bug!("variant should not be used"), |
| 122 |
#[cfg(feature = "parsing")] |
| 123 |
Self::TryFromParsed(err) => Some(err), |
| 124 |
#[cfg(all(any(feature = "formatting", feature = "parsing"), feature = "alloc"))] |
| 125 |
Self::InvalidFormatDescription(err) => Some(err), |
| 126 |
Self::DifferentVariant(err) => Some(err), |
| 127 |
Self::InvalidVariant(err) => Some(err), |
| 128 |
} |
| 129 |
} |
| 130 |
} |