Skip to content

Commit

Permalink
Change error messages back to what they were. (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: claravanstaden <Cats 4 life!>
  • Loading branch information
claravanstaden authored and ralexstokes committed Jan 22, 2023
1 parent a7eda87 commit a231367
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
10 changes: 5 additions & 5 deletions ssz-rs/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ pub enum DeserializeError {
impl Display for DeserializeError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match *self {
DeserializeError::InputTooShort => write!(f, "InputTooShort"),
DeserializeError::ExtraInput => write!(f, "ExtraInput"),
DeserializeError::InvalidInput => write!(f, "InvalidInput"),
DeserializeError::InputTooShort => write!(f, "expected further data when decoding"),
DeserializeError::ExtraInput => write!(f, "unexpected additional data provided when decoding"),
DeserializeError::InvalidInput => write!(f, "invalid data for expected type"),
DeserializeError::IOError => write!(f, "IOError"),
DeserializeError::TypeBoundsViolated { bound, len } => write!(f, "TypeBoundsViolated bound: {} len {}", bound, len),
DeserializeError::IllegalType { bound } => write!(f, "IllegalType bound: {}", bound),
DeserializeError::TypeBoundsViolated { bound, len } => write!(f, "the type for this value has a bound of {} but the value has {} elements", bound, len),
DeserializeError::IllegalType { bound } => write!(f, "the type for this value has an illegal bound of {}", bound),
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion ssz-rs/src/merkleization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod node;
mod proofs;

use crate::ser::{Serialize, SerializeError};
use crate::std::{fmt::Debug, vec, Index, Option, Ordering, Vec};
use crate::std::{fmt::Debug, vec, Display, Formatter, Index, Option, Ordering, Vec};
use lazy_static::lazy_static;
use sha2::{Digest, Sha256};

Expand All @@ -26,6 +26,20 @@ pub enum MerkleizationError {
InputExceedsLimit(usize),
}

impl Display for MerkleizationError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Self::SerializationError(err) => {
write!(f, "failed to serialize value: {err}")
}
Self::InputExceedsLimit(size) => write!(
f,
"cannot merkleize data that exceeds the declared limit: {size}",
),
}
}
}

pub fn pack_bytes(buffer: &mut Vec<u8>) {
let data_len = buffer.len();
if data_len % BYTES_PER_CHUNK != 0 {
Expand Down
10 changes: 10 additions & 0 deletions ssz-rs/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ pub enum SerializeError {
InvalidType(/*#[from]*/ TypeError),
}

impl Display for SerializeError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match *self {
SerializeError::MaximumEncodedLengthExceeded(size) => write!(f, "the encoded length is {} which exceeds the maximum length {}", size, MAXIMUM_LENGTH),
SerializeError::TypeBoundsViolated{ bound, len } => write!(f, "the type for this value has a bound of {} but the value has {} elements", bound, len),
SerializeError::IllegalType{ bound } => write!(f, "the type for this value has an illegal bound of {}", bound),
}
}
}

pub trait Serialize {
/// Append an encoding of `self` to the `buffer`.
/// Return the number of bytes written.
Expand Down
10 changes: 3 additions & 7 deletions ssz-rs/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@ pub struct Vector<T: SimpleSerialize, const N: usize> {
cache: MerkleCache,
}

impl Debug for VectorError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self)
}
}

impl Display for VectorError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self)
match *self {
VectorError::IncorrectLength{ expected, provided } => write!(f, "incorrect number of elements {} to make a Vector of length {}", provided, expected),
}
}
}

Expand Down

0 comments on commit a231367

Please sign in to comment.