Skip to content

Commit

Permalink
Introduce error module with single ParseError enum in it.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apoelstra committed Nov 13, 2024
1 parent c8aa7d2 commit 7f2fc9b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
52 changes: 52 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Written in 2019 by Andrew Poelstra <[email protected]>
// SPDX-License-Identifier: CC0-1.0

//! Errors
use core::fmt;
#[cfg(feature = "std")]
use std::error;

use crate::blanket_traits::StaticDebugAndDisplay;
/// An error parsing a Miniscript object (policy, descriptor or miniscript)
/// from a string.
#[derive(Debug)]
pub enum ParseError {
/// Failed to parse a public key or hash.
///
/// Note that the error information is lost for nostd compatibility reasons. See
/// <https://users.rust-lang.org/t/how-to-box-an-error-type-retaining-std-error-only-when-std-is-enabled/>.
FromStr(Box<dyn StaticDebugAndDisplay>),
/// Error parsing a string into an expression tree.
Tree(crate::ParseTreeError),
}

impl ParseError {
/// Boxes a `FromStr` error for a `Pk` (or associated types) into a `ParseError`
pub(crate) fn box_from_str<E: StaticDebugAndDisplay>(e: E) -> Self {
ParseError::FromStr(Box::new(e))
}
}

impl From<crate::ParseTreeError> for ParseError {
fn from(e: crate::ParseTreeError) -> Self { Self::Tree(e) }
}

impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ParseError::FromStr(ref e) => e.fmt(f),
ParseError::Tree(ref e) => e.fmt(f),
}
}
}

#[cfg(feature = "std")]
impl error::Error for ParseError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
ParseError::FromStr(..) => None,
ParseError::Tree(ref e) => Some(e),
}
}
}
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ mod pub_macros;
mod benchmarks;
mod blanket_traits;
pub mod descriptor;
mod error;
pub mod expression;
pub mod interpreter;
pub mod iter;
Expand All @@ -128,15 +129,14 @@ mod test_utils;
mod util;

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

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

pub use crate::blanket_traits::FromStrKey;
pub use crate::descriptor::{DefiniteDescriptorKey, Descriptor, DescriptorPublicKey};
pub use crate::error::ParseError;
pub use crate::expression::{ParseThresholdError, ParseTreeError};
pub use crate::interpreter::Interpreter;
pub use crate::miniscript::analyzable::{AnalysisError, ExtParams};
Expand Down Expand Up @@ -494,6 +494,8 @@ pub enum Error {
ParseThreshold(ParseThresholdError),
/// Invalid expression tree.
ParseTree(ParseTreeError),
/// Invalid expression tree.
Parse(ParseError),
}

// https://github.com/sipa/miniscript/pull/5 for discussion on this number
Expand Down Expand Up @@ -556,13 +558,14 @@ impl fmt::Display for Error {
Error::Threshold(ref e) => e.fmt(f),
Error::ParseThreshold(ref e) => e.fmt(f),
Error::ParseTree(ref e) => e.fmt(f),
Error::Parse(ref e) => e.fmt(f),
}
}
}

#[cfg(feature = "std")]
impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn std::error::Error> {
use self::Error::*;

match self {
Expand Down Expand Up @@ -607,6 +610,7 @@ impl error::Error for Error {
Threshold(e) => Some(e),
ParseThreshold(e) => Some(e),
ParseTree(e) => Some(e),
Parse(e) => Some(e),
}
}
}
Expand Down

0 comments on commit 7f2fc9b

Please sign in to comment.