1use crate::{errors, operation::Operation, Constructor, Error, Event, Function};
10use serde::{
11 de::{SeqAccess, Visitor},
12 Deserialize, Deserializer,
13};
14use std::{
15 collections::{hash_map::Values, HashMap},
16 fmt, io,
17 iter::Flatten,
18};
19
20#[derive(Clone, Debug, PartialEq)]
22pub struct Contract {
23 pub constructor: Option<Constructor>,
25 pub functions: HashMap<String, Vec<Function>>,
27 pub events: HashMap<String, Vec<Event>>,
29 pub receive: bool,
31 pub fallback: bool,
33}
34
35impl<'a> Deserialize<'a> for Contract {
36 fn deserialize<D>(deserializer: D) -> Result<Contract, D::Error>
37 where
38 D: Deserializer<'a>,
39 {
40 deserializer.deserialize_any(ContractVisitor)
41 }
42}
43
44struct ContractVisitor;
45
46impl<'a> Visitor<'a> for ContractVisitor {
47 type Value = Contract;
48
49 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
50 formatter.write_str("valid abi spec file")
51 }
52
53 fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
54 where
55 A: SeqAccess<'a>,
56 {
57 let mut result = Contract {
58 constructor: None,
59 functions: HashMap::default(),
60 events: HashMap::default(),
61 receive: true,
62 fallback: false,
63 };
64
65 while let Some(operation) = seq.next_element()? {
66 match operation {
67 Operation::Constructor(constructor) => {
68 result.constructor = Some(constructor);
69 }
70 Operation::Function(func) => {
71 result.functions.entry(func.name.clone()).or_default().push(func);
72 }
73 Operation::Event(event) => {
74 result.events.entry(event.name.clone()).or_default().push(event);
75 }
76 Operation::Fallback => {
77 result.fallback = true;
78 }
79 Operation::Receive => {
80 result.receive = true;
81 }
82 }
83 }
84
85 Ok(result)
86 }
87}
88
89impl Contract {
90 pub fn load<T: io::Read>(reader: T) -> errors::Result<Self> {
92 serde_json::from_reader(reader).map_err(From::from)
93 }
94
95 pub fn constructor(&self) -> Option<&Constructor> {
97 self.constructor.as_ref()
98 }
99
100 pub fn function(&self, name: &str) -> errors::Result<&Function> {
103 self.functions.get(name).into_iter().flatten().next().ok_or_else(|| Error::InvalidName(name.to_owned()))
104 }
105
106 pub fn event(&self, name: &str) -> errors::Result<&Event> {
108 self.events.get(name).into_iter().flatten().next().ok_or_else(|| Error::InvalidName(name.to_owned()))
109 }
110
111 pub fn events_by_name(&self, name: &str) -> errors::Result<&Vec<Event>> {
113 self.events.get(name).ok_or_else(|| Error::InvalidName(name.to_owned()))
114 }
115
116 pub fn functions_by_name(&self, name: &str) -> errors::Result<&Vec<Function>> {
118 self.functions.get(name).ok_or_else(|| Error::InvalidName(name.to_owned()))
119 }
120
121 pub fn functions(&self) -> Functions {
123 Functions(self.functions.values().flatten())
124 }
125
126 pub fn events(&self) -> Events {
128 Events(self.events.values().flatten())
129 }
130}
131
132pub struct Functions<'a>(Flatten<Values<'a, String, Vec<Function>>>);
134
135impl<'a> Iterator for Functions<'a> {
136 type Item = &'a Function;
137
138 fn next(&mut self) -> Option<Self::Item> {
139 self.0.next()
140 }
141}
142
143pub struct Events<'a>(Flatten<Values<'a, String, Vec<Event>>>);
145
146impl<'a> Iterator for Events<'a> {
147 type Item = &'a Event;
148
149 fn next(&mut self) -> Option<Self::Item> {
150 self.0.next()
151 }
152}