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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::ast;
use crate::{OptionSpanned as _, Parse, ParseError, Parser, Peek, Peeker, Spanned, ToTokens};
use runestick::{Id, Span};
#[derive(Debug, Clone, PartialEq, Eq, Parse, ToTokens, Spanned)]
#[rune(parse = "meta_only")]
pub struct ItemMod {
#[rune(id)]
pub id: Option<Id>,
#[rune(iter, meta)]
pub attributes: Vec<ast::Attribute>,
#[rune(optional, meta)]
pub visibility: ast::Visibility,
pub mod_token: T![mod],
pub name: ast::Ident,
pub body: ItemModBody,
}
impl ItemMod {
pub fn name_span(&self) -> Span {
if let Some(span) = self.visibility.option_span() {
span.join(self.name.span())
} else {
self.mod_token.span().join(self.name.span())
}
}
}
item_parse!(Mod, ItemMod, "mod item");
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
pub enum ItemModBody {
EmptyBody(T![;]),
InlineBody(ItemInlineBody),
}
impl Parse for ItemModBody {
fn parse(p: &mut Parser) -> Result<Self, ParseError> {
Ok(match p.nth(0)? {
K!['{'] => Self::InlineBody(p.parse()?),
_ => Self::EmptyBody(p.parse()?),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Parse, Spanned)]
pub struct ItemInlineBody {
pub open: T!['{'],
pub file: Box<ast::File>,
pub close: T!['}'],
}
impl Peek for ItemInlineBody {
fn peek(p: &mut Peeker<'_>) -> bool {
<T!['{']>::peek(p)
}
}