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, reason = "only triggers on some platforms")]
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum Parse {
13    #[expect(missing_docs)]
14    TryFromParsed(TryFromParsed),
15    #[expect(missing_docs)]
16    ParseFromDescription(ParseFromDescription),
17    #[expect(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    #[inline]
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::TryFromParsed(err) => err.fmt(f),
34            Self::ParseFromDescription(err) => err.fmt(f),
35            #[allow(deprecated)]
36            Self::UnexpectedTrailingCharacters { never } => match *never {},
37        }
38    }
39}
40
41impl core::error::Error for Parse {
42    #[inline]
43    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
44        match self {
45            Self::TryFromParsed(err) => Some(err),
46            Self::ParseFromDescription(err) => Some(err),
47            #[allow(deprecated)]
48            Self::UnexpectedTrailingCharacters { never } => match *never {},
49        }
50    }
51}
52
53impl From<TryFromParsed> for Parse {
54    #[inline]
55    fn from(err: TryFromParsed) -> Self {
56        Self::TryFromParsed(err)
57    }
58}
59
60impl TryFrom<Parse> for TryFromParsed {
61    type Error = error::DifferentVariant;
62
63    #[inline]
64    fn try_from(err: Parse) -> Result<Self, Self::Error> {
65        match err {
66            Parse::TryFromParsed(err) => Ok(err),
67            _ => Err(error::DifferentVariant),
68        }
69    }
70}
71
72impl From<ParseFromDescription> for Parse {
73    #[inline]
74    fn from(err: ParseFromDescription) -> Self {
75        Self::ParseFromDescription(err)
76    }
77}
78
79impl TryFrom<Parse> for ParseFromDescription {
80    type Error = error::DifferentVariant;
81
82    #[inline]
83    fn try_from(err: Parse) -> Result<Self, Self::Error> {
84        match err {
85            Parse::ParseFromDescription(err) => Ok(err),
86            _ => Err(error::DifferentVariant),
87        }
88    }
89}
90
91impl From<Parse> for crate::Error {
92    #[inline]
93    fn from(err: Parse) -> Self {
94        match err {
95            Parse::TryFromParsed(err) => Self::TryFromParsed(err),
96            Parse::ParseFromDescription(err) => Self::ParseFromDescription(err),
97            #[allow(deprecated)]
98            Parse::UnexpectedTrailingCharacters { never } => match never {},
99        }
100    }
101}
102
103impl TryFrom<crate::Error> for Parse {
104    type Error = error::DifferentVariant;
105
106    #[inline]
107    fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
108        match err {
109            crate::Error::ParseFromDescription(err) => Ok(Self::ParseFromDescription(err)),
110            #[allow(deprecated)]
111            crate::Error::UnexpectedTrailingCharacters { never } => match never {},
112            crate::Error::TryFromParsed(err) => Ok(Self::TryFromParsed(err)),
113            _ => Err(error::DifferentVariant),
114        }
115    }
116}