use commit::Commit;
use errors::*;
use reveal::Reveal;
use crypto_mac::Mac;
use std::cmp::Eq;
use std::collections::hash_map::{Entry, HashMap};
use std::hash::Hash;
pub struct Exchange<T, I, M>
where
M: Mac,
{
commits: HashMap<I, Commit<T, M>>,
}
impl<T, I, M> Exchange<T, I, M>
where
M: Mac,
I: Eq + Hash,
{
pub fn new() -> Self {
Self {
commits: HashMap::new(),
}
}
pub fn insert(&mut self, id: I, commit: Commit<T, M>) -> Result<()> {
match self.commits.entry(id) {
Entry::Occupied(_) => bail!(ErrorKind::AlreadyInserted),
Entry::Vacant(v) => {
v.insert(commit);
}
}
Ok(())
}
pub fn reveal(self) -> Result<Reveal<T, I, M>> {
if self.commits.is_empty() {
bail!(ErrorKind::Empty);
} else {
Ok(Reveal::new(self.commits))
}
}
}