Implement std::error::Error::source for MerkleBlockError

The `MerkleBlockError` type does not implement `Display` or
`std::error::Error` - bad rust-bitcoin developers, no biscuit.
This commit is contained in:
Tobin C. Harding 2022-07-28 12:21:17 +10:00
parent 613f1cf2d5
commit e9230019eb
1 changed files with 32 additions and 5 deletions

View File

@ -40,6 +40,8 @@
//! assert_eq!(1, index[0]); //! assert_eq!(1, index[0]);
//! ``` //! ```
use core::fmt;
use crate::prelude::*; use crate::prelude::*;
use crate::io; use crate::io;
@ -53,20 +55,45 @@ use crate::consensus::encode::{self, Decodable, Encodable};
use crate::util::merkleblock::MerkleBlockError::*; use crate::util::merkleblock::MerkleBlockError::*;
use crate::{Block, BlockHeader}; use crate::{Block, BlockHeader};
/// An error when verifying the merkle block /// An error when verifying the merkle block.
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive] #[non_exhaustive]
pub enum MerkleBlockError { pub enum MerkleBlockError {
/// When header merkle root don't match to the root calculated from the partial merkle tree /// Merkle root in the header doesn't match to the root calculated from partial merkle tree.
MerkleRootMismatch, MerkleRootMismatch,
/// When partial merkle tree contains no transactions /// Partial merkle tree contains no transactions.
NoTransactions, NoTransactions,
/// When there are too many transactions /// There are too many transactions.
TooManyTransactions, TooManyTransactions,
/// General format error /// General format error.
BadFormat(String), BadFormat(String),
} }
impl fmt::Display for MerkleBlockError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::MerkleBlockError::*;
match *self {
MerkleRootMismatch => write!(f, "merkle header root doesn't match to the root calculated from the partial merkle tree"),
NoTransactions => write!(f, "partial merkle tree contains no transactions"),
TooManyTransactions => write!(f, "too many transactions"),
BadFormat(ref s) => write!(f, "general format error: {}", s),
}
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for MerkleBlockError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use self::MerkleBlockError::*;
match *self {
MerkleRootMismatch | NoTransactions | TooManyTransactions | BadFormat(_) => None,
}
}
}
/// Data structure that represents a partial merkle tree. /// Data structure that represents a partial merkle tree.
/// ///
/// It represents a subset of the txid's of a known block, in a way that /// It represents a subset of the txid's of a known block, in a way that