pow: Move all error types into error submodule and re-export
What changed, and why it matters
This commit is a routine internal code cleanup in the rust-bitcoin library. It moves two error types (ParseTargetError and ParseWorkError) from a macro-generated location into a dedicated error submodule and re-exports them. The change does not alter parsing behavior, security checks, or public API functionality; it only reorganizes where the error types are defined and slightly adjusts their error messages to use a consistent internal helper.
No security action required. Treat as a normal refactoring commit during code review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff refactors proof-of-work error types in units/src/pow.rs. Previously, ParseTargetError and ParseWorkError were generated inside the do_impl! macro. The commit removes that macro-generated code and instead defines explicit ParseWorkError and ParseTargetError structs in the existing error submodule. It adds #[doc(no_inline)] re-exports at the top of pow.rs, changes the Display implementation to use internals::write_err with explicit ‘work parse error’ and ‘target parse error’ strings, and makes the inner ParseU256Error field pub(super). No logic changes to parsing, validation, or consensus behavior are present.
Changed components
units/src/pow.rsParseTargetErrorParseWorkErrordo_impl! macroInspect captured patch +50 / −22
diff --git a/units/src/pow.rs b/units/src/pow.rs
index 8fdffef6..b60c3219 100644
--- a/units/src/pow.rs
+++ b/units/src/pow.rs
@@ -16,6 +16,8 @@ use crate::parse_int::{self, ParseIntError, PrefixedHexError, UnprefixedHexError
#[cfg(feature = "encoding")]
#[doc(no_inline)]
pub use self::error::CompactTargetDecoderError;
+#[doc(no_inline)]
+pub use self::error::{ParseTargetError, ParseWorkError};
/// Implement traits and methods shared by `Target` and `Work`.
macro_rules! do_impl {
@@ -99,25 +101,6 @@ macro_rules! do_impl {
U256::from_str(s).map($ty).map_err($err_ty)
}
}
-
- #[doc = "Error returned when parsing a [`"]
- #[doc = stringify!($ty)]
- #[doc = "`] from a string."]
- #[derive(Debug, Clone, PartialEq, Eq)]
- pub struct $err_ty(ParseU256Error);
-
- impl From<core::convert::Infallible> for $err_ty {
- fn from(never: core::convert::Infallible) -> Self { match never {} }
- }
-
- impl fmt::Display for $err_ty {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.0.fmt(f) }
- }
-
- #[cfg(feature = "std")]
- impl std::error::Error for $err_ty {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
- }
};
}
@@ -373,14 +356,13 @@ impl encoding::Decodable for CompactTarget {
/// Error types for proof-of-work related integer types.
pub mod error {
- #[cfg(feature = "encoding")]
use core::convert::Infallible;
- #[cfg(feature = "encoding")]
use core::fmt;
- #[cfg(feature = "encoding")]
use internals::write_err;
+ use super::ParseU256Error;
+
/// An error consensus decoding an `CompactTarget`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg(feature = "encoding")]
@@ -403,6 +385,52 @@ pub mod error {
impl std::error::Error for CompactTargetDecoderError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
}
+
+ /// Error returned when parsing a [`Work`] from a string.
+ ///
+ /// [`Work`]: super::Work
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ pub struct ParseWorkError(pub(super) ParseU256Error);
+
+ impl From<Infallible> for ParseWorkError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
+
+ impl fmt::Display for ParseWorkError {
+ #[inline]
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write_err!(f, "work parse error"; self.0)
+ }
+ }
+
+ #[cfg(feature = "std")]
+ impl std::error::Error for ParseWorkError {
+ #[inline]
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
+ }
+
+ /// Error returned when parsing a [`Target`] from a string.
+ ///
+ /// [`Target`]: super::Target
+ #[derive(Debug, Clone, PartialEq, Eq)]
+ pub struct ParseTargetError(pub(super) ParseU256Error);
+
+ impl From<Infallible> for ParseTargetError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
+
+ impl fmt::Display for ParseTargetError {
+ #[inline]
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write_err!(f, "target parse error"; self.0)
+ }
+ }
+
+ #[cfg(feature = "std")]
+ impl std::error::Error for ParseTargetError {
+ #[inline]
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
+ }
}
#[cfg(feature = "arbitrary")]
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.