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
use crate::{Parse, ParseBracket, ParseResult, Parser};

use sway_ast::attribute::Annotated;
use sway_ast::{Braces, FnSignature, ItemFn, ItemTrait, Traits};

impl Parse for ItemTrait {
    fn parse(parser: &mut Parser) -> ParseResult<ItemTrait> {
        let visibility = parser.take();
        let trait_token = parser.parse()?;
        let name = parser.parse()?;
        let super_traits = match parser.take() {
            Some(colon_token) => {
                let traits = parser.parse()?;
                Some((colon_token, traits))
            }
            None => None,
        };

        let trait_items: Braces<Vec<(Annotated<FnSignature>, _)>> = parser.parse()?;
        for item in trait_items.get().iter() {
            let (fn_sig, _) = item;
            parser.ban_visibility_qualifier(&fn_sig.value.visibility)?;
        }

        let trait_defs_opt: Option<Braces<Vec<Annotated<ItemFn>>>> = Braces::try_parse(parser)?;
        if let Some(trait_defs) = &trait_defs_opt {
            for item in trait_defs.get().iter() {
                parser.ban_visibility_qualifier(&item.value.fn_signature.visibility)?;
            }
        }

        Ok(ItemTrait {
            visibility,
            trait_token,
            name,
            super_traits,
            trait_items,
            trait_defs_opt,
        })
    }
}

impl Parse for Traits {
    fn parse(parser: &mut Parser) -> ParseResult<Traits> {
        let prefix = parser.parse()?;
        let mut suffixes = Vec::new();
        while let Some(add_token) = parser.take() {
            let suffix = parser.parse()?;
            suffixes.push((add_token, suffix));
        }
        let traits = Traits { prefix, suffixes };
        Ok(traits)
    }
}