forge_doc/writer/traits.rs
1//! Helper traits for writing documentation.
2
3use solang_parser::pt::Expression;
4
5/// Helper trait to abstract over a solang type that can be documented as parameter
6pub(crate) trait ParamLike {
7 /// Returns the type of the parameter.
8 fn ty(&self) -> &Expression;
9
10 /// Returns the type as a string.
11 fn type_name(&self) -> String {
12 self.ty().to_string()
13 }
14
15 /// Returns the identifier of the parameter.
16 fn name(&self) -> Option<&str>;
17}
18
19impl ParamLike for solang_parser::pt::Parameter {
20 fn ty(&self) -> &Expression {
21 &self.ty
22 }
23
24 fn name(&self) -> Option<&str> {
25 self.name.as_ref().map(|id| id.name.as_str())
26 }
27}
28
29impl ParamLike for solang_parser::pt::VariableDeclaration {
30 fn ty(&self) -> &Expression {
31 &self.ty
32 }
33
34 fn name(&self) -> Option<&str> {
35 self.name.as_ref().map(|id| id.name.as_str())
36 }
37}
38
39impl ParamLike for solang_parser::pt::EventParameter {
40 fn ty(&self) -> &Expression {
41 &self.ty
42 }
43
44 fn name(&self) -> Option<&str> {
45 self.name.as_ref().map(|id| id.name.as_str())
46 }
47}
48
49impl ParamLike for solang_parser::pt::ErrorParameter {
50 fn ty(&self) -> &Expression {
51 &self.ty
52 }
53
54 fn name(&self) -> Option<&str> {
55 self.name.as_ref().map(|id| id.name.as_str())
56 }
57}
58
59impl<T> ParamLike for &T
60where
61 T: ParamLike,
62{
63 fn ty(&self) -> &Expression {
64 T::ty(*self)
65 }
66
67 fn name(&self) -> Option<&str> {
68 T::name(*self)
69 }
70}