tour_core/syntax.rs
1
2/// An expression delimiter.
3//
4// Opening and closing delimiter must be equal.
5#[derive(Clone, Copy, PartialEq, Eq)]
6pub enum Delimiter {
7 /// `{{ }}` escaped render.
8 Brace,
9 /// `{! !}` unescaped render.
10 Bang,
11 /// `{% %}` escaped render using `std::fmt::Display`.
12 Percent,
13 /// `{? ?}` escaped render using `std::fmt::Debug`.
14 Quest,
15 /// `{# #}` unused, same as [`Delimiter::Brace`].
16 Hash,
17 // /// `{@ @}`
18 // At,
19 // /// `{$ $}`
20 // Dollar,
21 // /// `{^ ^}`
22 // Caret,
23 // /// `{& &}`
24 // And,
25 // /// `{* *}`
26 // Star,
27 // /// `{( )}`
28 // Paren,
29 // /// `{[ ]}`
30 // Bracket,
31}
32
33impl Delimiter {
34 /// Returns [`Some`] if given byte considered as opening delimiter.
35 pub fn match_open(ch: u8) -> Option<Self> {
36 match ch {
37 b'{' => Some(Self::Brace),
38 b'!' => Some(Self::Bang),
39 b'%' => Some(Self::Percent),
40 b'?' => Some(Self::Quest),
41 b'#' => Some(Self::Hash),
42 _ => None,
43 }
44 }
45
46 /// Returns [`Some`] if given byte considered as closing delimiter.
47 pub fn match_close(ch: u8) -> Option<Self> {
48 match ch {
49 b'}' => Some(Self::Brace),
50 b'!' => Some(Self::Bang),
51 b'%' => Some(Self::Percent),
52 b'?' => Some(Self::Quest),
53 b'#' => Some(Self::Hash),
54 _ => None,
55 }
56 }
57}
58
59impl std::fmt::Display for Delimiter {
60 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
61 match self {
62 Self::Brace => f.write_str("brace"),
63 Self::Bang => f.write_str("!"),
64 Self::Percent => f.write_str("%"),
65 Self::Quest => f.write_str("?"),
66 Self::Hash => f.write_str("#"),
67 }
68 }
69}
70