Skip to main content

opening_hours_syntax/
warning.rs

1use pest::iterators::Pair;
2
3use crate::parser::Rule;
4
5/// Some warning emited during the expression parsing. These warning are not
6/// critical and the expression will be parsed with no ambuiguity.
7#[derive(Debug, Clone)]
8pub enum Warning<'e> {
9    /// The literal should be capitalized
10    ShouldBeCapitalized(Pair<'e, Rule>),
11    /// The literal should be lowercase
12    ShouldBeLowercase(Pair<'e, Rule>),
13}
14
15impl<'e> Warning<'e> {
16    // Get the pair that emited this warning.
17    pub fn as_pair(&self) -> &Pair<'e, Rule> {
18        match self {
19            Self::ShouldBeCapitalized(pair) | Self::ShouldBeLowercase(pair) => pair,
20        }
21    }
22}
23
24impl core::fmt::Display for Warning<'_> {
25    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26        match self {
27            Self::ShouldBeCapitalized(pair) => write!(
28                f,
29                "{:?} literal should be capitalized, got '{}'",
30                pair.as_rule(),
31                pair.as_str(),
32            ),
33            Self::ShouldBeLowercase(pair) => write!(
34                f,
35                "{:?} literal should be lowercase, got '{}'",
36                pair.as_rule(),
37                pair.as_str(),
38            ),
39        }
40    }
41}