Add suffix to IncompleteBuilder error type

By convention we always include the suffix "Error" on our error types.

Rename the error type `IncompleteBuilder` to `IncompleteBuilderError`.
This commit is contained in:
Tobin C. Harding 2023-10-04 13:44:07 +11:00
parent f41416a0ea
commit 2b40ea24fb
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 23 additions and 20 deletions

View File

@ -93,7 +93,7 @@ pub enum Error {
/// Parsing error indicating a taproot error /// Parsing error indicating a taproot error
Taproot(&'static str), Taproot(&'static str),
/// Taproot tree deserilaization error /// Taproot tree deserilaization error
TapTree(crate::taproot::IncompleteBuilder), TapTree(crate::taproot::IncompleteBuilderError),
/// Error related to an xpub key /// Error related to an xpub key
XPubKey(&'static str), XPubKey(&'static str),
/// Error related to PSBT version /// Error related to PSBT version

View File

@ -468,11 +468,11 @@ impl TaprootBuilder {
/// ///
/// # Errors: /// # Errors:
/// ///
/// [`IncompleteBuilder::NotFinalized`] if the builder is not finalized. The builder /// [`IncompleteBuilderError::NotFinalized`] if the builder is not finalized. The builder
/// can be restored by calling [`IncompleteBuilder::into_builder`] /// can be restored by calling [`IncompleteBuilderError::into_builder`]
pub fn try_into_node_info(mut self) -> Result<NodeInfo, IncompleteBuilder> { pub fn try_into_node_info(mut self) -> Result<NodeInfo, IncompleteBuilderError> {
if self.branch().len() != 1 { if self.branch().len() != 1 {
return Err(IncompleteBuilder::NotFinalized(self)); return Err(IncompleteBuilderError::NotFinalized(self));
} }
Ok(self Ok(self
.branch .branch
@ -483,11 +483,11 @@ impl TaprootBuilder {
/// Converts the builder into a [`TapTree`] if the builder is a full tree and /// Converts the builder into a [`TapTree`] if the builder is a full tree and
/// does not contain any hidden nodes /// does not contain any hidden nodes
pub fn try_into_taptree(self) -> Result<TapTree, IncompleteBuilder> { pub fn try_into_taptree(self) -> Result<TapTree, IncompleteBuilderError> {
let node = self.try_into_node_info()?; let node = self.try_into_node_info()?;
if node.has_hidden_nodes { if node.has_hidden_nodes {
// Reconstruct the builder as it was if it has hidden nodes // Reconstruct the builder as it was if it has hidden nodes
return Err(IncompleteBuilder::HiddenParts(TaprootBuilder { return Err(IncompleteBuilderError::HiddenParts(TaprootBuilder {
branch: vec![Some(node)], branch: vec![Some(node)],
})); }));
} }
@ -576,40 +576,43 @@ impl Default for TaprootBuilder {
/// having hidden branches or not being finalized. /// having hidden branches or not being finalized.
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)] #[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive] #[non_exhaustive]
pub enum IncompleteBuilder { pub enum IncompleteBuilderError {
/// Indicates an attempt to construct a tap tree from a builder containing incomplete branches. /// Indicates an attempt to construct a tap tree from a builder containing incomplete branches.
NotFinalized(TaprootBuilder), NotFinalized(TaprootBuilder),
/// Indicates an attempt to construct a tap tree from a builder containing hidden parts. /// Indicates an attempt to construct a tap tree from a builder containing hidden parts.
HiddenParts(TaprootBuilder), HiddenParts(TaprootBuilder),
} }
impl IncompleteBuilder { impl IncompleteBuilderError {
/// Converts error into the original incomplete [`TaprootBuilder`] instance. /// Converts error into the original incomplete [`TaprootBuilder`] instance.
pub fn into_builder(self) -> TaprootBuilder { pub fn into_builder(self) -> TaprootBuilder {
use IncompleteBuilderError::*;
match self { match self {
IncompleteBuilder::NotFinalized(builder) | IncompleteBuilder::HiddenParts(builder) => NotFinalized(builder) | HiddenParts(builder) => builder,
builder,
} }
} }
} }
impl core::fmt::Display for IncompleteBuilder { impl core::fmt::Display for IncompleteBuilderError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use IncompleteBuilderError::*;
f.write_str(match self { f.write_str(match self {
IncompleteBuilder::NotFinalized(_) => NotFinalized(_) =>
"an attempt to construct a tap tree from a builder containing incomplete branches.", "an attempt to construct a tap tree from a builder containing incomplete branches.",
IncompleteBuilder::HiddenParts(_) => HiddenParts(_) =>
"an attempt to construct a tap tree from a builder containing hidden parts.", "an attempt to construct a tap tree from a builder containing hidden parts.",
}) })
} }
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl std::error::Error for IncompleteBuilder { impl std::error::Error for IncompleteBuilderError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use self::IncompleteBuilder::*; use IncompleteBuilderError::*;
match self { match *self {
NotFinalized(_) | HiddenParts(_) => None, NotFinalized(_) | HiddenParts(_) => None,
} }
} }
@ -685,13 +688,13 @@ impl TapTree {
} }
impl TryFrom<TaprootBuilder> for TapTree { impl TryFrom<TaprootBuilder> for TapTree {
type Error = IncompleteBuilder; type Error = IncompleteBuilderError;
/// Constructs [`TapTree`] from a [`TaprootBuilder`] if it is complete binary tree. /// Constructs [`TapTree`] from a [`TaprootBuilder`] if it is complete binary tree.
/// ///
/// # Returns /// # Returns
/// ///
/// A [`TapTree`] iff the `builder` is complete, otherwise return [`IncompleteBuilder`] /// A [`TapTree`] iff the `builder` is complete, otherwise return [`IncompleteBuilderError`]
/// error with the content of incomplete `builder` instance. /// error with the content of incomplete `builder` instance.
fn try_from(builder: TaprootBuilder) -> Result<Self, Self::Error> { builder.try_into_taptree() } fn try_from(builder: TaprootBuilder) -> Result<Self, Self::Error> { builder.try_into_taptree() }
} }
@ -834,7 +837,7 @@ impl NodeInfo {
} }
impl TryFrom<TaprootBuilder> for NodeInfo { impl TryFrom<TaprootBuilder> for NodeInfo {
type Error = IncompleteBuilder; type Error = IncompleteBuilderError;
fn try_from(builder: TaprootBuilder) -> Result<Self, Self::Error> { fn try_from(builder: TaprootBuilder) -> Result<Self, Self::Error> {
builder.try_into_node_info() builder.try_into_node_info()