|
20 | 20 | extern crate libc; |
21 | 21 |
|
22 | 22 | use libc::{c_int,c_uchar, c_uint}; |
| 23 | +use core::fmt; |
23 | 24 |
|
| 25 | +/// Errors returned by `libbitcoinconsensus` (see github.com/bitcoin/bitcoin/doc/shared-libraries.md). |
24 | 26 | #[allow(non_camel_case_types)] |
25 | 27 | #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)] |
26 | 28 | #[repr(C)] |
27 | 29 | pub enum Error { |
| 30 | + /// Default value, passed to `libbitcoinconsensus` as a return parameter. |
28 | 31 | ERR_SCRIPT = 0, |
| 32 | + /// An invalid index for `txTo`. |
29 | 33 | ERR_TX_INDEX, |
| 34 | + /// `txToLen` did not match with the size of `txTo`. |
30 | 35 | ERR_TX_SIZE_MISMATCH, |
| 36 | + /// An error deserializing `txTo`. |
31 | 37 | ERR_TX_DESERIALIZE, |
| 38 | + /// Input amount is required if WITNESS is used. |
32 | 39 | ERR_AMOUNT_REQUIRED, |
| 40 | + /// Script verification `flags` are invalid (i.e. not part of the libconsensus interface). |
33 | 41 | ERR_INVALID_FLAGS |
34 | 42 | } |
35 | 43 |
|
| 44 | +impl fmt::Display for Error { |
| 45 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 46 | + use self::Error::*; |
| 47 | + |
| 48 | + let s = match *self { |
| 49 | + ERR_SCRIPT => "error value was not set (value still 0)", |
| 50 | + ERR_TX_INDEX => "an invalid index for txTo", |
| 51 | + ERR_TX_SIZE_MISMATCH => "txToLen did not match with the size of txTo", |
| 52 | + ERR_TX_DESERIALIZE => "an error deserializing txTo", |
| 53 | + ERR_AMOUNT_REQUIRED => "input amount is required if WITNESS is used", |
| 54 | + ERR_INVALID_FLAGS => "script verification flags are invalid", |
| 55 | + }; |
| 56 | + f.write_str(s) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(feature = "std")] |
| 61 | +impl std::error::Error for Error { |
| 62 | + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 63 | + use self::Error::*; |
| 64 | + |
| 65 | + match *self { |
| 66 | + ERR_SCRIPT |
| 67 | + | ERR_TX_INDEX |
| 68 | + | ERR_TX_SIZE_MISMATCH |
| 69 | + | ERR_TX_DESERIALIZE |
| 70 | + | ERR_AMOUNT_REQUIRED |
| 71 | + | ERR_INVALID_FLAGS => None, |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
36 | 76 | /// Do not enable any verification. |
37 | 77 | pub const VERIFY_NONE : c_uint = 0; |
38 | 78 | /// Evaluate P2SH (BIP16) subscripts. |
|
0 commit comments