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 claravanstaden committed Nov 22, 2022
1 parent e594546 commit 2b7031c
Show file tree
Hide file tree
Showing 4 changed files with 29 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 @@ -15,12 +15,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
12 changes: 11 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};
use crate::std::{Index, Vec, vec, Option, Debug, Ordering, fmt};
use crate::std::{Index, Vec, vec, Option, Debug, Ordering, fmt, Display, Formatter};
use lazy_static::lazy_static;
use sha2::{Digest, Sha256};

Expand All @@ -25,6 +25,16 @@ pub enum MerkleizationError {
InputExceedsLimit(usize), // cannot merkleize data that exceeds the declared limit {0}
}

impl Display for MerkleizationError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
MerkleizationError::SerializationError => write!(f, "failed to serialize value"),
MerkleizationError::PartialChunk(chunk, size) => write!(f, "cannot merkleize a partial chunk of length {} (data: {:?}", size, chunk),
MerkleizationError::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 @@ -12,6 +12,16 @@ pub enum SerializeError {
IllegalType { bound: usize }, // the type for this value has an illegal bound of {bound}
}

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 @@ -23,15 +23,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 2b7031c

Please sign in to comment.