rune/ast/
rn_type.rs

1use crate::ast::prelude::*;
2
3#[test]
4#[cfg(not(miri))]
5fn ast_parse() {
6    rt::<ast::Type>("Bar");
7    rt::<ast::Type>("one::two::three::four::Five");
8    rt::<ast::Type>("Self");
9    rt::<ast::Type>("(one::One, two::Two)");
10    rt::<ast::Type>("(one::One, (two::Two, three::Three))");
11}
12
13/// A type, used for static typing.
14#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)]
15#[non_exhaustive]
16pub enum Type {
17    /// If the type is an identifier or a path.
18    Path(ast::Path),
19    /// If the type should return nothing (a.k.a the "never" type in Rust).
20    Bang(T![!]),
21    /// If the type is a tuple.
22    Tuple(ast::Parenthesized<Box<Type>, T![,]>),
23}
24
25impl Parse for Type {
26    fn parse(p: &mut Parser<'_>) -> Result<Self> {
27        let segment = match p.nth(0)? {
28            K![!] => Self::Bang(p.parse()?),
29            K!['('] => Self::Tuple(p.parse()?),
30            _ => Self::Path(p.parse()?),
31        };
32
33        Ok(segment)
34    }
35}