rune/ast/
lit_bool.rs

1use crate::ast::prelude::*;
2
3#[test]
4#[cfg(not(miri))]
5fn ast_parse() {
6    rt::<ast::LitBool>("true");
7    rt::<ast::LitBool>("false");
8}
9
10/// The boolean literal.
11///
12/// * `true`.
13/// * `false`.
14#[derive(Debug, TryClone, Clone, Copy, PartialEq, Eq, Spanned)]
15#[try_clone(copy)]
16#[non_exhaustive]
17pub struct LitBool {
18    /// The span corresponding to the literal.
19    pub span: Span,
20    /// The value of the literal.
21    #[rune(skip)]
22    pub value: bool,
23}
24
25impl Parse for LitBool {
26    fn parse(p: &mut Parser<'_>) -> Result<Self> {
27        let t = p.next()?;
28
29        let value = match t.kind {
30            K![true] => true,
31            K![false] => false,
32            _ => {
33                return Err(compile::Error::expected(t, Expectation::Boolean));
34            }
35        };
36
37        Ok(Self {
38            span: t.span,
39            value,
40        })
41    }
42}
43
44impl Peek for LitBool {
45    fn peek(p: &mut Peeker<'_>) -> bool {
46        matches!(p.nth(0), K![true] | K![false])
47    }
48}
49
50impl ToTokens for LitBool {
51    fn to_tokens(
52        &self,
53        _: &mut MacroContext<'_, '_, '_>,
54        stream: &mut TokenStream,
55    ) -> alloc::Result<()> {
56        stream.push(ast::Token {
57            span: self.span,
58            kind: if self.value {
59                ast::Kind::True
60            } else {
61                ast::Kind::False
62            },
63        })
64    }
65}