Merge rust-bitcoin/rust-bitcoin#2510: Trivial error cleanup

86f8043e80 Remove Error suffix from variant (Tobin C. Harding)
482c8cb7f8 Clean up error type from impls (Tobin C. Harding)

Pull request description:

  Done while working on other error code; two trivial cleanups.

  This PR gives us a place to debate the current error `From` impl format. The canonical form is, as far as I understand:

  ```rust
  impl From<FooError> for Error {
      #[inline]
      fn from(e: FooError) -> Self { Self::Foo(e) }
  }
  ```

ACKs for top commit:
  apoelstra:
    utACK 86f8043e80
  Kixunil:
    ACK 86f8043e80

Tree-SHA512: 82169baf5ec58f5fd08c7615e6f639721870a264dd93e253d3128ebee5bfe3646354fa10d38b496792bf979e03a1c7a54fb47c7a4dce9822ea7f04b84a0fbac1
This commit is contained in:
Andrew Poelstra 2024-02-28 19:08:20 +00:00
commit 162094322f
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 11 additions and 11 deletions

View File

@ -241,15 +241,15 @@ impl std::error::Error for Error {
} }
impl From<secp256k1::Error> for Error { impl From<secp256k1::Error> for Error {
fn from(e: secp256k1::Error) -> Error { Error::Secp256k1(e) } fn from(e: secp256k1::Error) -> Self { Self::Secp256k1(e) }
} }
impl From<NonStandardSighashTypeError> for Error { impl From<NonStandardSighashTypeError> for Error {
fn from(err: NonStandardSighashTypeError) -> Self { Error::SighashType(err) } fn from(e: NonStandardSighashTypeError) -> Self { Self::SighashType(e) }
} }
impl From<hex::HexToBytesError> for Error { impl From<hex::HexToBytesError> for Error {
fn from(err: hex::HexToBytesError) -> Self { Error::Hex(err) } fn from(e: hex::HexToBytesError) -> Self { Self::Hex(e) }
} }
#[cfg(test)] #[cfg(test)]

View File

@ -921,7 +921,7 @@ impl std::error::Error for FromSliceError {
} }
impl From<secp256k1::Error> for FromSliceError { impl From<secp256k1::Error> for FromSliceError {
fn from(e: secp256k1::Error) -> FromSliceError { Self::Secp256k1(e) } fn from(e: secp256k1::Error) -> Self { Self::Secp256k1(e) }
} }
/// Error generated from WIF key format. /// Error generated from WIF key format.
@ -956,11 +956,11 @@ impl std::error::Error for FromWifError {
} }
impl From<base58::Error> for FromWifError { impl From<base58::Error> for FromWifError {
fn from(e: base58::Error) -> FromWifError { Self::Base58(e) } fn from(e: base58::Error) -> Self { Self::Base58(e) }
} }
impl From<secp256k1::Error> for FromWifError { impl From<secp256k1::Error> for FromWifError {
fn from(e: secp256k1::Error) -> FromWifError { Self::Secp256k1(e) } fn from(e: secp256k1::Error) -> Self { Self::Secp256k1(e) }
} }
/// Error returned while constructing public key from string. /// Error returned while constructing public key from string.
@ -1007,7 +1007,7 @@ pub enum ParseCompressedPublicKeyError {
/// Secp256k1 Error. /// Secp256k1 Error.
Secp256k1(secp256k1::Error), Secp256k1(secp256k1::Error),
/// hex to array conversion error. /// hex to array conversion error.
HexError(hex::HexToArrayError), Hex(hex::HexToArrayError),
} }
impl fmt::Display for ParseCompressedPublicKeyError { impl fmt::Display for ParseCompressedPublicKeyError {
@ -1015,7 +1015,7 @@ impl fmt::Display for ParseCompressedPublicKeyError {
use ParseCompressedPublicKeyError::*; use ParseCompressedPublicKeyError::*;
match self { match self {
Secp256k1(e) => write_err!(f, "secp256k1 error"; e), Secp256k1(e) => write_err!(f, "secp256k1 error"; e),
HexError(e) => write_err!(f, "invalid hex"; e) Hex(e) => write_err!(f, "invalid hex"; e)
} }
} }
} }
@ -1027,17 +1027,17 @@ impl std::error::Error for ParseCompressedPublicKeyError {
match self { match self {
Secp256k1(e) => Some(e), Secp256k1(e) => Some(e),
HexError(e) => Some(e), Hex(e) => Some(e),
} }
} }
} }
impl From<secp256k1::Error> for ParseCompressedPublicKeyError { impl From<secp256k1::Error> for ParseCompressedPublicKeyError {
fn from(e: secp256k1::Error) -> ParseCompressedPublicKeyError { Self::Secp256k1(e) } fn from(e: secp256k1::Error) -> Self { Self::Secp256k1(e) }
} }
impl From<hex::HexToArrayError> for ParseCompressedPublicKeyError { impl From<hex::HexToArrayError> for ParseCompressedPublicKeyError {
fn from(e: hex::HexToArrayError) -> ParseCompressedPublicKeyError { Self::HexError(e) } fn from(e: hex::HexToArrayError) -> Self { Self::Hex(e) }
} }
/// Segwit public keys must always be compressed. /// Segwit public keys must always be compressed.