time/error/
parse_from_description.rs

1//! Error parsing an input into a [`Parsed`](crate::parsing::Parsed) struct
2
3use core::fmt;
4
5use 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)]
10pub 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    /// The input was expected to have ended, but there are characters that remain.
17    #[non_exhaustive]
18    UnexpectedTrailingCharacters,
19}
20
21impl fmt::Display for ParseFromDescription {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            Self::InvalidLiteral => f.write_str("a character literal was not valid"),
25            Self::InvalidComponent(name) => {
26                write!(f, "the '{name}' component could not be parsed")
27            }
28            Self::UnexpectedTrailingCharacters => {
29                f.write_str("unexpected trailing characters; the end of input was expected")
30            }
31        }
32    }
33}
34
35#[cfg(feature = "std")]
36impl std::error::Error for ParseFromDescription {}
37
38impl From<ParseFromDescription> for crate::Error {
39    fn from(original: ParseFromDescription) -> Self {
40        Self::ParseFromDescription(original)
41    }
42}
43
44impl TryFrom<crate::Error> for ParseFromDescription {
45    type Error = error::DifferentVariant;
46
47    fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
48        match err {
49            crate::Error::ParseFromDescription(err) => Ok(err),
50            _ => Err(error::DifferentVariant),
51        }
52    }
53}