use std::collections::BTreeMap;
use crate::{
bytes::Bytes,
hash::{Address, H256},
spec::{Account, Builtin}
};
use serde::Deserialize;
#[cfg_attr(any(test, feature = "test-helpers"), derive(Clone))]
#[derive(Debug, PartialEq, Deserialize)]
#[serde(untagged)]
pub enum HashOrMap {
Hash(H256),
Map(BTreeMap<Address, Account>),
}
#[cfg_attr(any(test, feature = "test-helpers"), derive(Clone))]
#[derive(Debug, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct State(pub HashOrMap);
impl State {
pub fn builtins(&self) -> BTreeMap<Address, Builtin> {
match &self.0 {
HashOrMap::Hash(_) => BTreeMap::default(),
HashOrMap::Map(map) => {
map.iter().filter_map(|(add, ref acc)| {
acc.builtin.clone().map(|b| (add.clone(), b.into()))
}).collect()
}
}
}
pub fn constructors(&self) -> BTreeMap<Address, Bytes> {
match &self.0 {
HashOrMap::Hash(_) => BTreeMap::default(),
HashOrMap::Map(map) => {
map.iter().filter_map(|(add, ref acc)| {
acc.constructor.clone().map(|b| (add.clone(), b))
}).collect()
}
}
}
}
impl IntoIterator for State {
type Item = <BTreeMap<Address, Account> as IntoIterator>::Item;
type IntoIter = <BTreeMap<Address, Account> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
if let HashOrMap::Map(m) = self.0 {
m.into_iter()
} else {
BTreeMap::default().into_iter()
}
}
}