Skip to content

Commit

Permalink
Implements From impl from SerializeError to MerkleizationError::Seria…
Browse files Browse the repository at this point in the history
…lizationError.
  • Loading branch information
claravanstaden authored and claravanstaden committed Dec 14, 2022
1 parent 0296103 commit b58448c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ssz-rs/examples/container_with_some_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn main() {
let mut restored_foo = match Foo::<4>::deserialize(&encoding) {
Ok(value) => value,
Err(e) => {
eprintln!("some error decoding: {:?}", e);
eprintln!("some error decoding: {}", e);
return;
}
};
Expand Down
5 changes: 2 additions & 3 deletions ssz-rs/src/bitlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ impl<const N: usize> Bitlist<N> {

fn pack_bits(&self) -> Result<Vec<u8>, MerkleizationError> {
let mut data = vec![];
let _ = self
.serialize_with_length(&mut data, false)
.map_err(|_| MerkleizationError::SerializationError);
let _ = self.serialize_with_length(&mut data, false);
pack_bytes(&mut data);
Ok(data)
}
Expand Down Expand Up @@ -210,6 +208,7 @@ impl<const N: usize> FromIterator<bool> for Bitlist<N> {

#[cfg(test)]
mod tests {
use std::hash::Hash;
use super::*;
use crate::serialize;

Expand Down
4 changes: 1 addition & 3 deletions ssz-rs/src/bitvector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ impl<const N: usize> Bitvector<N> {

fn pack_bits(&self) -> Result<Vec<u8>, MerkleizationError> {
let mut data = vec![];
let _ = self
.serialize(&mut data)
.map_err(|_| MerkleizationError::SerializationError);
let _ = self.serialize(&mut data);
pack_bytes(&mut data);
Ok(data)
}
Expand Down
11 changes: 8 additions & 3 deletions ssz-rs/src/merkleization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use sha2::{Digest, Sha256};
pub use cache::Cache as MerkleCache;
pub use node::Node;
pub use proofs::is_valid_merkle_branch;
use crate::SerializeError;

pub(crate) const BYTES_PER_CHUNK: usize = 32;

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

impl From<SerializeError> for MerkleizationError {
fn from(_: SerializeError) -> Self {
return MerkleizationError::SerializationError
}
}

impl Display for MerkleizationError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Expand Down Expand Up @@ -60,9 +67,7 @@ where
{
let mut buffer = vec![];
for value in values {
value
.serialize(&mut buffer)
.map_err(|_| MerkleizationError::SerializationError)?;
value.serialize(&mut buffer);
}
pack_bytes(&mut buffer);
Ok(buffer)
Expand Down
4 changes: 1 addition & 3 deletions ssz-rs/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ macro_rules! define_uint {
impl Merkleized for $uint {
fn hash_tree_root(&mut self) -> Result<Node, MerkleizationError> {
let mut root = vec![];
let _ = self
.serialize(&mut root)
.map_err(|_| MerkleizationError::SerializationError);
let _ = self.serialize(&mut root)?;
pack_bytes(&mut root);
Ok(root.as_slice().try_into().expect("is valid root"))
}
Expand Down

0 comments on commit b58448c

Please sign in to comment.