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 receive: bool,
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(),
receive: true,
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;
}
Operation::Receive => {
result.receive = 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 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()
}
}