consensus_encoding: flatten error constructors
What changed, and why it matters
This commit is a pure code-style refactor. It rewrites how error values are built so each wrapping step is on its own line instead of being nested inside a single expression. The actual error values produced and the program's behavior are unchanged. There is no security fix or vulnerability here.
No security action needed. Treat as a normal readability refactor during code review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch flattens nested error constructors in consensus_encoding. For example, slice.try_into().map_err(|_| CompactSizeDecoderError(E::UnexpectedEof { ... })) becomes slice.try_into().map_err(|_| E::UnexpectedEof { ... }).map_err(CompactSizeDecoderError). Similarly, FromHexError(FromHexErrorInner::Decode(DecodeError::Parse(e))) chains three map_err calls. An #[allow(clippy::unnecessary_map_on_constructor)] attribute is added because the new style triggers a clippy lint. No logic, control flow, or error semantics change.
Changed components
consensus_encoding/src/compact_size.rsconsensus_encoding/src/decode/mod.rsInspect captured patch +26 / −11
diff --git a/consensus_encoding/src/compact_size.rs b/consensus_encoding/src/compact_size.rs
index 7a264b31..a2efb70b 100644
--- a/consensus_encoding/src/compact_size.rs
+++ b/consensus_encoding/src/compact_size.rs
@@ -278,14 +278,16 @@ fn compact_size_decode_u64(buf: &ArrayVec<u8, 9>) -> Result<u64, CompactSizeDeco
use CompactSizeDecoderErrorInner as E;
fn arr<const N: usize>(slice: &[u8]) -> Result<[u8; N], CompactSizeDecoderError> {
- slice.try_into().map_err(|_| {
- CompactSizeDecoderError(E::UnexpectedEof { required: N, received: slice.len() })
- })
+ slice
+ .try_into()
+ .map_err(|_| E::UnexpectedEof { required: N, received: slice.len() })
+ .map_err(CompactSizeDecoderError)
}
let (first, payload) = buf
.split_first()
- .ok_or(CompactSizeDecoderError(E::UnexpectedEof { required: 1, received: 0 }))?;
+ .ok_or(E::UnexpectedEof { required: 1, received: 0 })
+ .map_err(CompactSizeDecoderError)?;
match *first {
PREFIX_U64 => {
diff --git a/consensus_encoding/src/decode/mod.rs b/consensus_encoding/src/decode/mod.rs
index a7d933f2..aa3e5fb0 100644
--- a/consensus_encoding/src/decode/mod.rs
+++ b/consensus_encoding/src/decode/mod.rs
@@ -166,6 +166,7 @@ pub fn decode_from_hex_with_decoder<D: Decoder + Default>(
}
#[cfg(feature = "hex")]
+#[allow(clippy::unnecessary_map_on_constructor)]
fn decode_from_hex_internal<D: Decoder>(
hex: &str,
mut decoder: D,
@@ -187,12 +188,15 @@ fn decode_from_hex_internal<D: Decoder>(
while !to_flush.is_empty() {
if decoder
.push_bytes(&mut to_flush)
- .map_err(|e| FromHexError(FromHexErrorInner::Decode(DecodeError::Parse(e))))?
+ .map_err(DecodeError::Parse)
+ .map_err(FromHexErrorInner::Decode)
+ .map_err(FromHexError)?
.is_ready()
{
- return Err(FromHexError(FromHexErrorInner::Decode(DecodeError::Unconsumed(
- UnconsumedError(),
- ))));
+ return Err(UnconsumedError())
+ .map_err(DecodeError::Unconsumed)
+ .map_err(FromHexErrorInner::Decode)
+ .map_err(FromHexError);
}
}
index = 0;
@@ -205,7 +209,9 @@ fn decode_from_hex_internal<D: Decoder>(
while !to_flush.is_empty() {
if decoder
.push_bytes(&mut to_flush)
- .map_err(|e| FromHexError(FromHexErrorInner::Decode(DecodeError::Parse(e))))?
+ .map_err(DecodeError::Parse)
+ .map_err(FromHexErrorInner::Decode)
+ .map_err(FromHexError)?
.is_ready()
{
break;
@@ -213,9 +219,16 @@ fn decode_from_hex_internal<D: Decoder>(
}
if to_flush.is_empty() {
- decoder.end().map_err(|e| FromHexError(FromHexErrorInner::Decode(DecodeError::Parse(e))))
+ decoder
+ .end()
+ .map_err(DecodeError::Parse)
+ .map_err(FromHexErrorInner::Decode)
+ .map_err(FromHexError)
} else {
- Err(FromHexError(FromHexErrorInner::Decode(DecodeError::Unconsumed(UnconsumedError()))))
+ Err(UnconsumedError())
+ .map_err(DecodeError::Unconsumed)
+ .map_err(FromHexErrorInner::Decode)
+ .map_err(FromHexError)
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.