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 pub const fn as_str(self) -> &'static str {
68 match self {
69 Self::External => "external",
70 Self::Public => "public",
71 Self::Internal => "internal",
72 Self::Private => "private",
73 }
74 }
75}
76
77/// Solidity function state mutability attribute. See the [Solidity docs] for more information.
78///
79/// [Solidity docs]: https://docs.soliditylang.org/en/latest/contracts.html#state-mutability
80#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
81#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
82#[serde(rename_all = "camelCase")]
83pub enum Mutability {
84 /// Disallows modification or access of state.
85 Pure,
86 /// Disallows modification of state.
87 View,
88 /// Allows modification of state.
89 #[serde(rename = "")]
90 None,
91}
92
93impl fmt::Display for Mutability {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 f.write_str(self.as_str())
96 }
97}
98
99impl Mutability {
100 /// Returns the string representation of the mutability.
101 pub const fn as_str(self) -> &'static str {
102 match self {
103 Self::Pure => "pure",
104 Self::View => "view",
105 Self::None => "",
106 }
107 }
108}