foundry_cheatcodes_spec/
function.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4/// Solidity function.
5#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
6#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
7#[serde(rename_all = "camelCase")]
8#[non_exhaustive]
9pub struct Function<'a> {
10    /// The function's unique identifier. This is the function name, optionally appended with an
11    /// index if it is overloaded.
12    pub id: &'a str,
13    /// The description of the function.
14    /// This is a markdown string derived from the NatSpec documentation.
15    pub description: &'a str,
16    /// The Solidity function declaration, including full type and parameter names, visibility,
17    /// etc.
18    pub declaration: &'a str,
19    /// The Solidity function visibility attribute. This is currently always `external`, but this
20    /// may change in the future.
21    pub visibility: Visibility,
22    /// The Solidity function state mutability attribute.
23    pub mutability: Mutability,
24    /// The standard function signature used to calculate `selector`.
25    /// See the [Solidity docs] for more information.
26    ///
27    /// [Solidity docs]: https://docs.soliditylang.org/en/latest/abi-spec.html#function-selector
28    pub signature: &'a str,
29    /// The hex-encoded, "0x"-prefixed 4-byte function selector,
30    /// which is the Keccak-256 hash of `signature`.
31    pub selector: &'a str,
32    /// The 4-byte function selector as a byte array.
33    pub selector_bytes: [u8; 4],
34}
35
36impl fmt::Display for Function<'_> {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        f.write_str(self.declaration)
39    }
40}
41
42/// Solidity function visibility attribute. See the [Solidity docs] for more information.
43///
44/// [Solidity docs]: https://docs.soliditylang.org/en/latest/contracts.html#function-visibility
45#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
46#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
47#[serde(rename_all = "camelCase")]
48pub enum Visibility {
49    /// The function is only visible externally.
50    External,
51    /// Visible externally and internally.
52    Public,
53    /// Only visible internally.
54    Internal,
55    /// Only visible in the current contract
56    Private,
57}
58
59impl fmt::Display for Visibility {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        f.write_str(self.as_str())
62    }
63}
64
65impl Visibility {
66    /// Returns the string representation of the visibility.
67    #[inline]
68    pub const fn as_str(self) -> &'static str {
69        match self {
70            Self::External => "external",
71            Self::Public => "public",
72            Self::Internal => "internal",
73            Self::Private => "private",
74        }
75    }
76}
77
78/// Solidity function state mutability attribute. See the [Solidity docs] for more information.
79///
80/// [Solidity docs]: https://docs.soliditylang.org/en/latest/contracts.html#state-mutability
81#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
82#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
83#[serde(rename_all = "camelCase")]
84pub enum Mutability {
85    /// Disallows modification or access of state.
86    Pure,
87    /// Disallows modification of state.
88    View,
89    /// Allows modification of state.
90    #[serde(rename = "")]
91    None,
92}
93
94impl fmt::Display for Mutability {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        f.write_str(self.as_str())
97    }
98}
99
100impl Mutability {
101    /// Returns the string representation of the mutability.
102    #[inline]
103    pub const fn as_str(self) -> &'static str {
104        match self {
105            Self::Pure => "pure",
106            Self::View => "view",
107            Self::None => "",
108        }
109    }
110}