Skip to content

Commit a5db0f0

Browse files
committed
Re-order error code blocks
Audit the whole codebase and re-order code around error handling so that it is all in a uniform layout in the file, specifically 1. Error declaration 2. impl Display ... 3. impl error::Error ... 4. All the From impls Refactor only, no logic changes. All changes are just moving blocks of code around.
1 parent 2eac9de commit a5db0f0

File tree

8 files changed

+206
-206
lines changed

8 files changed

+206
-206
lines changed

src/interpreter/error.rs

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -118,78 +118,6 @@ pub enum Error {
118118
VerifyFailed,
119119
}
120120

121-
/// A type of representing which keys errored during interpreter checksig evaluation
122-
// Note that we can't use BitcoinKey because it is not public
123-
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
124-
pub enum PkEvalErrInner {
125-
/// Full Key
126-
FullKey(bitcoin::PublicKey),
127-
/// XOnly Key
128-
XOnlyKey(bitcoin::XOnlyPublicKey),
129-
}
130-
131-
impl From<BitcoinKey> for PkEvalErrInner {
132-
fn from(pk: BitcoinKey) -> Self {
133-
match pk {
134-
BitcoinKey::Fullkey(pk) => PkEvalErrInner::FullKey(pk),
135-
BitcoinKey::XOnlyPublicKey(xpk) => PkEvalErrInner::XOnlyKey(xpk),
136-
}
137-
}
138-
}
139-
140-
impl fmt::Display for PkEvalErrInner {
141-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142-
match self {
143-
PkEvalErrInner::FullKey(pk) => pk.fmt(f),
144-
PkEvalErrInner::XOnlyKey(xpk) => xpk.fmt(f),
145-
}
146-
}
147-
}
148-
149-
#[doc(hidden)]
150-
impl From<secp256k1::Error> for Error {
151-
fn from(e: secp256k1::Error) -> Error {
152-
Error::Secp(e)
153-
}
154-
}
155-
156-
#[doc(hidden)]
157-
impl From<bitcoin::util::sighash::Error> for Error {
158-
fn from(e: bitcoin::util::sighash::Error) -> Error {
159-
Error::SighashError(e)
160-
}
161-
}
162-
163-
#[doc(hidden)]
164-
impl From<bitcoin::EcdsaSigError> for Error {
165-
fn from(e: bitcoin::EcdsaSigError) -> Error {
166-
Error::EcdsaSig(e)
167-
}
168-
}
169-
170-
#[doc(hidden)]
171-
impl From<bitcoin::SchnorrSigError> for Error {
172-
fn from(e: bitcoin::SchnorrSigError) -> Error {
173-
Error::SchnorrSig(e)
174-
}
175-
}
176-
177-
#[doc(hidden)]
178-
impl From<crate::Error> for Error {
179-
fn from(e: crate::Error) -> Error {
180-
Error::Miniscript(e)
181-
}
182-
}
183-
184-
impl error::Error for Error {
185-
fn cause(&self) -> Option<&dyn error::Error> {
186-
match *self {
187-
Error::Secp(ref err) => Some(err),
188-
ref x => Some(x),
189-
}
190-
}
191-
}
192-
193121
impl fmt::Display for Error {
194122
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
195123
match *self {
@@ -263,3 +191,75 @@ impl fmt::Display for Error {
263191
}
264192
}
265193
}
194+
195+
impl error::Error for Error {
196+
fn cause(&self) -> Option<&dyn error::Error> {
197+
match *self {
198+
Error::Secp(ref err) => Some(err),
199+
ref x => Some(x),
200+
}
201+
}
202+
}
203+
204+
#[doc(hidden)]
205+
impl From<secp256k1::Error> for Error {
206+
fn from(e: secp256k1::Error) -> Error {
207+
Error::Secp(e)
208+
}
209+
}
210+
211+
#[doc(hidden)]
212+
impl From<bitcoin::util::sighash::Error> for Error {
213+
fn from(e: bitcoin::util::sighash::Error) -> Error {
214+
Error::SighashError(e)
215+
}
216+
}
217+
218+
#[doc(hidden)]
219+
impl From<bitcoin::EcdsaSigError> for Error {
220+
fn from(e: bitcoin::EcdsaSigError) -> Error {
221+
Error::EcdsaSig(e)
222+
}
223+
}
224+
225+
#[doc(hidden)]
226+
impl From<bitcoin::SchnorrSigError> for Error {
227+
fn from(e: bitcoin::SchnorrSigError) -> Error {
228+
Error::SchnorrSig(e)
229+
}
230+
}
231+
232+
#[doc(hidden)]
233+
impl From<crate::Error> for Error {
234+
fn from(e: crate::Error) -> Error {
235+
Error::Miniscript(e)
236+
}
237+
}
238+
239+
/// A type of representing which keys errored during interpreter checksig evaluation
240+
// Note that we can't use BitcoinKey because it is not public
241+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
242+
pub enum PkEvalErrInner {
243+
/// Full Key
244+
FullKey(bitcoin::PublicKey),
245+
/// XOnly Key
246+
XOnlyKey(bitcoin::XOnlyPublicKey),
247+
}
248+
249+
impl From<BitcoinKey> for PkEvalErrInner {
250+
fn from(pk: BitcoinKey) -> Self {
251+
match pk {
252+
BitcoinKey::Fullkey(pk) => PkEvalErrInner::FullKey(pk),
253+
BitcoinKey::XOnlyPublicKey(xpk) => PkEvalErrInner::XOnlyKey(xpk),
254+
}
255+
}
256+
}
257+
258+
impl fmt::Display for PkEvalErrInner {
259+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
260+
match self {
261+
PkEvalErrInner::FullKey(pk) => pk.fmt(f),
262+
PkEvalErrInner::XOnlyKey(xpk) => xpk.fmt(f),
263+
}
264+
}
265+
}

src/lib.rs

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -575,65 +575,6 @@ pub enum Error {
575575
TrNoExplicitScript,
576576
}
577577

578-
#[doc(hidden)]
579-
impl<Pk, Ctx> From<miniscript::types::Error<Pk, Ctx>> for Error
580-
where
581-
Pk: MiniscriptKey,
582-
Ctx: ScriptContext,
583-
{
584-
fn from(e: miniscript::types::Error<Pk, Ctx>) -> Error {
585-
Error::TypeCheck(e.to_string())
586-
}
587-
}
588-
589-
#[doc(hidden)]
590-
impl From<policy::LiftError> for Error {
591-
fn from(e: policy::LiftError) -> Error {
592-
Error::LiftError(e)
593-
}
594-
}
595-
596-
#[doc(hidden)]
597-
impl From<miniscript::context::ScriptContextError> for Error {
598-
fn from(e: miniscript::context::ScriptContextError) -> Error {
599-
Error::ContextError(e)
600-
}
601-
}
602-
603-
#[doc(hidden)]
604-
impl From<miniscript::analyzable::AnalysisError> for Error {
605-
fn from(e: miniscript::analyzable::AnalysisError) -> Error {
606-
Error::AnalysisError(e)
607-
}
608-
}
609-
610-
#[doc(hidden)]
611-
impl From<bitcoin::secp256k1::Error> for Error {
612-
fn from(e: bitcoin::secp256k1::Error) -> Error {
613-
Error::Secp(e)
614-
}
615-
}
616-
617-
#[doc(hidden)]
618-
impl From<bitcoin::util::address::Error> for Error {
619-
fn from(e: bitcoin::util::address::Error) -> Error {
620-
Error::AddrError(e)
621-
}
622-
}
623-
624-
fn errstr(s: &str) -> Error {
625-
Error::Unexpected(s.to_owned())
626-
}
627-
628-
impl error::Error for Error {
629-
fn cause(&self) -> Option<&dyn error::Error> {
630-
match *self {
631-
Error::BadPubkey(ref e) => Some(e),
632-
_ => None,
633-
}
634-
}
635-
}
636-
637578
// https://github.com/sipa/miniscript/pull/5 for discussion on this number
638579
const MAX_RECURSION_DEPTH: u32 = 402;
639580
// https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki
@@ -717,6 +658,61 @@ impl fmt::Display for Error {
717658
}
718659
}
719660

661+
impl error::Error for Error {
662+
fn cause(&self) -> Option<&dyn error::Error> {
663+
match *self {
664+
Error::BadPubkey(ref e) => Some(e),
665+
_ => None,
666+
}
667+
}
668+
}
669+
670+
#[doc(hidden)]
671+
impl<Pk, Ctx> From<miniscript::types::Error<Pk, Ctx>> for Error
672+
where
673+
Pk: MiniscriptKey,
674+
Ctx: ScriptContext,
675+
{
676+
fn from(e: miniscript::types::Error<Pk, Ctx>) -> Error {
677+
Error::TypeCheck(e.to_string())
678+
}
679+
}
680+
681+
#[doc(hidden)]
682+
impl From<policy::LiftError> for Error {
683+
fn from(e: policy::LiftError) -> Error {
684+
Error::LiftError(e)
685+
}
686+
}
687+
688+
#[doc(hidden)]
689+
impl From<miniscript::context::ScriptContextError> for Error {
690+
fn from(e: miniscript::context::ScriptContextError) -> Error {
691+
Error::ContextError(e)
692+
}
693+
}
694+
695+
#[doc(hidden)]
696+
impl From<miniscript::analyzable::AnalysisError> for Error {
697+
fn from(e: miniscript::analyzable::AnalysisError) -> Error {
698+
Error::AnalysisError(e)
699+
}
700+
}
701+
702+
#[doc(hidden)]
703+
impl From<bitcoin::secp256k1::Error> for Error {
704+
fn from(e: bitcoin::secp256k1::Error) -> Error {
705+
Error::Secp(e)
706+
}
707+
}
708+
709+
#[doc(hidden)]
710+
impl From<bitcoin::util::address::Error> for Error {
711+
fn from(e: bitcoin::util::address::Error) -> Error {
712+
Error::AddrError(e)
713+
}
714+
}
715+
720716
#[doc(hidden)]
721717
#[cfg(feature = "compiler")]
722718
impl From<crate::policy::compiler::CompilerError> for Error {
@@ -732,6 +728,10 @@ impl From<policy::concrete::PolicyError> for Error {
732728
}
733729
}
734730

731+
fn errstr(s: &str) -> Error {
732+
Error::Unexpected(s.to_owned())
733+
}
734+
735735
/// The size of an encoding of a number in Script
736736
pub fn script_num_size(n: usize) -> usize {
737737
match n {

src/miniscript/decode.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,6 @@ pub trait ParseableKey: Sized + ToPublicKey + private::Sealed {
4141
fn from_slice(sl: &[u8]) -> Result<Self, KeyParseError>;
4242
}
4343

44-
/// Decoding error while parsing keys
45-
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
46-
pub enum KeyParseError {
47-
/// Bitcoin PublicKey parse error
48-
FullKeyParseError(bitcoin::util::key::Error),
49-
/// Xonly key parse Error
50-
XonlyKeyParseError(bitcoin::secp256k1::Error),
51-
}
52-
5344
impl ParseableKey for bitcoin::PublicKey {
5445
fn from_slice(sl: &[u8]) -> Result<Self, KeyParseError> {
5546
bitcoin::PublicKey::from_slice(sl).map_err(KeyParseError::FullKeyParseError)
@@ -63,13 +54,13 @@ impl ParseableKey for bitcoin::secp256k1::XOnlyPublicKey {
6354
}
6455
}
6556

66-
impl error::Error for KeyParseError {
67-
fn cause(&self) -> Option<&(dyn error::Error + 'static)> {
68-
match self {
69-
KeyParseError::FullKeyParseError(e) => Some(e),
70-
KeyParseError::XonlyKeyParseError(e) => Some(e),
71-
}
72-
}
57+
/// Decoding error while parsing keys
58+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
59+
pub enum KeyParseError {
60+
/// Bitcoin PublicKey parse error
61+
FullKeyParseError(bitcoin::util::key::Error),
62+
/// Xonly key parse Error
63+
XonlyKeyParseError(bitcoin::secp256k1::Error),
7364
}
7465

7566
impl fmt::Display for KeyParseError {
@@ -81,6 +72,15 @@ impl fmt::Display for KeyParseError {
8172
}
8273
}
8374

75+
impl error::Error for KeyParseError {
76+
fn cause(&self) -> Option<&(dyn error::Error + 'static)> {
77+
match self {
78+
KeyParseError::FullKeyParseError(e) => Some(e),
79+
KeyParseError::XonlyKeyParseError(e) => Some(e),
80+
}
81+
}
82+
}
83+
8484
/// Private Mod to prevent downstream from implementing this public trait
8585
mod private {
8686

src/miniscript/types/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@ pub struct Error<Pk: MiniscriptKey, Ctx: ScriptContext> {
104104
pub error: ErrorKind,
105105
}
106106

107-
impl<Pk: MiniscriptKey, Ctx: ScriptContext> error::Error for Error<Pk, Ctx> {
108-
fn cause(&self) -> Option<&dyn error::Error> {
109-
None
110-
}
111-
}
112-
113107
impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Display for Error<Pk, Ctx> {
114108
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
115109
match self.error {
@@ -219,6 +213,12 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Display for Error<Pk, Ctx> {
219213
}
220214
}
221215

216+
impl<Pk: MiniscriptKey, Ctx: ScriptContext> error::Error for Error<Pk, Ctx> {
217+
fn cause(&self) -> Option<&dyn error::Error> {
218+
None
219+
}
220+
}
221+
222222
/// Structure representing the type of a Miniscript fragment, including all
223223
/// properties relevant to the main codebase
224224
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]

src/policy/compiler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ pub enum CompilerError {
6262
PolicyError(policy::concrete::PolicyError),
6363
}
6464

65-
impl error::Error for CompilerError {}
66-
6765
impl fmt::Display for CompilerError {
6866
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6967
match *self {
@@ -81,6 +79,8 @@ impl fmt::Display for CompilerError {
8179
}
8280
}
8381

82+
impl error::Error for CompilerError {}
83+
8484
#[doc(hidden)]
8585
impl From<policy::concrete::PolicyError> for CompilerError {
8686
fn from(e: policy::concrete::PolicyError) -> CompilerError {

0 commit comments

Comments
 (0)