foundry_cheatcodes_spec/
function.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use serde::{Deserialize, Serialize};
use std::fmt;

/// Solidity function.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Function<'a> {
    /// The function's unique identifier. This is the function name, optionally appended with an
    /// index if it is overloaded.
    pub id: &'a str,
    /// The description of the function.
    /// This is a markdown string derived from the NatSpec documentation.
    pub description: &'a str,
    /// The Solidity function declaration, including full type and parameter names, visibility,
    /// etc.
    pub declaration: &'a str,
    /// The Solidity function visibility attribute. This is currently always `external`, but this
    /// may change in the future.
    pub visibility: Visibility,
    /// The Solidity function state mutability attribute.
    pub mutability: Mutability,
    /// The standard function signature used to calculate `selector`.
    /// See the [Solidity docs] for more information.
    ///
    /// [Solidity docs]: https://docs.soliditylang.org/en/latest/abi-spec.html#function-selector
    pub signature: &'a str,
    /// The hex-encoded, "0x"-prefixed 4-byte function selector,
    /// which is the Keccak-256 hash of `signature`.
    pub selector: &'a str,
    /// The 4-byte function selector as a byte array.
    pub selector_bytes: [u8; 4],
}

impl fmt::Display for Function<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.declaration)
    }
}

/// Solidity function visibility attribute. See the [Solidity docs] for more information.
///
/// [Solidity docs]: https://docs.soliditylang.org/en/latest/contracts.html#function-visibility
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub enum Visibility {
    /// The function is only visible externally.
    External,
    /// Visible externally and internally.
    Public,
    /// Only visible internally.
    Internal,
    /// Only visible in the current contract
    Private,
}

impl fmt::Display for Visibility {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl Visibility {
    /// Returns the string representation of the visibility.
    #[inline]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::External => "external",
            Self::Public => "public",
            Self::Internal => "internal",
            Self::Private => "private",
        }
    }
}

/// Solidity function state mutability attribute. See the [Solidity docs] for more information.
///
/// [Solidity docs]: https://docs.soliditylang.org/en/latest/contracts.html#state-mutability
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub enum Mutability {
    /// Disallows modification or access of state.
    Pure,
    /// Disallows modification of state.
    View,
    /// Allows modification of state.
    #[serde(rename = "")]
    None,
}

impl fmt::Display for Mutability {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl Mutability {
    /// Returns the string representation of the mutability.
    #[inline]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Pure => "pure",
            Self::View => "view",
            Self::None => "",
        }
    }
}