bitcoin: Move witness_program module errors to error submodule
What changed, and why it matters
This commit is a simple internal code cleanup. It moves the definition of error types for witness programs into a new 'error' submodule and re-exports them at the original location. There is no change to how the code behaves, no bug fix, and no security improvement or regression.
No security action needed. Treat as a normal refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors the witness_program module in rust-bitcoin to follow the project’s convention of placing error types in a dedicated error submodule. It moves the Error enum, its From<Infallible>, Display, and std::error::Error implementations into witness_program::error and adds pub use self::error::Error; to preserve the public API. The diff is purely structural; no logic, validation, or behavior is modified.
Changed components
bitcoin/src/blockdata/script/witness_program.rsInspect captured patch +35 / −28
diff --git a/bitcoin/src/blockdata/script/witness_program.rs b/bitcoin/src/blockdata/script/witness_program.rs
index 422bdaca..e991aa6d 100644
--- a/bitcoin/src/blockdata/script/witness_program.rs
+++ b/bitcoin/src/blockdata/script/witness_program.rs
@@ -7,9 +7,6 @@
//!
//! [BIP-0141]: <https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki>
-use core::convert::Infallible;
-use core::fmt;
-
use internals::array_vec::ArrayVec;
use super::witness_version::WitnessVersion;
@@ -18,6 +15,10 @@ use crate::crypto::key::{FullPublicKey, TapTweak, TweakedPublicKey, UntweakedPub
use crate::script::WitnessScriptExt as _;
use crate::taproot::TapNodeHash;
+#[rustfmt::skip] // Keep public re-exports separate.
+#[doc(no_inline)]
+pub use self::error::Error;
+
/// The minimum byte size of a segregated witness program.
pub const MIN_SIZE: usize = 2;
@@ -141,36 +142,42 @@ impl WitnessProgram {
}
}
-/// Witness program error.
-#[derive(Clone, Debug, PartialEq, Eq)]
-#[non_exhaustive]
-pub enum Error {
- /// The witness program must be between 2 and 40 bytes in length.
- InvalidLength(usize),
- /// A v0 witness program must be either of length 20 or 32.
- InvalidSegwitV0Length(usize),
-}
+/// Error types for witness programs.
+pub mod error {
+ use core::convert::Infallible;
+ use core::fmt;
-impl From<Infallible> for Error {
- fn from(never: Infallible) -> Self { match never {} }
-}
+ /// Witness program error.
+ #[derive(Clone, Debug, PartialEq, Eq)]
+ #[non_exhaustive]
+ pub enum Error {
+ /// The witness program must be between 2 and 40 bytes in length.
+ InvalidLength(usize),
+ /// A v0 witness program must be either of length 20 or 32.
+ InvalidSegwitV0Length(usize),
+ }
-impl fmt::Display for Error {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self {
- Self::InvalidLength(len) =>
- write!(f, "witness program must be between 2 and 40 bytes: length={}", len),
- Self::InvalidSegwitV0Length(len) =>
- write!(f, "a v0 witness program must be either 20 or 32 bytes: length={}", len),
+ impl From<Infallible> for Error {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
+
+ impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Self::InvalidLength(len) =>
+ write!(f, "witness program must be between 2 and 40 bytes: length={}", len),
+ Self::InvalidSegwitV0Length(len) =>
+ write!(f, "a v0 witness program must be either 20 or 32 bytes: length={}", len),
+ }
}
}
-}
-#[cfg(feature = "std")]
-impl std::error::Error for Error {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- match self {
- Self::InvalidLength(_) | Self::InvalidSegwitV0Length(_) => None,
+ #[cfg(feature = "std")]
+ impl std::error::Error for Error {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ match self {
+ Self::InvalidLength(_) | Self::InvalidSegwitV0Length(_) => None,
+ }
}
}
}
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.