Skip to content

Commit 360ed59

Browse files
committed
Introduce error module with single ParseError enum in it.
We will use this error type in the next commit. For now we will continue to use the giant global Error enum, so we add a variant to hold the new `ParseError`. But eventually `ParseError` will become a top-level error and `Error` will go away.
1 parent 179dc87 commit 360ed59

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

src/error.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Written in 2019 by Andrew Poelstra <[email protected]>
2+
// SPDX-License-Identifier: CC0-1.0
3+
4+
//! Errors
5+
6+
use core::fmt;
7+
#[cfg(feature = "std")]
8+
use std::error;
9+
10+
use crate::blanket_traits::StaticDebugAndDisplay;
11+
use crate::Box;
12+
/// An error parsing a Miniscript object (policy, descriptor or miniscript)
13+
/// from a string.
14+
#[derive(Debug)]
15+
pub enum ParseError {
16+
/// Failed to parse a public key or hash.
17+
///
18+
/// Note that the error information is lost for nostd compatibility reasons. See
19+
/// <https://users.rust-lang.org/t/how-to-box-an-error-type-retaining-std-error-only-when-std-is-enabled/>.
20+
FromStr(Box<dyn StaticDebugAndDisplay>),
21+
/// Error parsing a string into an expression tree.
22+
Tree(crate::ParseTreeError),
23+
}
24+
25+
impl ParseError {
26+
/// Boxes a `FromStr` error for a `Pk` (or associated types) into a `ParseError`
27+
pub(crate) fn box_from_str<E: StaticDebugAndDisplay>(e: E) -> Self {
28+
ParseError::FromStr(Box::new(e))
29+
}
30+
}
31+
32+
impl From<crate::ParseTreeError> for ParseError {
33+
fn from(e: crate::ParseTreeError) -> Self { Self::Tree(e) }
34+
}
35+
36+
impl fmt::Display for ParseError {
37+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38+
match self {
39+
ParseError::FromStr(ref e) => e.fmt(f),
40+
ParseError::Tree(ref e) => e.fmt(f),
41+
}
42+
}
43+
}
44+
45+
#[cfg(feature = "std")]
46+
impl error::Error for ParseError {
47+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
48+
match self {
49+
ParseError::FromStr(..) => None,
50+
ParseError::Tree(ref e) => Some(e),
51+
}
52+
}
53+
}

src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ mod pub_macros;
114114
mod benchmarks;
115115
mod blanket_traits;
116116
pub mod descriptor;
117+
mod error;
117118
pub mod expression;
118119
pub mod interpreter;
119120
pub mod iter;
@@ -128,15 +129,14 @@ mod test_utils;
128129
mod util;
129130

130131
use core::{fmt, hash, str};
131-
#[cfg(feature = "std")]
132-
use std::error;
133132

134133
use bitcoin::hashes::{hash160, ripemd160, sha256, Hash};
135134
use bitcoin::hex::DisplayHex;
136135
use bitcoin::{script, Opcode};
137136

138137
pub use crate::blanket_traits::FromStrKey;
139138
pub use crate::descriptor::{DefiniteDescriptorKey, Descriptor, DescriptorPublicKey};
139+
pub use crate::error::ParseError;
140140
pub use crate::expression::{ParseThresholdError, ParseTreeError};
141141
pub use crate::interpreter::Interpreter;
142142
pub use crate::miniscript::analyzable::{AnalysisError, ExtParams};
@@ -494,6 +494,8 @@ pub enum Error {
494494
ParseThreshold(ParseThresholdError),
495495
/// Invalid expression tree.
496496
ParseTree(ParseTreeError),
497+
/// Invalid expression tree.
498+
Parse(ParseError),
497499
}
498500

499501
// https://github.com/sipa/miniscript/pull/5 for discussion on this number
@@ -556,13 +558,14 @@ impl fmt::Display for Error {
556558
Error::Threshold(ref e) => e.fmt(f),
557559
Error::ParseThreshold(ref e) => e.fmt(f),
558560
Error::ParseTree(ref e) => e.fmt(f),
561+
Error::Parse(ref e) => e.fmt(f),
559562
}
560563
}
561564
}
562565

563566
#[cfg(feature = "std")]
564-
impl error::Error for Error {
565-
fn cause(&self) -> Option<&dyn error::Error> {
567+
impl std::error::Error for Error {
568+
fn cause(&self) -> Option<&dyn std::error::Error> {
566569
use self::Error::*;
567570

568571
match self {
@@ -607,6 +610,7 @@ impl error::Error for Error {
607610
Threshold(e) => Some(e),
608611
ParseThreshold(e) => Some(e),
609612
ParseTree(e) => Some(e),
613+
Parse(e) => Some(e),
610614
}
611615
}
612616
}

0 commit comments

Comments
 (0)