sway_ast/
module.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
use crate::priv_prelude::*;

#[derive(Clone, Debug, Serialize)]
pub struct Module {
    pub kind: ModuleKind,
    pub semicolon_token: SemicolonToken,
    pub items: Vec<Item>,
}

impl Module {
    pub fn submodules(&self) -> impl Iterator<Item = &Submodule> {
        self.items.iter().filter_map(|i| {
            if let ItemKind::Submodule(submod) = &i.value {
                Some(submod)
            } else {
                None
            }
        })
    }
}

impl Spanned for Module {
    fn span(&self) -> Span {
        let start = self.kind.span();
        let end = if let Some(item) = self.items.last() {
            item.span()
        } else {
            self.semicolon_token.span()
        };
        Span::join(start, &end)
    }
}

#[derive(Clone, Debug, Serialize)]
pub enum ModuleKind {
    Script { script_token: ScriptToken },
    Contract { contract_token: ContractToken },
    Predicate { predicate_token: PredicateToken },
    Library { library_token: LibraryToken },
}

impl Spanned for ModuleKind {
    fn span(&self) -> Span {
        match self {
            Self::Script { script_token } => script_token.span(),
            Self::Contract { contract_token } => contract_token.span(),
            Self::Predicate { predicate_token } => predicate_token.span(),
            Self::Library { library_token } => library_token.span(),
        }
    }
}