diff --git a/src/error.rs b/src/error.rs index b0a0bf6a8..b2679661b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -13,6 +13,57 @@ use crate::key::error::{ }; use crate::{schnorr, MessageLengthError}; +/// Implements `From for $error` for all the specific errors in this crate. +/// +/// Requires `$error` to be a an enum with a variant `Secp256k1`. +#[macro_export] +macro_rules! impl_from_for_all_crate_errors_for { + ($error:ty) => { + $crate::impl_from_for_all_crate_errors_for!($error, Secp256k1); + }; + ($error:ty, $variant:ident) => { + impl From<$crate::Error> for $error { + fn from(e: $crate::Error) -> Self { Self::$variant(e.into()) } + } + impl From<$crate::NotEnoughMemoryError> for $error { + fn from(e: $crate::NotEnoughMemoryError) -> Self { Self::$variant(e.into()) } + } + impl From<$crate::MessageLengthError> for $error { + fn from(e: $crate::MessageLengthError) -> Self { Self::$variant(e.into()) } + } + impl From<$crate::schnorr::SignatureError> for $error { + fn from(e: $crate::schnorr::SignatureError) -> Self { Self::$variant(e.into()) } + } + impl From<$crate::ecdsa::SignatureError> for $error { + fn from(e: $crate::ecdsa::SignatureError) -> Self { Self::$variant(e.into()) } + } + impl From<$crate::RecoveryIdError> for $error { + fn from(e: $crate::RecoveryIdError) -> Self { Self::$variant(e.into()) } + } + impl From<$crate::SecretKeyError> for $error { + fn from(e: $crate::SecretKeyError) -> Self { Self::$variant(e.into()) } + } + impl From<$crate::PublicKeyError> for $error { + fn from(e: $crate::PublicKeyError) -> Self { Self::$variant(e.into()) } + } + impl From<$crate::PublicKeySumError> for $error { + fn from(e: $crate::PublicKeySumError) -> Self { Self::$variant(e.into()) } + } + impl From<$crate::TweakError> for $error { + fn from(e: $crate::TweakError) -> Self { Self::$variant(e.into()) } + } + impl From<$crate::ParityValueError> for $error { + fn from(e: $crate::ParityValueError) -> Self { Self::$variant(e.into()) } + } + impl From<$crate::XOnlyTweakError> for $error { + fn from(e: $crate::XOnlyTweakError) -> Self { Self::$variant(e.into()) } + } + impl From<$crate::SharedSecretError> for $error { + fn from(e: $crate::SharedSecretError) -> Self { Self::$variant(e.into()) } + } + }; +} + /// This is a general purpose error type that can be used to wrap all the errors in this crate. /// /// Every error types in this crate can be converted (using `?`) to this type. We also support