Skip to content

Commit

Permalink
return a result from Code::wrap
Browse files Browse the repository at this point in the history
The multihash digest may not fit.
  • Loading branch information
Stebalien committed Nov 9, 2021
1 parent 0c0eb11 commit 8be815b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions derive/src/multihash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ pub fn multihash(s: Structure) -> TokenStream {
}
}

fn wrap(&self, digest: &[u8]) -> Multihash {
Multihash::wrap((*self).into(), digest).unwrap()
fn wrap(&self, digest: &[u8]) -> Result<Multihash, #mh_crate::Error> {
Multihash::wrap((*self).into(), digest)
}
}

Expand Down Expand Up @@ -298,8 +298,8 @@ mod tests {
}
}

fn wrap(&self, digest: &[u8]) -> Multihash {
Multihash::wrap((*self).into(), digest).unwrap()
fn wrap(&self, digest: &[u8]) -> Result<Multihash, multihash::Error> {
Multihash::wrap((*self).into(), digest)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/multihash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait MultihashDigest<const S: usize>:
/// let hash = Code::Sha3_256.wrap(&hasher.finalize());
/// println!("{:02x?}", hash);
/// ```
fn wrap(&self, digest: &[u8]) -> Multihash<S>;
fn wrap(&self, digest: &[u8]) -> Result<Multihash<S>, Error>;
}

/// A Multihash instance that only supports the basic functionality and no hashing.
Expand Down
4 changes: 2 additions & 2 deletions src/multihash_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod tests {
let mut hasher = Sha3_256::default();
hasher.update(b"hello world");
let digest = hasher.finalize();
let hash = Code::Sha3_256.wrap(digest);
let hash = Code::Sha3_256.wrap(digest).unwrap();
let hash2 = Code::Sha3_256.digest(b"hello world");
assert_eq!(hash.code(), u64::from(Code::Sha3_256));
assert_eq!(hash.size(), 32);
Expand All @@ -102,7 +102,7 @@ mod tests {
let mut hasher = Sha3_512::default();
hasher.update(b"hello world");
let digest = hasher.finalize();
let hash = Code::Sha3_512.wrap(digest);
let hash = Code::Sha3_512.wrap(digest).unwrap();
let hash2 = Code::Sha3_512.digest(b"hello world");
assert_eq!(hash.code(), u64::from(Code::Sha3_512));
assert_eq!(hash.size(), 64);
Expand Down
8 changes: 4 additions & 4 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ macro_rules! assert_encode {
let mut hasher = <$alg>::default();
hasher.update($data);
assert_eq!(
$code.wrap(hasher.finalize()).to_bytes(),
$code.wrap(hasher.finalize()).unwrap().to_bytes(),
expected,
"{:?} encodes correctly (from hasher)", stringify!($alg)
);
Expand Down Expand Up @@ -151,7 +151,7 @@ macro_rules! assert_roundtrip {
{
let mut hasher = <$alg>::default();
hasher.update(b"helloworld");
let hash = $code.wrap(hasher.finalize());
let hash = $code.wrap(hasher.finalize()).unwrap();
assert_eq!(
Multihash::from_bytes(&hash.to_bytes()).unwrap().code(),
hash.code()
Expand All @@ -161,7 +161,7 @@ macro_rules! assert_roundtrip {
{
let mut hasher = <$alg>::default();
hasher.write_all(b"helloworld").unwrap();
let hash = $code.wrap(hasher.finalize());
let hash = $code.wrap(hasher.finalize()).unwrap();
assert_eq!(
Multihash::from_bytes(&hash.to_bytes()).unwrap().code(),
hash.code()
Expand Down Expand Up @@ -217,7 +217,7 @@ where
// Test from hasher digest conversion
let mut hasher = H::default();
hasher.update(b"hello world");
let multihash_from_digest = code.wrap(hasher.finalize());
let multihash_from_digest = code.wrap(hasher.finalize()).unwrap();
assert_eq!(multihash_from_digest.code(), u64::from(code));
assert_eq!(multihash_from_digest.size() as usize, digest.len());
assert_eq!(multihash_from_digest.digest(), digest);
Expand Down

0 comments on commit 8be815b

Please sign in to comment.