time/error/
parse.rs

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