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#[allow(clippy::large_enum_variant)]
14#[derive(Clone, Debug, Serialize)]
15pub enum ItemImplItem {
16    Fn(ItemFn),
17    Const(ItemConst),
18    Type(TraitType),
19}
20
21impl ItemImplItem {
22    /// [ItemImplItem]'s friendly name string used for various reportings.
23    pub fn friendly_name(&self, parent: ImplItemParent) -> &'static str {
24        use ItemImplItem::*;
25        match self {
26            Fn(item_fn) => match item_fn.fn_signature.arguments.inner {
27                FnArgs::Static(_) => match parent {
28                    ImplItemParent::Contract => "contract method",
29                    ImplItemParent::Other => "associated function",
30                },
31                FnArgs::NonStatic { .. } => "method",
32            },
33            Const(..) => "associated constant",
34            Type(..) => "associated type",
35        }
36    }
37}
38
39#[derive(Clone, Debug, Serialize)]
40pub struct ItemImpl {
41    pub impl_token: ImplToken,
42    pub generic_params_opt: Option<GenericParams>,
43    pub trait_opt: Option<(PathType, ForToken)>,
44    pub ty: Ty,
45    pub where_clause_opt: Option<WhereClause>,
46    pub contents: Braces<Vec<Annotated<ItemImplItem>>>,
47}
48
49impl Spanned for ItemImpl {
50    fn span(&self) -> Span {
51        Span::join(self.impl_token.span(), &self.contents.span())
52    }
53}
54
55impl Spanned for ItemImplItem {
56    fn span(&self) -> Span {
57        match self {
58            ItemImplItem::Fn(fn_decl) => fn_decl.span(),
59            ItemImplItem::Const(const_decl) => const_decl.span(),
60            ItemImplItem::Type(type_decl) => type_decl.span(),
61        }
62    }
63}