forge_doc/writer/
traits.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
//! Helper traits for writing documentation.

use solang_parser::pt::Expression;

/// Helper trait to abstract over a solang type that can be documented as parameter
pub(crate) trait ParamLike {
    /// Returns the type of the parameter.
    fn ty(&self) -> &Expression;

    /// Returns the type as a string.
    fn type_name(&self) -> String {
        self.ty().to_string()
    }

    /// Returns the identifier of the parameter.
    fn name(&self) -> Option<&str>;
}

impl ParamLike for solang_parser::pt::Parameter {
    fn ty(&self) -> &Expression {
        &self.ty
    }

    fn name(&self) -> Option<&str> {
        self.name.as_ref().map(|id| id.name.as_str())
    }
}

impl ParamLike for solang_parser::pt::VariableDeclaration {
    fn ty(&self) -> &Expression {
        &self.ty
    }

    fn name(&self) -> Option<&str> {
        self.name.as_ref().map(|id| id.name.as_str())
    }
}

impl ParamLike for solang_parser::pt::EventParameter {
    fn ty(&self) -> &Expression {
        &self.ty
    }

    fn name(&self) -> Option<&str> {
        self.name.as_ref().map(|id| id.name.as_str())
    }
}

impl ParamLike for solang_parser::pt::ErrorParameter {
    fn ty(&self) -> &Expression {
        &self.ty
    }

    fn name(&self) -> Option<&str> {
        self.name.as_ref().map(|id| id.name.as_str())
    }
}

impl<T> ParamLike for &T
where
    T: ParamLike,
{
    fn ty(&self) -> &Expression {
        T::ty(*self)
    }

    fn name(&self) -> Option<&str> {
        T::name(*self)
    }
}