time/error/
try_from_parsed.rs

1//! Error converting a [`Parsed`](crate::parsing::Parsed) struct to another type
2
3use core::fmt;
4
5use crate::error;
6
7/// An error that occurred when converting a [`Parsed`](crate::parsing::Parsed) to another type.
8#[non_exhaustive]
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum TryFromParsed {
11    /// The [`Parsed`](crate::parsing::Parsed) did not include enough information to construct the
12    /// type.
13    InsufficientInformation,
14    /// Some component contained an invalid value for the type.
15    ComponentRange(error::ComponentRange),
16}
17
18impl fmt::Display for TryFromParsed {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            Self::InsufficientInformation => f.write_str(
22                "the `Parsed` struct did not include enough information to construct the type",
23            ),
24            Self::ComponentRange(err) => err.fmt(f),
25        }
26    }
27}
28
29impl From<error::ComponentRange> for TryFromParsed {
30    fn from(v: error::ComponentRange) -> Self {
31        Self::ComponentRange(v)
32    }
33}
34
35impl TryFrom<TryFromParsed> for error::ComponentRange {
36    type Error = error::DifferentVariant;
37
38    fn try_from(err: TryFromParsed) -> Result<Self, Self::Error> {
39        match err {
40            TryFromParsed::ComponentRange(err) => Ok(err),
41            _ => Err(error::DifferentVariant),
42        }
43    }
44}
45
46#[cfg(feature = "std")]
47#[allow(clippy::std_instead_of_core)]
48impl std::error::Error for TryFromParsed {
49    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
50        match self {
51            Self::InsufficientInformation => None,
52            Self::ComponentRange(err) => Some(err),
53        }
54    }
55}
56
57impl From<TryFromParsed> for crate::Error {
58    fn from(original: TryFromParsed) -> Self {
59        Self::TryFromParsed(original)
60    }
61}
62
63impl TryFrom<crate::Error> for TryFromParsed {
64    type Error = error::DifferentVariant;
65
66    fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
67        match err {
68            crate::Error::TryFromParsed(err) => Ok(err),
69            _ => Err(error::DifferentVariant),
70        }
71    }
72}