From 99f565f932008c715bd95eb15f70d099ce09bccb Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 31 May 2022 14:29:50 +1000 Subject: [PATCH] Add non_exhaustive to all error enums Adding an error variant to a public enum is an API breaking change, this means making what could be small refactorings or improvements harder. If we use `non_exhaustive` for error types then we mitigate this cost. There is a tradeoff however, downstream users who explicitly match on our public error types must include a wildcard pattern. --- src/blockdata/block.rs | 1 + src/blockdata/script.rs | 1 + src/blockdata/transaction.rs | 1 + src/consensus/encode.rs | 1 + src/network/mod.rs | 1 + src/util/address.rs | 1 + src/util/amount.rs | 1 + src/util/base58.rs | 1 + src/util/bip32.rs | 1 + src/util/ecdsa.rs | 1 + src/util/key.rs | 1 + src/util/misc.rs | 1 + src/util/mod.rs | 1 + src/util/psbt/error.rs | 1 + src/util/psbt/map/output.rs | 1 + src/util/psbt/mod.rs | 1 + src/util/schnorr.rs | 1 + src/util/sighash.rs | 1 + src/util/taproot.rs | 2 ++ 19 files changed, 20 insertions(+) diff --git a/src/blockdata/block.rs b/src/blockdata/block.rs index 06a532981f..d41c1aabfb 100644 --- a/src/blockdata/block.rs +++ b/src/blockdata/block.rs @@ -333,6 +333,7 @@ impl Block { /// An error when looking up a BIP34 block height. #[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] pub enum Bip34Error { /// The block does not support BIP34 yet. Unsupported, diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index 839a7c2923..af58864c82 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -137,6 +137,7 @@ where /// much as it could be; patches welcome if more detailed errors /// would help you. #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)] +#[non_exhaustive] pub enum Error { /// Something did a non-minimal push; for more information see /// `https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#Push_operators` diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index c35a1b65bd..f6b40a76ce 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -114,6 +114,7 @@ impl fmt::Display for OutPoint { /// An error in parsing an OutPoint. #[derive(Clone, PartialEq, Eq, Debug)] +#[non_exhaustive] pub enum ParseOutPointError { /// Error in TXID part. Txid(hashes::hex::Error), diff --git a/src/consensus/encode.rs b/src/consensus/encode.rs index 6505df37db..d4a14579c5 100644 --- a/src/consensus/encode.rs +++ b/src/consensus/encode.rs @@ -47,6 +47,7 @@ use crate::network::{message_blockdata::Inventory, address::{Address, AddrV2Mess /// Encoding error #[derive(Debug)] +#[non_exhaustive] pub enum Error { /// And I/O error Io(io::Error), diff --git a/src/network/mod.rs b/src/network/mod.rs index 2686e0ef43..fffab8b635 100644 --- a/src/network/mod.rs +++ b/src/network/mod.rs @@ -50,6 +50,7 @@ pub mod stream_reader; /// Network error #[derive(Debug)] +#[non_exhaustive] pub enum Error { /// And I/O error Io(io::Error), diff --git a/src/util/address.rs b/src/util/address.rs index afa4c71468..7605f7cad3 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -53,6 +53,7 @@ use crate::util::schnorr::{TapTweak, UntweakedPublicKey, TweakedPublicKey}; /// Address error. #[derive(Debug, PartialEq, Eq, Clone)] +#[non_exhaustive] pub enum Error { /// Base58 encoding error. Base58(base58::Error), diff --git a/src/util/amount.rs b/src/util/amount.rs index 18f2e72d46..afe58736f4 100644 --- a/src/util/amount.rs +++ b/src/util/amount.rs @@ -148,6 +148,7 @@ fn denomination_from_str(mut s: &str) -> Option { /// An error during amount parsing. #[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] pub enum ParseAmountError { /// Amount is negative. Negative, diff --git a/src/util/base58.rs b/src/util/base58.rs index 70e8c5d4e9..a20912182e 100644 --- a/src/util/base58.rs +++ b/src/util/base58.rs @@ -29,6 +29,7 @@ use crate::util::{endian, key}; /// An error that might occur during base58 decoding #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)] +#[non_exhaustive] pub enum Error { /// Invalid character encountered BadByte(u8), diff --git a/src/util/bip32.rs b/src/util/bip32.rs index 0bc23c6474..965d2bdb4f 100644 --- a/src/util/bip32.rs +++ b/src/util/bip32.rs @@ -457,6 +457,7 @@ pub type KeySource = (Fingerprint, DerivationPath); /// A BIP32 error #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[non_exhaustive] pub enum Error { /// A pk->pk derivation was attempted on a hardened key CannotDeriveFromHardenedKey, diff --git a/src/util/ecdsa.rs b/src/util/ecdsa.rs index 185b22f672..659453a328 100644 --- a/src/util/ecdsa.rs +++ b/src/util/ecdsa.rs @@ -87,6 +87,7 @@ impl FromStr for EcdsaSig { /// A key-related error. #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[non_exhaustive] pub enum EcdsaSigError { /// Hex encoding error HexEncoding(hex::Error), diff --git a/src/util/key.rs b/src/util/key.rs index aab5e6171c..5d43b1e826 100644 --- a/src/util/key.rs +++ b/src/util/key.rs @@ -31,6 +31,7 @@ use crate::util::base58; /// A key-related error. #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[non_exhaustive] pub enum Error { /// Base58 encoding error Base58(base58::Error), diff --git a/src/util/misc.rs b/src/util/misc.rs index 9f61f5630c..49636c308d 100644 --- a/src/util/misc.rs +++ b/src/util/misc.rs @@ -48,6 +48,7 @@ mod message_signing { /// An error used for dealing with Bitcoin Signed Messages. #[cfg_attr(docsrs, doc(cfg(feature = "secp-recovery")))] #[derive(Debug, PartialEq, Eq)] + #[non_exhaustive] pub enum MessageSignatureError { /// Signature is expected to be 65 bytes. InvalidLength, diff --git a/src/util/mod.rs b/src/util/mod.rs index 2d3d8a2334..3f9f192edb 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -67,6 +67,7 @@ pub trait BitArray { /// A general error code, other errors should implement conversions to/from this /// if appropriate. #[derive(Debug)] +#[non_exhaustive] pub enum Error { /// Encoding error Encode(encode::Error), diff --git a/src/util/psbt/error.rs b/src/util/psbt/error.rs index b191e28bff..9a62c87c24 100644 --- a/src/util/psbt/error.rs +++ b/src/util/psbt/error.rs @@ -33,6 +33,7 @@ pub enum PsbtHash { } /// Ways that a Partially Signed Transaction might fail. #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[non_exhaustive] pub enum Error { /// Magic bytes for a PSBT must be the ASCII for "psbt" serialized in most /// significant byte order. diff --git a/src/util/psbt/map/output.rs b/src/util/psbt/map/output.rs index 08ba533d37..1063c83a2e 100644 --- a/src/util/psbt/map/output.rs +++ b/src/util/psbt/map/output.rs @@ -82,6 +82,7 @@ pub struct Output { /// Error happening when [`TapTree`] is constructed from a [`TaprootBuilder`] /// having hidden branches or not being finalized. #[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)] +#[non_exhaustive] pub enum IncompleteTapTree { /// Indicates an attempt to construct a tap tree from a builder containing incomplete branches. NotFinalized(TaprootBuilder), diff --git a/src/util/psbt/mod.rs b/src/util/psbt/mod.rs index 81aeefa3a9..6b595b30d3 100644 --- a/src/util/psbt/mod.rs +++ b/src/util/psbt/mod.rs @@ -223,6 +223,7 @@ mod display_from_str { /// Error encountered during PSBT decoding from Base64 string. #[derive(Debug)] #[cfg_attr(docsrs, doc(cfg(feature = "base64")))] + #[non_exhaustive] pub enum PsbtParseError { /// Error in internal PSBT data structure. PsbtEncoding(Error), diff --git a/src/util/schnorr.rs b/src/util/schnorr.rs index 8d6791209b..c172655a11 100644 --- a/src/util/schnorr.rs +++ b/src/util/schnorr.rs @@ -261,6 +261,7 @@ impl SchnorrSig { /// A schnorr sig related error. #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[non_exhaustive] pub enum SchnorrSigError { /// Base58 encoding error InvalidSighashType(u8), diff --git a/src/util/sighash.rs b/src/util/sighash.rs index 63b1a1470e..5c3f4c1ba1 100644 --- a/src/util/sighash.rs +++ b/src/util/sighash.rs @@ -167,6 +167,7 @@ impl str::FromStr for SchnorrSighashType { /// Possible errors in computing the signature message. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[non_exhaustive] pub enum Error { /// Could happen only by using `*_encode_signing_*` methods with custom writers, engines writers /// like the ones used in methods `*_signature_hash` do not error. diff --git a/src/util/taproot.rs b/src/util/taproot.rs index b1efc1da28..6f4c06f014 100644 --- a/src/util/taproot.rs +++ b/src/util/taproot.rs @@ -969,6 +969,7 @@ impl<'de> ::serde::Deserialize<'de> for LeafVersion { /// Detailed error type for taproot builder. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[non_exhaustive] pub enum TaprootBuilderError { /// Merkle tree depth must not be more than 128. InvalidMerkleTreeDepth(usize), @@ -1030,6 +1031,7 @@ impl std::error::Error for TaprootBuilderError { /// Detailed error type for taproot utilities. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[non_exhaustive] pub enum TaprootError { /// Proof size must be a multiple of 32. InvalidMerkleBranchSize(usize),