Skip to content

Commit

Permalink
Switch to a single-variant error type, implement standard derives
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Jan 14, 2021
1 parent 4a7cf34 commit 0df86b4
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/util/uint.rs
Expand Up @@ -98,9 +98,9 @@ macro_rules! construct_uint {

/// Creates big integer value from a byte slice using
/// big-endian encoding
pub fn from_be_slice(bytes: &[u8]) -> Result<$name, Error> {
pub fn from_be_slice(bytes: &[u8]) -> Result<$name, ParseLengthError> {
if bytes.len() != $n_words * 8 {
Err(Error::InvalidLength(bytes.len(), $n_words*8))
Err(ParseLengthError { actual: bytes.len(), expected: $n_words*8 })
} else {
Ok(Self::_from_be_slice(bytes))
}
Expand Down Expand Up @@ -493,11 +493,20 @@ macro_rules! construct_uint {
construct_uint!(Uint256, 4);
construct_uint!(Uint128, 2);

/// Uint error
#[derive(Debug)]
pub enum Error {
/// Invalid slice length (actual, expected)
InvalidLength(usize, usize),
/// Invalid slice length
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Hash)]
/// Invalid slice length
pub struct ParseLengthError {
/// The length of the slice de-facto
pub actual: usize,
/// The required length of the slice
pub expected: usize,
}

impl ::std::fmt::Display for ParseLengthError {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "Invalid length: got {}, expected {}", self.actual, self.expected)
}
}

impl Uint256 {
Expand Down

0 comments on commit 0df86b4

Please sign in to comment.