Skip to content

Commit 4d37e69

Browse files
sisoujsdanielh
authored andcommitted
Refactor match statements into .map/.map_err flows
As per PR review comment.
1 parent 392367e commit 4d37e69

1 file changed

Lines changed: 6 additions & 13 deletions

File tree

web-client/src/common/merkle_path.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ impl MerklePath {
3030
/// Computes the Merkle root of the path given a leaf hash.
3131
#[wasm_bindgen(js_name = computeRoot)]
3232
pub fn compute_root(&self, leaf: &[u8]) -> Result<Vec<u8>, JsError> {
33-
match Blake2bHash::deserialize_from_vec(leaf) {
34-
Ok(leaf_hash) => Ok(self.inner.compute_root(&leaf_hash).serialize_to_vec()),
35-
Err(_) => Err(JsError::new(
36-
"Failed to deserialize leaf: not a Blake2b hash",
37-
)),
38-
}
33+
let leaf_hash = Blake2bHash::deserialize_from_vec(leaf)
34+
.map_err(|_| JsError::new("Failed to deserialize leaf: not a Blake2b hash"))?;
35+
Ok(self.inner.compute_root(&leaf_hash).serialize_to_vec())
3936
}
4037

4138
/// Serializes the Merkle path into a byte array.
@@ -45,13 +42,9 @@ impl MerklePath {
4542

4643
/// Deserializes a Merkle path from a byte array.
4744
pub fn deserialize(data: &[u8]) -> Result<MerklePath, JsError> {
48-
match nimiq_utils::merkle::Blake2bMerklePath::deserialize_from_vec(data) {
49-
Ok(path) => Ok(path.into()),
50-
Err(e) => Err(JsError::new(&format!(
51-
"Failed to deserialize MerklePath: {}",
52-
e
53-
))),
54-
}
45+
nimiq_utils::merkle::Blake2bMerklePath::deserialize_from_vec(data)
46+
.map(|path| path.into())
47+
.map_err(|err| JsError::new(&format!("Failed to deserialize MerklePath: {}", err)))
5548
}
5649
}
5750

0 commit comments

Comments
 (0)