use crate::ast::prelude::*;
#[test]
#[cfg(not(miri))]
fn ast_parse() {
rt::<ast::ItemUse>("use foo");
rt::<ast::ItemUse>("use foo::bar");
rt::<ast::ItemUse>("use foo::bar::baz");
rt::<ast::ItemUse>("#[macro_use] use foo::bar::baz");
rt::<ast::ItemUse>("#[macro_use] pub(crate) use foo::bar::baz");
rt::<ast::ItemUsePath>("crate::foo");
rt::<ast::ItemUsePath>("foo::bar");
rt::<ast::ItemUsePath>("foo::bar::{baz::*, biz}");
rt::<ast::ItemUsePath>("{*, bar::*}");
rt::<ast::ItemUsePath>("::{*, bar::*}");
}
#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
#[rune(parse = "meta_only")]
#[non_exhaustive]
pub struct ItemUse {
#[rune(iter, meta)]
pub attributes: Vec<ast::Attribute>,
#[rune(option, meta)]
pub visibility: ast::Visibility,
pub use_token: T![use],
pub path: ItemUsePath,
}
item_parse!(Use, ItemUse, "use item");
#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
#[non_exhaustive]
pub struct ItemUsePath {
#[rune(iter)]
pub global: Option<T![::]>,
pub first: ItemUseSegment,
#[rune(iter)]
pub segments: Vec<(T![::], ItemUseSegment)>,
#[rune(iter)]
pub alias: Option<(T![as], ast::Ident)>,
}
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)]
#[non_exhaustive]
pub enum ItemUseSegment {
PathSegment(ast::PathSegment),
Wildcard(T![*]),
Group(ast::Braced<ast::ItemUsePath, T![,]>),
}
impl Peek for ItemUseSegment {
fn peek(p: &mut Peeker<'_>) -> bool {
matches!(p.nth(0), K![*] | K!['[']) || ast::PathSegment::peek(p)
}
}
impl Parse for ItemUseSegment {
fn parse(p: &mut Parser<'_>) -> Result<Self> {
Ok(match p.nth(0)? {
K![*] => Self::Wildcard(p.parse()?),
K!['{'] => Self::Group(p.parse()?),
_ => Self::PathSegment(p.parse()?),
})
}
}