Make TapTree::from_inner return a proper error type

This commit is contained in:
Dr Maxim Orlovsky 2022-04-05 22:24:08 +02:00
parent e24c6e23e3
commit efa800fb1f
No known key found for this signature in database
GPG Key ID: FFC0250947E5C6F7
3 changed files with 42 additions and 8 deletions

View File

@ -24,7 +24,7 @@ mod input;
mod output;
pub use self::input::{Input, PsbtSighashType};
pub use self::output::{Output, TapTree};
pub use self::output::{Output, TapTree, IncompleteTapTree};
/// A trait that describes a PSBT key-value map.
pub(super) trait Map {

View File

@ -79,6 +79,39 @@ pub struct Output {
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
}
/// Error happening when [`TapTree`] is constructed from a [`TaprootBuilder`]
/// having hidden branches or not being finalized.
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub enum IncompleteTapTree {
/// Indicates an attempt to construct a tap tree from a builder containing incomplete branches.
NotFinalized(TaprootBuilder),
/// Indicates an attempt to construct a tap tree from a builder containing hidden parts.
HiddenParts(TaprootBuilder),
}
impl IncompleteTapTree {
/// Converts error into the original incomplete [`TaprootBuilder`] instance.
pub fn into_builder(self) -> TaprootBuilder {
match self {
IncompleteTapTree::NotFinalized(builder) |
IncompleteTapTree::HiddenParts(builder) => builder
}
}
}
impl core::fmt::Display for IncompleteTapTree {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
IncompleteTapTree::NotFinalized(_) => "an attempt to construct a tap tree from a builder containing incomplete branches.",
IncompleteTapTree::HiddenParts(_) => "an attempt to construct a tap tree from a builder containing hidden parts.",
})
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl ::std::error::Error for IncompleteTapTree {}
/// Taproot Tree representing a finalized [`TaprootBuilder`] (a complete binary tree).
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@ -104,13 +137,14 @@ impl TapTree {
/// Converts a [`TaprootBuilder`] into a tree if it is complete binary tree.
///
/// # Return
/// A `TapTree` iff the `inner` builder is complete, otherwise return the inner as `Err`.
pub fn from_inner(inner: TaprootBuilder) -> Result<Self, TaprootBuilder> {
if inner.is_complete() {
Ok(TapTree(inner))
/// # Returns
/// A [`TapTree`] iff the `inner` builder is complete, otherwise return [`IncompleteTapTree`]
/// error with the content of incomplete builder `inner` instance.
pub fn from_inner(inner: TaprootBuilder) -> Result<Self, IncompleteTapTree> {
if !inner.is_complete() {
Err(IncompleteTapTree::NotFinalized(inner))
} else {
Err(inner)
Ok(TapTree(inner))
}
}

View File

@ -41,7 +41,7 @@ mod macros;
pub mod serialize;
mod map;
pub use self::map::{Input, Output, TapTree, PsbtSighashType};
pub use self::map::{Input, Output, TapTree, PsbtSighashType, IncompleteTapTree};
use self::map::Map;
use util::bip32::{ExtendedPubKey, KeySource};