rune/ast/
rn_type.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
use crate::ast::prelude::*;

#[test]
#[cfg(not(miri))]
fn ast_parse() {
    rt::<ast::Type>("Bar");
    rt::<ast::Type>("one::two::three::four::Five");
    rt::<ast::Type>("Self");
    rt::<ast::Type>("(one::One, two::Two)");
    rt::<ast::Type>("(one::One, (two::Two, three::Three))");
}

/// A type, used for static typing.
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)]
#[non_exhaustive]
pub enum Type {
    /// If the type is an identifier or a path.
    Path(ast::Path),
    /// If the type should return nothing (a.k.a the "never" type in Rust).
    Bang(T![!]),
    /// If the type is a tuple.
    Tuple(ast::Parenthesized<Box<Type>, T![,]>),
}

impl Parse for Type {
    fn parse(p: &mut Parser<'_>) -> Result<Self> {
        let segment = match p.nth(0)? {
            K![!] => Self::Bang(p.parse()?),
            K!['('] => Self::Tuple(p.parse()?),
            _ => Self::Path(p.parse()?),
        };

        Ok(segment)
    }
}