use std::error::Error as StdError;
use std::fmt;
error_chain! {
errors {
Serialize {
description("unable to serialize a value")
}
AlreadyInserted {
description("a commit or secret has already been inserted for this key")
}
NotPresent {
description("no commit exists for this key")
}
Empty {
description("no commits added to exchange")
}
}
}
#[derive(Debug)]
pub enum RevealErrorKind {
MissingSecret,
ValidationFailed,
}
pub struct RevealError<I> {
failed: Vec<(RevealErrorKind, I)>,
}
impl<I> fmt::Debug for RevealError<I> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "RevealError {{")?;
writeln!(f, " failed: [")?;
for (kind, _) in self.failed.iter() {
writeln!(f, " ({:?}, _),", kind)?;
}
writeln!(f, " ]")?;
writeln!(f, "}}")?;
Ok(())
}
}
impl<I> fmt::Display for RevealError<I> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"unable to reveal, {} participant(s) failed",
self.failed.len()
)
}
}
impl<I> StdError for RevealError<I> {}
impl<I> RevealError<I> {
pub(crate) fn new(failed: Vec<(RevealErrorKind, I)>) -> Self {
Self { failed }
}
pub fn failed(self) -> impl Iterator<Item = (RevealErrorKind, I)> {
self.failed.into_iter()
}
}
pub type RevealResult<T, I> = ::std::result::Result<T, RevealError<I>>;