use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
InvalidLength {
what: &'static str,
expected: usize,
got: usize,
},
InvalidPoint,
InvalidIdentityPoint,
InvalidScalar,
InvalidSecretKey,
InvalidHex,
InvalidPrefix {
expected: &'static str,
got: String,
},
InvalidChecksum,
SignerNotInRing,
RingTooSmall,
DuplicateRingMember,
EmptyVote,
EmptyElectionId,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidLength {
what,
expected,
got,
} => write!(
f,
"invalid length for {what}: expected {expected} bytes, got {got}"
),
Error::InvalidPoint => f.write_str("bytes do not encode a valid Ristretto255 point"),
Error::InvalidIdentityPoint => f.write_str("point must not be the Ristretto identity"),
Error::InvalidScalar => f.write_str("bytes do not encode a canonical scalar"),
Error::InvalidSecretKey => f.write_str("secret key must be a non-zero scalar"),
Error::InvalidHex => f.write_str("input is not valid hexadecimal"),
Error::InvalidPrefix { expected, got } => {
if got.is_empty() {
write!(f, "expected a \"{expected}_\"-prefixed value")
} else {
write!(f, "expected a \"{expected}_\" prefix, got \"{got}_\"")
}
}
Error::InvalidChecksum => {
f.write_str("checksum mismatch: the value was mistyped or corrupted")
}
Error::SignerNotInRing => {
f.write_str("signer's public key is not part of the authorised ring")
}
Error::RingTooSmall => f.write_str("authorised ring must contain at least two members"),
Error::DuplicateRingMember => {
f.write_str("authorised ring contains a duplicate public key")
}
Error::EmptyVote => f.write_str("ballot is empty"),
Error::EmptyElectionId => f.write_str("election identifier is empty"),
}
}
}
impl std::error::Error for Error {}
pub type Result<T> = core::result::Result<T, Error>;