rune/parse/
expectation.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use core::fmt;

/// Something that describes an expectation or actuality.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum Expectation {
    /// A static description.
    Description(&'static str),
    /// A keyword like `await`.
    Keyword(&'static str),
    /// A delimiter.
    Delimiter(&'static str),
    /// A punctuation which can be a sequence of characters, like `!=`.
    Punctuation(&'static str),
    /// Expected a specific kind of syntax node.
    Syntax(&'static str),
    /// An open delimiter.
    OpenDelimiter,
    /// A bolean.
    Boolean,
    /// A literal.
    Literal,
    /// An expression.
    Expression,
    /// A shebang.
    Shebang,
    /// A comment.
    Comment,
}

impl fmt::Display for Expectation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Expectation::Description(s) => s.fmt(f),
            Expectation::Keyword(k) => write!(f, "`{k}` keyword"),
            Expectation::Delimiter(d) => write!(f, "`{}` delimiter", d),
            Expectation::Punctuation(p) => write!(f, "`{}`", p),
            Expectation::OpenDelimiter => write!(f, "`(`, `[`, or `{{`"),
            Expectation::Boolean => write!(f, "true or false"),
            Expectation::Literal => write!(f, r#"literal like `"a string"` or 42"#),
            Expectation::Expression => write!(f, "expression"),
            Expectation::Shebang => write!(f, "shebang"),
            Expectation::Comment => write!(f, "comment"),
            Expectation::Syntax(s) => s.fmt(f),
        }
    }
}

/// Helper trait to get description.
pub(crate) trait IntoExpectation {
    /// Get the description for the thing.
    fn into_expectation(self) -> Expectation;
}

impl IntoExpectation for Expectation {
    fn into_expectation(self) -> Expectation {
        self
    }
}

impl IntoExpectation for &'static str {
    fn into_expectation(self) -> Expectation {
        Expectation::Description(self)
    }
}