use core::fmt::{self, Display, Formatter};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub enum Error {
SharingMinThreshold,
SharingLimitLessThanThreshold,
InvalidSizeRequest,
SharingInvalidIdentifier,
SharingDuplicateIdentifier,
SharingMaxRequest,
InvalidShare,
InvalidGenerator(&'static str),
InvalidSecret,
InvalidShareConversion,
NotImplemented,
InvalidShareElement,
NotEnoughShareIdentifiers,
NotEnoughShares,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Error::SharingMinThreshold => write!(f, "Threshold cannot be less than 2"),
Error::SharingLimitLessThanThreshold => write!(f, "Limit is less than threshold"),
Error::InvalidSizeRequest => write!(f, "Requested more shares than space was provided"),
Error::SharingInvalidIdentifier => write!(f, "An invalid share detected"),
Error::SharingDuplicateIdentifier => write!(f, "Duplicate share detected"),
Error::SharingMaxRequest => write!(
f,
"The maximum number of shares to be made when splitting was reached"
),
Error::InvalidShare => write!(
f,
"An invalid share was supplied for verification or combine"
),
Error::InvalidGenerator(s) => write!(
f,
"An invalid generator was supplied for share generation: {}",
s
),
Error::InvalidSecret => write!(f, "An invalid secret was supplied for split"),
Error::InvalidShareConversion => {
write!(f, "A share cannot be converted to a group or field element")
}
Error::NotImplemented => write!(f, "Not implemented"),
Error::InvalidShareElement => write!(f, "Invalid share element"),
Error::NotEnoughShareIdentifiers => write!(f, "Not enough share identifiers available"),
Error::NotEnoughShares => write!(f, "Not enough shares available"),
}
}
}
pub type VsssResult<T> = Result<T, Error>;
#[cfg(test)]
mod tests {
use super::Error;
use std::string::ToString;
#[test]
fn display_messages_cover_all_error_variants() {
let cases = [
(
Error::SharingMinThreshold,
"Threshold cannot be less than 2",
),
(
Error::SharingLimitLessThanThreshold,
"Limit is less than threshold",
),
(
Error::InvalidSizeRequest,
"Requested more shares than space was provided",
),
(Error::SharingInvalidIdentifier, "An invalid share detected"),
(
Error::SharingDuplicateIdentifier,
"Duplicate share detected",
),
(
Error::SharingMaxRequest,
"The maximum number of shares to be made when splitting was reached",
),
(
Error::InvalidShare,
"An invalid share was supplied for verification or combine",
),
(
Error::InvalidGenerator("empty list"),
"An invalid generator was supplied for share generation: empty list",
),
(
Error::InvalidSecret,
"An invalid secret was supplied for split",
),
(
Error::InvalidShareConversion,
"A share cannot be converted to a group or field element",
),
(Error::NotImplemented, "Not implemented"),
(Error::InvalidShareElement, "Invalid share element"),
(
Error::NotEnoughShareIdentifiers,
"Not enough share identifiers available",
),
(Error::NotEnoughShares, "Not enough shares available"),
];
for (error, message) in cases {
assert_eq!(error.to_string(), message);
}
}
}