time/error/
parse.rs

1//! Error that occurred at some stage of parsing
2
3use core::convert::Infallible;
4use core::fmt;
5
6use crate::error::{self, ParseFromDescription, TryFromParsed};
7
8/// An error that occurred at some stage of parsing.
9#[non_exhaustive]
10#[allow(variant_size_differences)]
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum Parse {
13    #[allow(missing_docs)]
14    TryFromParsed(TryFromParsed),
15    #[allow(missing_docs)]
16    ParseFromDescription(ParseFromDescription),
17    #[allow(missing_docs)]
18    #[non_exhaustive]
19    #[deprecated(
20        since = "0.3.28",
21        note = "no longer output. moved to the `ParseFromDescription` variant"
22    )]
23    UnexpectedTrailingCharacters {
24        #[doc(hidden)]
25        never: Infallible,
26    },
27}
28
29impl fmt::Display for Parse {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            Self::TryFromParsed(err) => err.fmt(f),
33            Self::ParseFromDescription(err) => err.fmt(f),
34            #[allow(deprecated)]
35            Self::UnexpectedTrailingCharacters { never } => match *never {},
36        }
37    }
38}
39
40#[cfg(feature = "std")]
41impl std::error::Error for Parse {
42    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
43        match self {
44            Self::TryFromParsed(err) => Some(err),
45            Self::ParseFromDescription(err) => Some(err),
46            #[allow(deprecated)]
47            Self::UnexpectedTrailingCharacters { never } => match *never {},
48        }
49    }
50}
51
52impl From<TryFromParsed> for Parse {
53    fn from(err: TryFromParsed) -> Self {
54        Self::TryFromParsed(err)
55    }
56}
57
58impl TryFrom<Parse> for TryFromParsed {
59    type Error = error::DifferentVariant;
60
61    fn try_from(err: Parse) -> Result<Self, Self::Error> {
62        match err {
63            Parse::TryFromParsed(err) => Ok(err),
64            _ => Err(error::DifferentVariant),
65        }
66    }
67}
68
69impl From<ParseFromDescription> for Parse {
70    fn from(err: ParseFromDescription) -> Self {
71        Self::ParseFromDescription(err)
72    }
73}
74
75impl TryFrom<Parse> for ParseFromDescription {
76    type Error = error::DifferentVariant;
77
78    fn try_from(err: Parse) -> Result<Self, Self::Error> {
79        match err {
80            Parse::ParseFromDescription(err) => Ok(err),
81            _ => Err(error::DifferentVariant),
82        }
83    }
84}
85
86impl From<Parse> for crate::Error {
87    fn from(err: Parse) -> Self {
88        match err {
89            Parse::TryFromParsed(err) => Self::TryFromParsed(err),
90            Parse::ParseFromDescription(err) => Self::ParseFromDescription(err),
91            #[allow(deprecated)]
92            Parse::UnexpectedTrailingCharacters { never } => match never {},
93        }
94    }
95}
96
97impl TryFrom<crate::Error> for Parse {
98    type Error = error::DifferentVariant;
99
100    fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
101        match err {
102            crate::Error::ParseFromDescription(err) => Ok(Self::ParseFromDescription(err)),
103            #[allow(deprecated)]
104            crate::Error::UnexpectedTrailingCharacters { never } => match never {},
105            crate::Error::TryFromParsed(err) => Ok(Self::TryFromParsed(err)),
106            _ => Err(error::DifferentVariant),
107        }
108    }
109}