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")]
36#[allow(clippy::std_instead_of_core)]
37impl std::error::Error for ParseFromDescription {}
38
39impl From<ParseFromDescription> for crate::Error {
40    fn from(original: ParseFromDescription) -> Self {
41        Self::ParseFromDescription(original)
42    }
43}
44
45impl TryFrom<crate::Error> for ParseFromDescription {
46    type Error = error::DifferentVariant;
47
48    fn try_from(err: crate::Error) -> Result<Self, Self::Error> {
49        match err {
50            crate::Error::ParseFromDescription(err) => Ok(err),
51            _ => Err(error::DifferentVariant),
52        }
53    }
54}