Skip to main content

opening_hours_syntax/
error.rs

1use alloc::boxed::Box;
2use alloc::string::String;
3use core::fmt;
4
5use crate::parser::Rule;
6
7pub type Result<T> = core::result::Result<T, Error>;
8
9#[derive(Clone, Debug)]
10pub enum Error {
11    Parser(Box<pest::error::Error<Rule>>),
12    Unsupported(&'static str),
13    Overflow { value: String, expected: String },
14    InvalidExtendTime { hour: u8, minutes: u8 },
15}
16
17impl From<pest::error::Error<Rule>> for Error {
18    #[inline]
19    fn from(pest_err: pest::error::Error<Rule>) -> Self {
20        Self::Parser(pest_err.into())
21    }
22}
23
24impl fmt::Display for Error {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Self::Parser(pest_err) => write!(f, "{pest_err}"),
28            Self::Unsupported(desc) => write!(f, "using an unsupported feature: {desc}"),
29            Self::InvalidExtendTime { hour, minutes: minute } => {
30                write!(f, "invalid extended time for {hour:02}:{minute:02}")
31            }
32            Self::Overflow { value, expected } => {
33                write!(f, "{value} is too large: expected {expected}")
34            }
35        }
36    }
37}
38
39#[cfg(feature = "std")]
40impl std::error::Error for Error {
41    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
42        match self {
43            Self::Parser(err) => Some(err as _),
44            _ => None,
45        }
46    }
47}