Merge rust-bitcoin/rust-bitcoin#7102: Flatten nested error constructors
What changed, and why it matters
This commit is a code-style cleanup in the rust-bitcoin library. It rewrites how some internal error types are constructed so that nested error wrappers are created in a flatter, more consistent way using a common Rust pattern (map_err). There is no functional change to how the library behaves, no bug fix, and no security improvement or vulnerability introduced.
No security action required. Treat as normal refactoring and review for code-style consistency if desired.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The merge commit flattens nested error constructors across base58, key_expression, primitives, and units modules. Each change replaces direct construction of an outer error variant containing an inner error (e.g., OuterError::Inner(InnerError::new(…))) with construction of the inner error followed by .map_err(OuterError::Inner). This is a purely cosmetic refactoring that preserves the same error values and control flow. No logic, bounds checks, or public APIs are altered.
Changed components
base58/src/lib.rskey_expression/src/bip32.rsprimitives/src/block.rsprimitives/src/hash_types/script_hash.rsunits/src/amount/mod.rsInspect captured patch +12 / −13
### base58/src/lib.rs
@@ -121,7 +121,8 @@ fn build_base256<T: Buffer>(data: &str, scratch: &mut T) -> Result<(), Base256Er
.get(usize::from(d58))
.copied()
.flatten()
- .ok_or(Base256Error::InvalidChar(InvalidCharacterError::new(d58)))
+ .ok_or(InvalidCharacterError::new(d58))
+ .map_err(Base256Error::InvalidChar)
.map(u32::from)?;
for d256 in scratch.slice_mut() {
carry += u32::from(*d256) * 58;
### key_expression/src/bip32.rs
@@ -944,7 +944,8 @@ impl Xpub {
depth: self
.depth
.checked_add(1)
- .ok_or(DeriveXpubError::MaximumDepthExceeded(MaximumDepthExceededError {}))?,
+ .ok_or(MaximumDepthExceededError {})
+ .map_err(DeriveXpubError::MaximumDepthExceeded)?,
parent_fingerprint: self.fingerprint(),
child_number,
public_key: tweaked,
@@ -1074,9 +1075,8 @@ impl FromStr for Xpriv {
let data = base58::decode_check(inp)?;
if data.len() != 78 {
- return Err(ParseXprivError::InvalidBase58PayloadLength(
- InvalidBase58PayloadLengthError { length: data.len() },
- ));
+ return Err(InvalidBase58PayloadLengthError { length: data.len() })
+ .map_err(ParseXprivError::InvalidBase58PayloadLength);
}
Self::decode(&data).map_err(ParseXprivError::Decode)
@@ -1096,9 +1096,8 @@ impl FromStr for Xpub {
let data = base58::decode_check(inp)?;
if data.len() != 78 {
- return Err(ParseXpubError::InvalidBase58PayloadLength(
- InvalidBase58PayloadLengthError { length: data.len() },
- ));
+ return Err(InvalidBase58PayloadLengthError { length: data.len() })
+ .map_err(ParseXpubError::InvalidBase58PayloadLength);
}
Self::decode(&data).map_err(ParseXpubError::Decode)
### primitives/src/block.rs
@@ -621,7 +621,7 @@ crate::decoder_newtype! {
fn end(result: Result<(Header, Vec<Transaction>), <BlockInnerDecoder as encoding::Decoder>::Error>) -> Result<Block, BlockDecoderError> {
let (header, transactions) =
- result.map_err(|e| BlockDecoderError(error::BlockDecoderErrorInner::Decode(e)))?;
+ result.map_err(error::BlockDecoderErrorInner::Decode).map_err(BlockDecoderError)?;
let weight = block_weight_wu(&transactions);
if weight > crate::transaction::MAX_BLOCK_WEIGHT.to_wu() {
return Err(BlockDecoderError(error::BlockDecoderErrorInner::BlockTooHeavy(weight)));
### primitives/src/hash_types/script_hash.rs
@@ -80,9 +80,8 @@ impl ScriptHash {
T: ScriptHashableTag,
{
if redeem_script.len() > MAX_REDEEM_SCRIPT_SIZE {
- return Err(RedeemScriptError::Size(RedeemScriptSizeError {
- size: redeem_script.len(),
- }));
+ return Err(RedeemScriptSizeError { size: redeem_script.len() })
+ .map_err(RedeemScriptError::Size);
}
match redeem_script.witness_version() {
None | Some(WitnessVersion::V0) => {}
### units/src/amount/mod.rs
@@ -223,7 +223,7 @@ impl FromStr for Denomination {
let form = Self::forms(s);
- form.ok_or_else(|| E::Unknown(UnknownDenominationError(s.into())))
+ form.ok_or_else(|| UnknownDenominationError(s.into())).map_err(E::Unknown)
}
}
Why this scored 19/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.