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.
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.
- Loading branch information
Showing
2 changed files
with
60 additions
and
4 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
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), | ||
} | ||
} | ||
} |
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