sway_ast/item/
item_trait.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use sway_error::handler::ErrorEmitted;

use crate::priv_prelude::*;

#[derive(Clone, Debug, Serialize)]
pub enum ItemTraitItem {
    Fn(FnSignature, Option<SemicolonToken>),
    Const(ItemConst, Option<SemicolonToken>),
    Type(TraitType, Option<SemicolonToken>),
    // to handle parser recovery: Error represents an incomplete trait item
    Error(Box<[Span]>, #[serde(skip_serializing)] ErrorEmitted),
}

#[derive(Clone, Debug, Serialize)]
pub struct ItemTrait {
    pub visibility: Option<PubToken>,
    pub trait_token: TraitToken,
    pub name: Ident,
    pub generics: Option<GenericParams>,
    pub where_clause_opt: Option<WhereClause>,
    pub super_traits: Option<(ColonToken, Traits)>,
    pub trait_items: Braces<Vec<Annotated<ItemTraitItem>>>,
    pub trait_defs_opt: Option<Braces<Vec<Annotated<ItemFn>>>>,
}

impl Spanned for ItemTrait {
    fn span(&self) -> Span {
        let start = match &self.visibility {
            Some(pub_token) => pub_token.span(),
            None => self.trait_token.span(),
        };
        let end = match &self.trait_defs_opt {
            Some(trait_defs) => trait_defs.span(),
            None => self.trait_items.span(),
        };
        Span::join(start, &end)
    }
}

impl Spanned for ItemTraitItem {
    fn span(&self) -> Span {
        match self {
            ItemTraitItem::Fn(fn_decl, semicolon) => match semicolon.as_ref().map(|x| x.span()) {
                Some(semicolon) => Span::join(fn_decl.span(), &semicolon),
                None => fn_decl.span(),
            },
            ItemTraitItem::Const(const_decl, semicolon) => {
                match semicolon.as_ref().map(|x| x.span()) {
                    Some(semicolon) => Span::join(const_decl.span(), &semicolon),
                    None => const_decl.span(),
                }
            }
            ItemTraitItem::Type(type_decl, semicolon) => {
                match semicolon.as_ref().map(|x| x.span()) {
                    Some(semicolon) => Span::join(type_decl.span(), &semicolon),
                    None => type_decl.span(),
                }
            }
            ItemTraitItem::Error(spans, _) => Span::join_all(spans.iter().cloned()),
        }
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct Traits {
    pub prefix: PathType,
    pub suffixes: Vec<(AddToken, PathType)>,
}

impl Spanned for Traits {
    fn span(&self) -> Span {
        match self.suffixes.last() {
            Some((_add_token, path_type)) => Span::join(self.prefix.span(), &path_type.span()),
            None => self.prefix.span(),
        }
    }
}