sway_ast/item/
item_impl.rs

1use crate::{priv_prelude::*, FnArgs};
2
3/// Denotes to what kind of an item an [ItemImplItem] belongs.
4/// This enum is used mostly for reporting use cases.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum ImplItemParent {
7    Contract,
8    // Currently we don't have cases that need further distinction.
9    // Add other specific items like enum, struct, etc. when needed.
10    Other,
11}
12
13#[derive(Clone, Debug, Serialize)]
14pub enum ItemImplItem {
15    Fn(ItemFn),
16    Const(ItemConst),
17    Type(TraitType),
18}
19
20impl ItemImplItem {
21    /// [ItemImplItem]'s friendly name string used for various reportings.
22    pub fn friendly_name(&self, parent: ImplItemParent) -> &'static str {
23        use ItemImplItem::*;
24        match self {
25            Fn(item_fn) => match item_fn.fn_signature.arguments.inner {
26                FnArgs::Static(_) => match parent {
27                    ImplItemParent::Contract => "contract method",
28                    ImplItemParent::Other => "associated function",
29                },
30                FnArgs::NonStatic { .. } => "method",
31            },
32            Const(..) => "associated constant",
33            Type(..) => "associated type",
34        }
35    }
36}
37
38#[derive(Clone, Debug, Serialize)]
39pub struct ItemImpl {
40    pub impl_token: ImplToken,
41    pub generic_params_opt: Option<GenericParams>,
42    pub trait_opt: Option<(PathType, ForToken)>,
43    pub ty: Ty,
44    pub where_clause_opt: Option<WhereClause>,
45    pub contents: Braces<Vec<Annotated<ItemImplItem>>>,
46}
47
48impl Spanned for ItemImpl {
49    fn span(&self) -> Span {
50        Span::join(self.impl_token.span(), &self.contents.span())
51    }
52}
53
54impl Spanned for ItemImplItem {
55    fn span(&self) -> Span {
56        match self {
57            ItemImplItem::Fn(fn_decl) => fn_decl.span(),
58            ItemImplItem::Const(const_decl) => const_decl.span(),
59            ItemImplItem::Type(type_decl) => type_decl.span(),
60        }
61    }
62}