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