use crate::ast::prelude::*;
#[test]
#[cfg(not(miri))]
fn ast_parse() {
rt::<ast::ItemMod>("mod ruins {}");
let item = rt::<ast::ItemMod>("#[cfg(test)] mod tests {}");
assert_eq!(item.attributes.len(), 1);
let item = rt::<ast::ItemMod>("mod whiskey_bravo { #![allow(dead_code)] fn x() {} }");
assert_eq!(item.attributes.len(), 0);
assert!(matches!(item.body, ast::ItemModBody::InlineBody(..)));
}
#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
#[rune(parse = "meta_only")]
#[non_exhaustive]
pub struct ItemMod {
#[rune(iter, meta)]
pub attributes: Vec<ast::Attribute>,
#[rune(option, meta)]
pub visibility: ast::Visibility,
pub mod_token: T![mod],
pub name: ast::Ident,
pub body: ItemModBody,
#[rune(skip)]
pub(crate) id: ItemId,
}
impl ItemMod {
pub(crate) 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, TryClone, PartialEq, Eq, ToTokens, Spanned)]
#[non_exhaustive]
pub enum ItemModBody {
EmptyBody(T![;]),
InlineBody(ItemInlineBody),
}
impl Parse for ItemModBody {
fn parse(p: &mut Parser<'_>) -> Result<Self> {
Ok(match p.nth(0)? {
K!['{'] => Self::InlineBody(p.parse()?),
_ => Self::EmptyBody(p.parse()?),
})
}
}
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Parse, Spanned)]
#[non_exhaustive]
pub struct ItemInlineBody {
pub open: T!['{'],
#[rune(option)]
pub file: Box<ast::File>,
pub close: T!['}'],
}
impl Peek for ItemInlineBody {
fn peek(p: &mut Peeker<'_>) -> bool {
<T!['{']>::peek(p)
}
}