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
use crate::ast::prelude::*;

#[test]
fn ast_parse() {
    use crate::testing::rt;

    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::*}");
}

/// A `use` item.
///
/// * `use <path>`
#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
#[rune(parse = "meta_only")]
#[non_exhaustive]
pub struct ItemUse {
    /// The attributes on use item
    #[rune(iter, meta)]
    pub attributes: Vec<ast::Attribute>,
    /// The visibility of the `use` item
    #[rune(option, meta)]
    pub visibility: ast::Visibility,
    /// The use token.
    pub use_token: T![use],
    /// Item path.
    pub path: ItemUsePath,
}

item_parse!(Use, ItemUse, "use item");

/// A single use declaration path.
///
/// * `foo::bar::{baz::*, biz}`.
#[derive(Debug, TryClone, PartialEq, Eq, Parse, ToTokens, Spanned)]
#[non_exhaustive]
pub struct ItemUsePath {
    /// Global prefix.
    #[rune(iter)]
    pub global: Option<T![::]>,
    /// The first use component.
    pub first: ItemUseSegment,
    /// Optional segments.
    #[rune(iter)]
    pub segments: Vec<(T![::], ItemUseSegment)>,
    /// The alias of the import.
    #[rune(iter)]
    pub alias: Option<(T![as], ast::Ident)>,
}

/// A use component.
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)]
#[non_exhaustive]
pub enum ItemUseSegment {
    /// A path segment.
    PathSegment(ast::PathSegment),
    /// A wildcard import.
    Wildcard(T![*]),
    /// A grouped import.
    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()?),
        })
    }
}