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
use serde::{
de::{Error, Visitor},
Deserialize, Deserializer,
};
use std::fmt;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum StateMutability {
Pure,
View,
NonPayable,
Payable,
}
impl Default for StateMutability {
fn default() -> Self {
Self::NonPayable
}
}
impl<'a> Deserialize<'a> for StateMutability {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'a>,
{
deserializer.deserialize_any(StateMutabilityVisitor)
}
}
struct StateMutabilityVisitor;
impl<'a> Visitor<'a> for StateMutabilityVisitor {
type Value = StateMutability;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "the string 'pure', 'view', 'payable', or 'nonpayable'")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
use StateMutability::*;
Ok(match v {
"pure" => Pure,
"view" => View,
"payable" => Payable,
"nonpayable" => NonPayable,
_ => return Err(Error::unknown_variant(v, &["pure", "view", "payable", "nonpayable"])),
})
}
}