rune/parse/
expectation.rs

1use core::fmt;
2
3/// Something that describes an expectation or actuality.
4#[derive(Debug, Clone, Copy)]
5#[non_exhaustive]
6pub enum Expectation {
7    /// A static description.
8    Description(&'static str),
9    /// A keyword like `await`.
10    Keyword(&'static str),
11    /// A delimiter.
12    Delimiter(&'static str),
13    /// A punctuation which can be a sequence of characters, like `!=`.
14    Punctuation(&'static str),
15    /// Expected a specific kind of syntax node.
16    Syntax(&'static str),
17    /// An open delimiter.
18    OpenDelimiter,
19    /// A bolean.
20    Boolean,
21    /// A literal.
22    Literal,
23    /// An expression.
24    Expression,
25    /// A shebang.
26    Shebang,
27    /// A comment.
28    Comment,
29}
30
31impl fmt::Display for Expectation {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            Expectation::Description(s) => s.fmt(f),
35            Expectation::Keyword(k) => write!(f, "`{k}` keyword"),
36            Expectation::Delimiter(d) => write!(f, "`{}` delimiter", d),
37            Expectation::Punctuation(p) => write!(f, "`{}`", p),
38            Expectation::OpenDelimiter => write!(f, "`(`, `[`, or `{{`"),
39            Expectation::Boolean => write!(f, "true or false"),
40            Expectation::Literal => write!(f, r#"literal like `"a string"` or 42"#),
41            Expectation::Expression => write!(f, "expression"),
42            Expectation::Shebang => write!(f, "shebang"),
43            Expectation::Comment => write!(f, "comment"),
44            Expectation::Syntax(s) => s.fmt(f),
45        }
46    }
47}
48
49/// Helper trait to get description.
50pub(crate) trait IntoExpectation {
51    /// Get the description for the thing.
52    fn into_expectation(self) -> Expectation;
53}
54
55impl IntoExpectation for Expectation {
56    fn into_expectation(self) -> Expectation {
57        self
58    }
59}
60
61impl IntoExpectation for &'static str {
62    fn into_expectation(self) -> Expectation {
63        Expectation::Description(self)
64    }
65}