bitcoinsv/
result.rs

1use base58::FromBase58Error;
2use bytes::TryGetError;
3use hex::FromHexError;
4use std::fmt::Formatter;
5use std::io;
6use std::string::FromUtf8Error;
7
8/// Standard Result used in the library
9pub type Result<T> = std::result::Result<T, Error>;
10
11/// Standard error type used in the library
12#[derive(Debug)]
13pub enum Error {
14    /// An argument provided is invalid
15    BadArgument(String),
16    /// The data provided is invalid
17    BadData(String),
18    /// The data did not match the checksum.
19    ChecksumMismatch,
20    /// The WIF provided was too long.
21    WifTooLong,
22    /// The blockchain specifier was not recognized.
23    InvalidBlockchainSpecifier,
24    /// Unrecognized Opcode
25    UnrecognizedOpCode,
26    /// The data provided is too small to perform the operation.
27    DataTooSmall,
28    /// The data provided is too large to perform the operation.
29    DataTooLarge,
30    /// Script is too large
31    ScriptTooLarge,
32    /// Script has too many operations
33    ScriptTooManyOps,
34    /// Script stack overflow
35    ScriptStackOverflow,
36    /// Script unbalanced conditional
37    ScriptUnbalancedConditional,
38    /// Script verify failed
39    ScriptVerifyFailed,
40    /// Script OP_RETURN encountered
41    ScriptOpReturn,
42    /// Script invalid stack operation
43    ScriptInvalidStackOperation,
44    /// Script number too large
45    ScriptNumberTooLarge,
46    /// Script disabled opcode
47    ScriptDisabledOpcode,
48    /// Script requires transaction context
49    ScriptRequiresContext,
50    /// Script reserved opcode
51    ScriptReservedOpcode,
52    /// Script unimplemented opcode
53    ScriptUnimplementedOpcode,
54    /// Internal error
55    Internal(String),
56    /// Internal errors
57    InternalError(InternalError),
58    /// Hex string could not be decoded
59    FromHexError(FromHexError),
60    /// Base58 string could not be decoded
61    FromBase58Error(FromBase58Error),
62    /// secp256k1 library error
63    Secp256k1Error(secp256k1::Error),
64    /// Standard library IO error
65    IOError(io::Error),
66    /// String conversion error
67    Utf8Error(FromUtf8Error),
68    /// Error from TryGet
69    TryGet(TryGetError),
70}
71
72impl std::fmt::Display for Error {
73    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
74        match self {
75            Error::BadArgument(s) => f.write_str(&format!("Bad argument: {}", s)),
76            Error::BadData(s) => f.write_str(&format!("Bad data: {}", s)),
77            Error::ChecksumMismatch => f.write_str("Checksum mismatch"),
78            Error::WifTooLong => f.write_str("WIF too long"),
79            Error::InvalidBlockchainSpecifier => f.write_str("Unknown blockchain"),
80            Error::UnrecognizedOpCode => f.write_str("unrecognized opcode"),
81            Error::DataTooSmall => f.write_str("data too small"),
82            Error::DataTooLarge => f.write_str("data too large"),
83            Error::ScriptTooLarge => f.write_str("script too large"),
84            Error::ScriptTooManyOps => f.write_str("script has too many operations"),
85            Error::ScriptStackOverflow => f.write_str("script stack overflow"),
86            Error::ScriptUnbalancedConditional => f.write_str("script unbalanced conditional"),
87            Error::ScriptVerifyFailed => f.write_str("script verify failed"),
88            Error::ScriptOpReturn => f.write_str("script OP_RETURN encountered"),
89            Error::ScriptInvalidStackOperation => f.write_str("script invalid stack operation"),
90            Error::ScriptNumberTooLarge => f.write_str("script number too large"),
91            Error::ScriptDisabledOpcode => f.write_str("script disabled opcode"),
92            Error::ScriptRequiresContext => f.write_str("script requires transaction context"),
93            Error::ScriptReservedOpcode => f.write_str("script reserved opcode"),
94            Error::ScriptUnimplementedOpcode => f.write_str("script unimplemented opcode"),
95            Error::Internal(s) => f.write_str(&format!("Internal error: {}", s)),
96            Error::InternalError(e) => e.fmt(f),
97            Error::FromHexError(e) => f.write_str(&format!("Hex decoding error: {}", e)),
98            Error::FromBase58Error(e) => f.write_str(&format!("Base58 decoding error: {:?}", e)),
99            Error::Secp256k1Error(e) => f.write_str(&format!("secpk256k1 error: {:?}", e)),
100            Error::IOError(e) => f.write_str(&format!("IO error: {}", e)),
101            Error::Utf8Error(e) => f.write_str(&format!("UTF8 error: {}", e)),
102            Error::TryGet(e) => f.write_str(&format!("Tryget error: {}", e)),
103        }
104    }
105}
106
107impl From<InternalError> for Error {
108    fn from(value: InternalError) -> Self {
109        Error::InternalError(value)
110    }
111}
112
113impl From<FromHexError> for Error {
114    fn from(e: FromHexError) -> Self {
115        Error::FromHexError(e)
116    }
117}
118
119impl From<FromBase58Error> for Error {
120    fn from(e: FromBase58Error) -> Self {
121        Error::FromBase58Error(e)
122    }
123}
124
125impl From<io::Error> for Error {
126    fn from(e: io::Error) -> Self {
127        Error::IOError(e)
128    }
129}
130
131impl From<FromUtf8Error> for Error {
132    fn from(e: FromUtf8Error) -> Self {
133        Error::Utf8Error(e)
134    }
135}
136
137impl From<secp256k1::Error> for Error {
138    fn from(e: secp256k1::Error) -> Self {
139        Error::Secp256k1Error(e)
140    }
141}
142
143impl From<TryGetError> for Error {
144    fn from(e: TryGetError) -> Self {
145        Error::TryGet(e)
146    }
147}
148
149/// These are errors that are used internally within the library.
150///
151/// This is needed to enable Clone for minactor.
152#[derive(Debug, Clone)]
153pub enum InternalError {
154    Dummy,
155}
156
157impl std::fmt::Display for InternalError {
158    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
159        use InternalError::*;
160        match self {
161            Dummy => f.write_str("Dummy"),
162        }
163    }
164}