Skip to content

Commit 5fea2b3

Browse files
committed
miniscript: rename and cleanup decode::KeyParseError
The word "parse" is redundant at best, and wrong at worst (we use "decode" when converting a script and "parse" when converting a string). The variants were also overly verbose and the formatting text lost information. Clean all this up.
1 parent a57fab3 commit 5fea2b3

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ pub enum Error {
481481
/// Bare descriptors don't have any addresses
482482
BareDescriptorAddr,
483483
/// PubKey invalid under current context
484-
PubKeyCtxError(miniscript::decode::KeyParseError, &'static str),
484+
PubKeyCtxError(miniscript::decode::KeyError, &'static str),
485485
/// No script code for Tr descriptors
486486
TrNoScriptCode,
487487
/// At least two BIP389 key expressions in the descriptor contain tuples of

src/miniscript/decode.rs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,18 @@ use crate::{
2727
/// Trait for parsing keys from byte slices
2828
pub trait ParseableKey: Sized + ToPublicKey + private::Sealed {
2929
/// Parse a key from slice
30-
fn from_slice(sl: &[u8]) -> Result<Self, KeyParseError>;
30+
fn from_slice(sl: &[u8]) -> Result<Self, KeyError>;
3131
}
3232

3333
impl ParseableKey for bitcoin::PublicKey {
34-
fn from_slice(sl: &[u8]) -> Result<Self, KeyParseError> {
35-
bitcoin::PublicKey::from_slice(sl).map_err(KeyParseError::FullKeyParseError)
34+
fn from_slice(sl: &[u8]) -> Result<Self, KeyError> {
35+
bitcoin::PublicKey::from_slice(sl).map_err(KeyError::Full)
3636
}
3737
}
3838

3939
impl ParseableKey for bitcoin::secp256k1::XOnlyPublicKey {
40-
fn from_slice(sl: &[u8]) -> Result<Self, KeyParseError> {
41-
bitcoin::secp256k1::XOnlyPublicKey::from_slice(sl)
42-
.map_err(KeyParseError::XonlyKeyParseError)
43-
}
44-
}
45-
46-
/// Decoding error while parsing keys
47-
#[derive(Debug, Clone, PartialEq, Eq)]
48-
pub enum KeyParseError {
49-
/// Bitcoin PublicKey parse error
50-
FullKeyParseError(bitcoin::key::FromSliceError),
51-
/// Xonly key parse Error
52-
XonlyKeyParseError(bitcoin::secp256k1::Error),
53-
}
54-
55-
impl fmt::Display for KeyParseError {
56-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57-
match self {
58-
KeyParseError::FullKeyParseError(_e) => write!(f, "FullKey Parse Error"),
59-
KeyParseError::XonlyKeyParseError(_e) => write!(f, "XonlyKey Parse Error"),
60-
}
61-
}
62-
}
63-
64-
#[cfg(feature = "std")]
65-
impl error::Error for KeyParseError {
66-
fn cause(&self) -> Option<&(dyn error::Error + 'static)> {
67-
match self {
68-
KeyParseError::FullKeyParseError(e) => Some(e),
69-
KeyParseError::XonlyKeyParseError(e) => Some(e),
70-
}
40+
fn from_slice(sl: &[u8]) -> Result<Self, KeyError> {
41+
bitcoin::secp256k1::XOnlyPublicKey::from_slice(sl).map_err(KeyError::XOnly)
7142
}
7243
}
7344

@@ -727,3 +698,31 @@ fn is_and_v(tokens: &mut TokenIter) -> bool {
727698
| Some(&Tk::Swap)
728699
)
729700
}
701+
702+
/// Decoding error while parsing keys
703+
#[derive(Debug, Clone, PartialEq, Eq)]
704+
pub enum KeyError {
705+
/// Bitcoin PublicKey parse error
706+
Full(bitcoin::key::FromSliceError),
707+
/// Xonly key parse Error
708+
XOnly(bitcoin::secp256k1::Error),
709+
}
710+
711+
impl fmt::Display for KeyError {
712+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
713+
match self {
714+
Self::Full(e) => e.fmt(f),
715+
Self::XOnly(e) => e.fmt(f),
716+
}
717+
}
718+
}
719+
720+
#[cfg(feature = "std")]
721+
impl error::Error for KeyError {
722+
fn cause(&self) -> Option<&(dyn error::Error + 'static)> {
723+
match self {
724+
Self::Full(e) => Some(e),
725+
Self::XOnly(e) => Some(e),
726+
}
727+
}
728+
}

0 commit comments

Comments
 (0)