bitcoin: Move network module errors to error submodule
What changed, and why it matters
This commit is a straightforward internal code reorganization. It moves two error types related to Bitcoin network parsing into a new 'error' submodule and re-exports them so existing code keeps working. There is no functional change, no bug fix, and no security improvement or regression visible in the diff.
No security action needed. Treat as a normal maintainability refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors bitcoin/src/network/mod.rs by creating a pub mod error and moving UnknownChainHashError plus the re-export of ParseNetworkError into it. The TryFrom
Changed components
bitcoin/src/network/mod.rsInspect captured patch +38 / −28
diff --git a/bitcoin/src/network/mod.rs b/bitcoin/src/network/mod.rs
index f4f390cd..a1798454 100644
--- a/bitcoin/src/network/mod.rs
+++ b/bitcoin/src/network/mod.rs
@@ -8,18 +8,17 @@
pub mod params;
-use core::fmt;
-
use crate::constants::ChainHash;
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)]
pub use self::params::Params;
-#[doc(no_inline)]
-pub use network::ParseNetworkError;
#[doc(inline)]
pub use network::{Network, NetworkKind, TestnetVersion};
+#[doc(no_inline)]
+pub use self::error::{ParseNetworkError, UnknownChainHashError};
+
/// Extension functionality for the [`Network`] type.
// `define_extension_trait` chokes on the rustdoc example code.
pub trait NetworkExt: sealed::Sealed {
@@ -77,34 +76,45 @@ mod sealed {
impl Sealed for super::Network {}
}
-/// Error in parsing network from chain hash.
-#[derive(Debug, Clone, PartialEq, Eq)]
-#[non_exhaustive]
-pub struct UnknownChainHashError(ChainHash);
+/// Error types for network operations.
+pub mod error {
+ use core::fmt;
+
+ use super::{ChainHash, Network, TestnetVersion};
+
+ #[rustfmt::skip] // Keep public re-exports separate.
+ #[doc(no_inline)]
+ pub use network::ParseNetworkError;
-impl fmt::Display for UnknownChainHashError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "unknown chain hash: {}", self.0)
+ /// Error in parsing network from chain hash.
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ #[non_exhaustive]
+ pub struct UnknownChainHashError(ChainHash);
+
+ impl fmt::Display for UnknownChainHashError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "unknown chain hash: {}", self.0)
+ }
}
-}
-#[cfg(feature = "std")]
-impl std::error::Error for UnknownChainHashError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
-}
+ #[cfg(feature = "std")]
+ impl std::error::Error for UnknownChainHashError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
+ }
-impl TryFrom<ChainHash> for Network {
- type Error = UnknownChainHashError;
-
- fn try_from(chain_hash: ChainHash) -> Result<Self, Self::Error> {
- match chain_hash {
- // Note: any new network entries must be matched against here.
- ChainHash::BITCOIN => Ok(Self::Bitcoin),
- ChainHash::TESTNET3 => Ok(Self::Testnet(TestnetVersion::V3)),
- ChainHash::TESTNET4 => Ok(Self::Testnet(TestnetVersion::V4)),
- ChainHash::SIGNET => Ok(Self::Signet),
- ChainHash::REGTEST => Ok(Self::Regtest),
- _ => Err(UnknownChainHashError(chain_hash)),
+ impl TryFrom<ChainHash> for Network {
+ type Error = UnknownChainHashError;
+
+ fn try_from(chain_hash: ChainHash) -> Result<Self, Self::Error> {
+ match chain_hash {
+ // Note: any new network entries must be matched against here.
+ ChainHash::BITCOIN => Ok(Self::Bitcoin),
+ ChainHash::TESTNET3 => Ok(Self::Testnet(TestnetVersion::V3)),
+ ChainHash::TESTNET4 => Ok(Self::Testnet(TestnetVersion::V4)),
+ ChainHash::SIGNET => Ok(Self::Signet),
+ ChainHash::REGTEST => Ok(Self::Regtest),
+ _ => Err(UnknownChainHashError(chain_hash)),
+ }
}
}
}
Why this scored 20/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.