sway_ast/item/
item_use.rs

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

#[derive(Clone, Debug, Serialize)]
pub struct ItemUse {
    pub visibility: Option<PubToken>,
    pub use_token: UseToken,
    pub root_import: Option<DoubleColonToken>,
    pub tree: UseTree,
    pub semicolon_token: SemicolonToken,
}

impl Spanned for ItemUse {
    fn span(&self) -> Span {
        let start = match &self.visibility {
            Some(pub_token) => pub_token.span(),
            None => self.use_token.span(),
        };
        let end = self.semicolon_token.span();
        Span::join(start, &end)
    }
}

#[derive(Clone, Debug, Serialize)]
pub enum UseTree {
    Group {
        imports: Braces<Punctuated<UseTree, CommaToken>>,
    },
    Name {
        name: Ident,
    },
    Rename {
        name: Ident,
        as_token: AsToken,
        alias: Ident,
    },
    Glob {
        star_token: StarToken,
    },
    Path {
        prefix: Ident,
        double_colon_token: DoubleColonToken,
        suffix: Box<UseTree>,
    },
    // to handle parsing recovery, e.g. foo::
    Error {
        spans: Box<[Span]>,
    },
}

impl Spanned for UseTree {
    fn span(&self) -> Span {
        match self {
            UseTree::Group { imports } => imports.span(),
            UseTree::Name { name } => name.span(),
            UseTree::Rename { name, alias, .. } => Span::join(name.span(), &alias.span()),
            UseTree::Glob { star_token } => star_token.span(),
            UseTree::Path { prefix, suffix, .. } => Span::join(prefix.span(), &suffix.span()),
            UseTree::Error { spans } => Span::join_all(spans.to_vec().clone()),
        }
    }
}