forked from rust-bitcoin/rust-miniscript
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expression: unify Taproot and non-Taproot parsing
The `expression::Tree` type now parses expression trees containing both {} and () as children. It marks which is which, and it is the responsibility of FromTree implementors to make sure that the correct style is used. This eliminates a ton of ad-hoc string parsing code from descriptor/tr.rs, including 8 instances of Error::Unexpected. It is also the first change that preserves (sorta) a pubkey parsing error rather than converting it to a string. Because of rust-bitcoin#734 this inserts a call to `Descriptor::sanity_check` when parsing a string, specifically for Taproot descriptors. This is weird and wrong but we want to address it separately from this PR, whose goal is to preserve all behavior. This also introduces the new `error` module and `ParseError` enum, which we will move all string-parsing errors into. For now we just stick it in the big global enum, but eventually it will live on its own.
- Loading branch information
Showing
5 changed files
with
214 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} |
Oops, something went wrong.