1use core::fmt;
4
5use crate::error::{self, ParseFromDescription, TryFromParsed};
6use crate::internal_macros::bug;
7
8#[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 #[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")]
38#[allow(clippy::std_instead_of_core)]
39impl std::error::Error for Parse {
40 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
41 match self {
42 Self::TryFromParsed(err) => Some(err),
43 Self::ParseFromDescription(err) => Some(err),
44 #[allow(deprecated)]
45 Self::UnexpectedTrailingCharacters => bug!("variant should not be used"),
46 }
47 }
48}
49
50impl From<TryFromParsed> for Parse {
51 fn from(err: TryFromParsed) -> Self {
52 Self::TryFromParsed(err)
53 }
54}
55
56impl TryFrom<Parse> for TryFromParsed {
57 type Error = error::DifferentVariant;
58
59 fn try_from(err: Parse) -> Result<Self, Self::Error> {
60 match err {
61 Parse::TryFromParsed(err) => Ok(err),
62 _ => Err(error::DifferentVariant),
63 }
64 }
65}
66
67impl From<ParseFromDescription> for Parse {
68 fn from(err: ParseFromDescription) -> Self {
69 Self::ParseFromDescription(err)
70 }
71}
72
73impl TryFrom<Parse> for ParseFromDescription {
74 type Error = error::DifferentVariant;
75
76 fn try_from(err: Parse) -> Result<Self, Self::Error> {
77 match err {
78 Parse::ParseFromDescription(err) => Ok(err),
79 _ => Err(error::DifferentVariant),
80 }
81 }
82}
83
84impl From<Parse> for crate::Error {
85 fn from(err: Parse) -> Self {
86 match err {
87 Parse::TryFromParsed(err) => Self::TryFromParsed(err),
88 Parse::ParseFromDescription(err) => Self::ParseFromDescription(err),
89 #[allow(deprecated)]
90 Parse::UnexpectedTrailingCharacters => bug!("variant should not be used"),
91 }
92 }
93}
94
95impl TryFrom<crate::Error> for Parse {
96 type Error = error::DifferentVariant;
97
98 fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
99 match err {
100 crate::Error::ParseFromDescription(err) => Ok(Self::ParseFromDescription(err)),
101 #[allow(deprecated)]
102 crate::Error::UnexpectedTrailingCharacters => bug!("variant should not be used"),
103 crate::Error::TryFromParsed(err) => Ok(Self::TryFromParsed(err)),
104 _ => Err(error::DifferentVariant),
105 }
106 }
107}