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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use serde::{Deserialize, Serialize};

const COMPAT_ERROR: &str = "state mutability cannot be both `payable` and `constant`";

/// A JSON ABI function's state mutability.
///
/// This will serialize/deserialize as the `stateMutability` JSON ABI field's value, see
/// [`as_json_str`](Self::as_json_str).
/// For backwards compatible deserialization, see [`serde_state_mutability_compat`].
#[derive(
    Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[serde(rename_all = "lowercase")]
pub enum StateMutability {
    /// Pure functions promise not to read from or modify the state.
    Pure,
    /// View functions promise not to modify the state.
    View,
    /// Nonpayable functions promise not to receive Ether.
    ///
    /// This is the solidity default: <https://docs.soliditylang.org/en/latest/abi-spec.html#json>
    ///
    /// The state mutability nonpayable is reflected in Solidity by not specifying a state
    /// mutability modifier at all.
    #[default]
    NonPayable,
    /// Payable functions make no promises.
    Payable,
}

impl StateMutability {
    /// Returns the string representation of the state mutability.
    #[inline]
    pub const fn as_str(self) -> Option<&'static str> {
        if let Self::NonPayable = self {
            None
        } else {
            Some(self.as_json_str())
        }
    }

    /// Returns the string representation of the state mutability when serialized to JSON.
    #[inline]
    pub const fn as_json_str(self) -> &'static str {
        match self {
            Self::Pure => "pure",
            Self::View => "view",
            Self::NonPayable => "nonpayable",
            Self::Payable => "payable",
        }
    }
}

/// [`serde`] implementation for [`StateMutability`] for backwards compatibility with older
/// versions of the JSON ABI.
///
/// In particular, this will deserialize the `stateMutability` field if it is present,
/// and otherwise fall back to the deprecated `constant` and `payable` fields.
///
/// Since it must be used in combination with `#[serde(flatten)]`, a `serialize` implementation
/// is also provided, which will always serialize the `stateMutability` field.
///
/// # Examples
///
/// Usage: `#[serde(default, flatten, with = "serde_state_mutability_compat")]` on a
/// [`StateMutability`] struct field.
///
/// ```rust
/// use alloy_json_abi::{serde_state_mutability_compat, StateMutability};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize)]
/// #[serde(rename_all = "camelCase")]
/// struct MyStruct {
///     #[serde(default, flatten, with = "serde_state_mutability_compat")]
///     state_mutability: StateMutability,
/// }
///
/// let json = r#"{"constant":true,"payable":false}"#;
/// let ms = serde_json::from_str::<MyStruct>(json).expect("failed deserializing");
/// assert_eq!(ms.state_mutability, StateMutability::View);
///
/// let reserialized = serde_json::to_string(&ms).expect("failed reserializing");
/// assert_eq!(reserialized, r#"{"stateMutability":"view"}"#);
/// ```
pub mod serde_state_mutability_compat {
    use super::*;
    use serde::ser::SerializeStruct;

    /// Deserializes a [`StateMutability`], compatible with older JSON ABI versions.
    ///
    /// See [the module-level documentation](self) for more information.
    pub fn deserialize<'de, D: serde::Deserializer<'de>>(
        deserializer: D,
    ) -> Result<StateMutability, D::Error> {
        #[derive(Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct StateMutabilityCompat {
            #[serde(default)]
            state_mutability: Option<StateMutability>,
            #[serde(default)]
            payable: Option<bool>,
            #[serde(default)]
            constant: Option<bool>,
        }

        impl StateMutabilityCompat {
            fn flatten(self) -> Option<StateMutability> {
                let Self { state_mutability, payable, constant } = self;
                if state_mutability.is_some() {
                    return state_mutability;
                }
                match (payable.unwrap_or(false), constant.unwrap_or(false)) {
                    (false, false) => Some(StateMutability::default()),
                    (true, false) => Some(StateMutability::Payable),
                    (false, true) => Some(StateMutability::View),
                    (true, true) => None,
                }
            }
        }

        StateMutabilityCompat::deserialize(deserializer).and_then(|compat| {
            compat.flatten().ok_or_else(|| serde::de::Error::custom(COMPAT_ERROR))
        })
    }

    /// Serializes a [`StateMutability`] as a single-field struct (`stateMutability`).
    ///
    /// See [the module-level documentation](self) for more information.
    pub fn serialize<S: serde::Serializer>(
        state_mutability: &StateMutability,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        let mut s = serializer.serialize_struct("StateMutability", 1)?;
        s.serialize_field("stateMutability", state_mutability)?;
        s.end()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::string::ToString;

    #[derive(Debug, Serialize, Deserialize)]
    struct CompatTest {
        #[serde(default, flatten, with = "serde_state_mutability_compat")]
        sm: StateMutability,
    }

    #[test]
    fn test_compat() {
        let test = |expect: StateMutability, json: &str| {
            let compat = serde_json::from_str::<CompatTest>(json).expect(json);
            assert_eq!(compat.sm, expect, "{json:?}");

            let re_ser = serde_json::to_string(&compat).expect(json);
            let expect = format!(r#"{{"stateMutability":"{}"}}"#, expect.as_json_str());
            assert_eq!(re_ser, expect, "{json:?}");
        };

        test(StateMutability::Pure, r#"{"stateMutability":"pure"}"#);
        test(
            StateMutability::Pure,
            r#"{"stateMutability":"pure","constant":false,"payable":false}"#,
        );

        test(StateMutability::View, r#"{"constant":true}"#);
        test(StateMutability::View, r#"{"constant":true,"payable":false}"#);

        test(StateMutability::Payable, r#"{"payable":true}"#);
        test(StateMutability::Payable, r#"{"constant":false,"payable":true}"#);

        test(StateMutability::NonPayable, r#"{}"#);
        test(StateMutability::NonPayable, r#"{"constant":false}"#);
        test(StateMutability::NonPayable, r#"{"payable":false}"#);
        test(StateMutability::NonPayable, r#"{"constant":false,"payable":false}"#);

        let json = r#"{"constant":true,"payable":true}"#;
        let e = serde_json::from_str::<CompatTest>(json).unwrap_err().to_string();
        assert!(e.contains(COMPAT_ERROR), "{e:?}");
    }
}