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; mod output;
pub use self::input::{Input, PsbtSighashType}; 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. /// A trait that describes a PSBT key-value map.
pub(super) trait Map { pub(super) trait Map {

View File

@ -79,6 +79,39 @@ pub struct Output {
pub unknown: BTreeMap<raw::Key, Vec<u8>>, 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). /// Taproot Tree representing a finalized [`TaprootBuilder`] (a complete binary tree).
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[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. /// Converts a [`TaprootBuilder`] into a tree if it is complete binary tree.
/// ///
/// # Return /// # Returns
/// A `TapTree` iff the `inner` builder is complete, otherwise return the inner as `Err`. /// A [`TapTree`] iff the `inner` builder is complete, otherwise return [`IncompleteTapTree`]
pub fn from_inner(inner: TaprootBuilder) -> Result<Self, TaprootBuilder> { /// error with the content of incomplete builder `inner` instance.
if inner.is_complete() { pub fn from_inner(inner: TaprootBuilder) -> Result<Self, IncompleteTapTree> {
Ok(TapTree(inner)) if !inner.is_complete() {
Err(IncompleteTapTree::NotFinalized(inner))
} else { } else {
Err(inner) Ok(TapTree(inner))
} }
} }

View File

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