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
use crate::{errors, operation::Operation, Constructor, Error, Event, Function};
use serde::{
de::{SeqAccess, Visitor},
Deserialize, Deserializer,
};
use std::{
collections::{hash_map::Values, HashMap},
fmt, io,
iter::Flatten,
};
#[derive(Clone, Debug, PartialEq)]
pub struct Contract {
pub constructor: Option<Constructor>,
pub functions: HashMap<String, Vec<Function>>,
pub events: HashMap<String, Vec<Event>>,
pub fallback: bool,
}
impl<'a> Deserialize<'a> for Contract {
fn deserialize<D>(deserializer: D) -> Result<Contract, D::Error>
where
D: Deserializer<'a>,
{
deserializer.deserialize_any(ContractVisitor)
}
}
struct ContractVisitor;
impl<'a> Visitor<'a> for ContractVisitor {
type Value = Contract;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("valid abi spec file")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'a>,
{
let mut result =
Contract { constructor: None, functions: HashMap::default(), events: HashMap::default(), fallback: false };
while let Some(operation) = seq.next_element()? {
match operation {
Operation::Constructor(constructor) => {
result.constructor = Some(constructor);
}
Operation::Function(func) => {
result.functions.entry(func.name.clone()).or_default().push(func);
}
Operation::Event(event) => {
result.events.entry(event.name.clone()).or_default().push(event);
}
Operation::Fallback => {
result.fallback = true;
}
}
}
Ok(result)
}
}
impl Contract {
pub fn load<T: io::Read>(reader: T) -> errors::Result<Self> {
serde_json::from_reader(reader).map_err(From::from)
}
pub fn constructor(&self) -> Option<&Constructor> {
self.constructor.as_ref()
}
pub fn function(&self, name: &str) -> errors::Result<&Function> {
self.functions.get(name).into_iter().flatten().next().ok_or_else(|| Error::InvalidName(name.to_owned()))
}
pub fn event(&self, name: &str) -> errors::Result<&Event> {
self.events.get(name).into_iter().flatten().next().ok_or_else(|| Error::InvalidName(name.to_owned()))
}
pub fn events_by_name(&self, name: &str) -> errors::Result<&Vec<Event>> {
self.events.get(name).ok_or_else(|| Error::InvalidName(name.to_owned()))
}
pub fn functions_by_name(&self, name: &str) -> errors::Result<&Vec<Function>> {
self.functions.get(name).ok_or_else(|| Error::InvalidName(name.to_owned()))
}
pub fn functions(&self) -> Functions {
Functions(self.functions.values().flatten())
}
pub fn events(&self) -> Events {
Events(self.events.values().flatten())
}
pub fn fallback(&self) -> bool {
self.fallback
}
}
pub struct Functions<'a>(Flatten<Values<'a, String, Vec<Function>>>);
impl<'a> Iterator for Functions<'a> {
type Item = &'a Function;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub struct Events<'a>(Flatten<Values<'a, String, Vec<Event>>>);
impl<'a> Iterator for Events<'a> {
type Item = &'a Event;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}