]> git.proxmox.com Git - rustc.git/blob - vendor/time/src/error/parse_from_description.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / vendor / time / src / error / parse_from_description.rs
1 //! Error parsing an input into a [`Parsed`](crate::parsing::Parsed) struct
2
3 use core::fmt;
4
5 use crate::error;
6
7 /// An error that occurred while parsing the input into a [`Parsed`](crate::parsing::Parsed) struct.
8 #[non_exhaustive]
9 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
10 pub enum ParseFromDescription {
11 /// A string literal was not what was expected.
12 #[non_exhaustive]
13 InvalidLiteral,
14 /// A dynamic component was not valid.
15 InvalidComponent(&'static str),
16 }
17
18 impl fmt::Display for ParseFromDescription {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 match self {
21 Self::InvalidLiteral => f.write_str("a character literal was not valid"),
22 Self::InvalidComponent(name) => {
23 write!(f, "the '{name}' component could not be parsed")
24 }
25 }
26 }
27 }
28
29 #[cfg(feature = "std")]
30 impl std::error::Error for ParseFromDescription {}
31
32 impl From<ParseFromDescription> for crate::Error {
33 fn from(original: ParseFromDescription) -> Self {
34 Self::ParseFromDescription(original)
35 }
36 }
37
38 impl TryFrom<crate::Error> for ParseFromDescription {
39 type Error = error::DifferentVariant;
40
41 fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
42 match err {
43 crate::Error::ParseFromDescription(err) => Ok(err),
44 _ => Err(error::DifferentVariant),
45 }
46 }
47 }