rune/ast/
item_use.rs
1use crate::ast::prelude::*;
2
3#[test]
4#[cfg(not(miri))]
5fn ast_parse() {
6 rt::<ast::ItemUse>("use foo");
7 rt::<ast::ItemUse>("use foo::bar");
8 rt::<ast::ItemUse>("use foo::bar::baz");
9 rt::<ast::ItemUse>("#[macro_use] use foo::bar::baz");
10 rt::<ast::ItemUse>("#[macro_use] pub(crate) use foo::bar::baz");
11
12 rt::<ast::ItemUsePath>("crate::foo");
13 rt::<ast::ItemUsePath>("foo::bar");
14 rt::<ast::ItemUsePath>("foo::bar::{baz::*, biz}");
15 rt::<ast::ItemUsePath>("{*, bar::*}");
16 rt::<ast::ItemUsePath>("::{*, bar::*}");
17}
18
19#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
23#[rune(parse = "meta_only")]
24#[non_exhaustive]
25pub struct ItemUse {
26 #[rune(iter, meta)]
28 pub attributes: Vec<ast::Attribute>,
29 #[rune(option, meta)]
31 pub visibility: ast::Visibility,
32 pub use_token: T![use],
34 pub path: ItemUsePath,
36}
37
38item_parse!(Use, ItemUse, "use item");
39
40#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
44#[non_exhaustive]
45pub struct ItemUsePath {
46 #[rune(iter)]
48 pub global: Option<T![::]>,
49 pub first: ItemUseSegment,
51 #[rune(iter)]
53 pub segments: Vec<(T![::], ItemUseSegment)>,
54 #[rune(iter)]
56 pub alias: Option<(T![as], ast::Ident)>,
57}
58
59#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)]
61#[non_exhaustive]
62pub enum ItemUseSegment {
63 PathSegment(ast::PathSegment),
65 Wildcard(T![*]),
67 Group(ast::Braced<ast::ItemUsePath, T![,]>),
69}
70
71impl Peek for ItemUseSegment {
72 fn peek(p: &mut Peeker<'_>) -> bool {
73 matches!(p.nth(0), K![*] | K!['[']) || ast::PathSegment::peek(p)
74 }
75}
76
77impl Parse for ItemUseSegment {
78 fn parse(p: &mut Parser<'_>) -> Result<Self> {
79 Ok(match p.nth(0)? {
80 K![*] => Self::Wildcard(p.parse()?),
81 K!['{'] => Self::Group(p.parse()?),
82 _ => Self::PathSegment(p.parse()?),
83 })
84 }
85}