ethabi_next/
state_mutability.rs

1use serde::{
2	de::{Error, Visitor},
3	Deserialize, Deserializer,
4};
5use std::fmt;
6
7/// Whether a function modifies or reads blockchain state
8#[derive(Debug, Copy, Clone, PartialEq, Eq)]
9pub enum StateMutability {
10	/// Specified not to read blockchain state
11	Pure,
12	/// Specified to not modify the blockchain state
13	View,
14	/// Function does not accept Ether - the default
15	NonPayable,
16	/// Function accepts Ether
17	Payable,
18}
19
20impl Default for StateMutability {
21	fn default() -> Self {
22		Self::NonPayable
23	}
24}
25
26impl<'a> Deserialize<'a> for StateMutability {
27	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
28	where
29		D: Deserializer<'a>,
30	{
31		deserializer.deserialize_any(StateMutabilityVisitor)
32	}
33}
34
35struct StateMutabilityVisitor;
36
37impl<'a> Visitor<'a> for StateMutabilityVisitor {
38	type Value = StateMutability;
39
40	fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
41		write!(formatter, "the string 'pure', 'view', 'payable', or 'nonpayable'")
42	}
43
44	fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
45	where
46		E: Error,
47	{
48		use StateMutability::*;
49		Ok(match v {
50			"pure" => Pure,
51			"view" => View,
52			"payable" => Payable,
53			"nonpayable" => NonPayable,
54			_ => return Err(Error::unknown_variant(v, &["pure", "view", "payable", "nonpayable"])),
55		})
56	}
57}