Skip to content

Commit 50cf4f8

Browse files
committed
WIP: Add an exported macro to impl From for all errors
When we use this crate downstream and wish to create a custom error enum with a variant to hold the general purpose error type we need all the `From` impls implemented. Is this too much magic?
1 parent 5126c94 commit 50cf4f8

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/error.rs

+51
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,57 @@ use crate::key::error::{
1313
};
1414
use crate::{schnorr, MessageLengthError};
1515

16+
/// Implements `From<E> for $error` for all the specific errors in this crate.
17+
///
18+
/// Requires `$error` to be a an enum with a variant `Secp256k1`.
19+
#[macro_export]
20+
macro_rules! impl_from_for_all_crate_errors_for {
21+
($error:ty) => {
22+
$crate::impl_from_for_all_crate_errors_for!($error, Secp256k1);
23+
};
24+
($error:ty, $variant:ident) => {
25+
impl From<$crate::Error> for $error {
26+
fn from(e: $crate::Error) -> Self { Self::$variant(e.into()) }
27+
}
28+
impl From<$crate::NotEnoughMemoryError> for $error {
29+
fn from(e: $crate::NotEnoughMemoryError) -> Self { Self::$variant(e.into()) }
30+
}
31+
impl From<$crate::MessageLengthError> for $error {
32+
fn from(e: $crate::MessageLengthError) -> Self { Self::$variant(e.into()) }
33+
}
34+
impl From<$crate::schnorr::SignatureError> for $error {
35+
fn from(e: $crate::schnorr::SignatureError) -> Self { Self::$variant(e.into()) }
36+
}
37+
impl From<$crate::ecdsa::SignatureError> for $error {
38+
fn from(e: $crate::ecdsa::SignatureError) -> Self { Self::$variant(e.into()) }
39+
}
40+
impl From<$crate::RecoveryIdError> for $error {
41+
fn from(e: $crate::RecoveryIdError) -> Self { Self::$variant(e.into()) }
42+
}
43+
impl From<$crate::SecretKeyError> for $error {
44+
fn from(e: $crate::SecretKeyError) -> Self { Self::$variant(e.into()) }
45+
}
46+
impl From<$crate::PublicKeyError> for $error {
47+
fn from(e: $crate::PublicKeyError) -> Self { Self::$variant(e.into()) }
48+
}
49+
impl From<$crate::PublicKeySumError> for $error {
50+
fn from(e: $crate::PublicKeySumError) -> Self { Self::$variant(e.into()) }
51+
}
52+
impl From<$crate::TweakError> for $error {
53+
fn from(e: $crate::TweakError) -> Self { Self::$variant(e.into()) }
54+
}
55+
impl From<$crate::ParityValueError> for $error {
56+
fn from(e: $crate::ParityValueError) -> Self { Self::$variant(e.into()) }
57+
}
58+
impl From<$crate::XOnlyTweakError> for $error {
59+
fn from(e: $crate::XOnlyTweakError) -> Self { Self::$variant(e.into()) }
60+
}
61+
impl From<$crate::SharedSecretError> for $error {
62+
fn from(e: $crate::SharedSecretError) -> Self { Self::$variant(e.into()) }
63+
}
64+
};
65+
}
66+
1667
/// This is a general purpose error type that can be used to wrap all the errors in this crate.
1768
///
1869
/// Every error types in this crate can be converted (using `?`) to this type. We also support

0 commit comments

Comments
 (0)