|
| 1 | +// SPDX-License-Identifier: CC0-1.0 |
| 2 | + |
| 3 | +use core::fmt; |
| 4 | + |
| 5 | +use bitcoin::amount::ParseAmountError; |
| 6 | +use bitcoin::error::UnprefixedHexError; |
| 7 | +use bitcoin::{hex, network}; |
| 8 | + |
| 9 | +use crate::error::write_err; |
| 10 | +use crate::NumericError; |
| 11 | + |
| 12 | +/// Error when converting a `GetBlockchainInfo` type into the model type. |
| 13 | +#[derive(Debug)] |
| 14 | +pub enum GetBlockchainInfoError { |
| 15 | + /// Conversion of numeric type to expected type failed. |
| 16 | + Numeric(NumericError), |
| 17 | + /// Conversion of the `chain` field failed. |
| 18 | + Chain(network::ParseNetworkError), |
| 19 | + /// Conversion of the `best_block_hash` field failed. |
| 20 | + BestBlockHash(hex::HexToArrayError), |
| 21 | + /// Conversion of the `chain_work` field failed. |
| 22 | + ChainWork(UnprefixedHexError), |
| 23 | +} |
| 24 | + |
| 25 | +impl fmt::Display for GetBlockchainInfoError { |
| 26 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 27 | + use GetBlockchainInfoError::*; |
| 28 | + |
| 29 | + match *self { |
| 30 | + Numeric(ref e) => write_err!(f, "numeric"; e), |
| 31 | + Chain(ref e) => write_err!(f, "conversion of the `chain` field failed"; e), |
| 32 | + BestBlockHash(ref e) => { |
| 33 | + write_err!(f, "conversion of the `best_block_hash` field failed"; e) |
| 34 | + } |
| 35 | + ChainWork(ref e) => write_err!(f, "conversion of the `chain_work` field failed"; e), |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +#[cfg(feature = "std")] |
| 41 | +impl std::error::Error for GetBlockchainInfoError { |
| 42 | + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 43 | + use GetBlockchainInfoError::*; |
| 44 | + |
| 45 | + match *self { |
| 46 | + Numeric(ref e) => Some(e), |
| 47 | + Chain(ref e) => Some(e), |
| 48 | + BestBlockHash(ref e) => Some(e), |
| 49 | + ChainWork(ref e) => Some(e), |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl From<NumericError> for GetBlockchainInfoError { |
| 55 | + fn from(e: NumericError) -> Self { Self::Numeric(e) } |
| 56 | +} |
| 57 | + |
| 58 | +/// Error when converting a `MapMempoolEntry` into the model type. |
| 59 | +#[derive(Debug)] |
| 60 | +pub enum MapMempoolEntryError { |
| 61 | + /// Conversion of a `txid` failed. |
| 62 | + Txid(hex::HexToArrayError), |
| 63 | + /// Conversion of a `MempoolEntry` failed. |
| 64 | + MempoolEntry(MempoolEntryError), |
| 65 | +} |
| 66 | + |
| 67 | +impl fmt::Display for MapMempoolEntryError { |
| 68 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 69 | + use MapMempoolEntryError as E; |
| 70 | + |
| 71 | + match *self { |
| 72 | + E::Txid(ref e) => write_err!(f, "conversion of a `txid` failed"; e), |
| 73 | + E::MempoolEntry(ref e) => write_err!(f, "conversion of an `MempoolEntry` failed"; e), |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(feature = "std")] |
| 79 | +impl std::error::Error for MapMempoolEntryError { |
| 80 | + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 81 | + use MapMempoolEntryError as E; |
| 82 | + |
| 83 | + match *self { |
| 84 | + E::Txid(ref e) => Some(e), |
| 85 | + E::MempoolEntry(ref e) => Some(e), |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// Error when converting a `Mem` type into the model type. |
| 91 | +#[derive(Debug)] |
| 92 | +pub enum MempoolEntryError { |
| 93 | + /// Conversion of numeric type to expected type failed. |
| 94 | + Numeric(NumericError), |
| 95 | + /// Conversion of the `wtxid` field failed. |
| 96 | + Wtxid(hex::HexToArrayError), |
| 97 | + /// Conversion of the `MempoolEntryFees` type failed. |
| 98 | + Fees(MempoolEntryFeesError), |
| 99 | + /// Conversion of the `depends` field failed. |
| 100 | + Depends(hex::HexToArrayError), |
| 101 | + /// Conversion of the `spent_by` field failed. |
| 102 | + SpentBy(hex::HexToArrayError), |
| 103 | +} |
| 104 | + |
| 105 | +impl From<NumericError> for MempoolEntryError { |
| 106 | + fn from(e: NumericError) -> Self { Self::Numeric(e) } |
| 107 | +} |
| 108 | + |
| 109 | +impl fmt::Display for MempoolEntryError { |
| 110 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 111 | + use MempoolEntryError as E; |
| 112 | + |
| 113 | + match *self { |
| 114 | + E::Numeric(ref e) => write_err!(f, "numeric"; e), |
| 115 | + E::Wtxid(ref e) => write_err!(f, "conversion of the `wtxid` field failed"; e), |
| 116 | + E::Fees(ref e) => write_err!(f, "conversion of the `fees` field failed"; e), |
| 117 | + E::Depends(ref e) => write_err!(f, "conversion of the `depends` field failed"; e), |
| 118 | + E::SpentBy(ref e) => write_err!(f, "conversion of the `spent_by` field failed"; e), |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +#[cfg(feature = "std")] |
| 124 | +impl std::error::Error for MempoolEntryError { |
| 125 | + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 126 | + use MempoolEntryError as E; |
| 127 | + |
| 128 | + match *self { |
| 129 | + E::Numeric(ref e) => Some(e), |
| 130 | + E::Wtxid(ref e) => Some(e), |
| 131 | + E::Fees(ref e) => Some(e), |
| 132 | + E::Depends(ref e) => Some(e), |
| 133 | + E::SpentBy(ref e) => Some(e), |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/// Error when converting a `MempoolEntryFeesError` type into the model type. |
| 139 | +#[derive(Debug)] |
| 140 | +pub enum MempoolEntryFeesError { |
| 141 | + /// Conversion of the `base` field failed. |
| 142 | + Base(ParseAmountError), |
| 143 | + /// Conversion of the `modified` field failed. |
| 144 | + Modified(ParseAmountError), |
| 145 | + /// Conversion of the `ancestor` field failed. |
| 146 | + MempoolEntry(ParseAmountError), |
| 147 | + /// Conversion of the `descendant` field failed. |
| 148 | + Descendant(ParseAmountError), |
| 149 | +} |
| 150 | + |
| 151 | +impl fmt::Display for MempoolEntryFeesError { |
| 152 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 153 | + use MempoolEntryFeesError as E; |
| 154 | + |
| 155 | + match *self { |
| 156 | + E::Base(ref e) => write_err!(f, "conversion of the `base` field failed"; e), |
| 157 | + E::Modified(ref e) => write_err!(f, "conversion of the `modified` field failed"; e), |
| 158 | + E::MempoolEntry(ref e) => write_err!(f, "conversion of the `ancestor` field failed"; e), |
| 159 | + E::Descendant(ref e) => write_err!(f, "conversion of the `descendant` field failed"; e), |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +#[cfg(feature = "std")] |
| 165 | +impl std::error::Error for MempoolEntryFeesError { |
| 166 | + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 167 | + use MempoolEntryFeesError as E; |
| 168 | + |
| 169 | + match *self { |
| 170 | + E::Base(ref e) => Some(e), |
| 171 | + E::Modified(ref e) => Some(e), |
| 172 | + E::MempoolEntry(ref e) => Some(e), |
| 173 | + E::Descendant(ref e) => Some(e), |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments