Skip to content

Commit ee2ebf6

Browse files
committed
feat: Re-export corepc-types
- update the error enum
1 parent 17266ca commit ee2ebf6

File tree

4 files changed

+22
-48
lines changed

4 files changed

+22
-48
lines changed

src/client.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl Client {
148148
/// The deserialized `Block` struct.
149149
pub fn get_block(&self, block_hash: &BlockHash) -> Result<Block, Error> {
150150
self.call::<String>("getblock", &[json!(block_hash), json!(0)])
151-
.and_then(|block_hex| deserialize_hex(&block_hex).map_err(Error::DecodeHex))
151+
.and_then(|blockhash_hex| deserialize_hex(&blockhash_hex).map_err(Error::DecodeHex))
152152
}
153153

154154
/// Retrieves the hash of the tip of the best block chain.
@@ -158,7 +158,7 @@ impl Client {
158158
/// The `BlockHash` of the chain tip.
159159
pub fn get_best_block_hash(&self) -> Result<BlockHash, Error> {
160160
self.call::<String>("getbestblockhash", &[])
161-
.and_then(|block_hex| block_hex.parse().map_err(Error::from))
161+
.and_then(|blockhash_hex| blockhash_hex.parse().map_err(Error::HexToArray))
162162
}
163163

164164
/// Retrieves the number of blocks in the longest chain
@@ -170,7 +170,7 @@ impl Client {
170170
self.call::<GetBlockCount>("getblockcount", &[])?
171171
.0
172172
.try_into()
173-
.map_err(Error::Overflow)
173+
.map_err(Error::TryFromInt)
174174
}
175175

176176
/// Retrieves the block hash at a given height
@@ -184,7 +184,7 @@ impl Client {
184184
/// The `BlockHash` for the given height
185185
pub fn get_block_hash(&self, height: u32) -> Result<BlockHash, Error> {
186186
self.call::<String>("getblockhash", &[json!(height)])
187-
.and_then(|block_hex| block_hex.parse().map_err(Error::from))
187+
.and_then(|blockhash_hex| blockhash_hex.parse().map_err(Error::HexToArray))
188188
}
189189

190190
/// Retrieve the `basic` BIP 157 content filter for a particular block
@@ -199,9 +199,7 @@ impl Client {
199199
pub fn get_block_filter(&self, block_hash: &BlockHash) -> Result<GetBlockFilter, Error> {
200200
let block_filter: v30::GetBlockFilter =
201201
self.call("getblockfilter", &[json!(block_hash)])?;
202-
block_filter
203-
.into_model()
204-
.map_err(Error::GetBlockFilterError)
202+
block_filter.into_model().map_err(Error::GetBlockFilter)
205203
}
206204

207205
/// Retrieves the raw block header for a given block hash.
@@ -265,7 +263,7 @@ impl Client {
265263
self.call("getblockheader", &[json!(block_hash)])?;
266264
header_info
267265
.into_model()
268-
.map_err(Error::GetBlockHeaderVerboseError)
266+
.map_err(Error::GetBlockHeaderVerbose)
269267
}
270268

271269
/// Retrieves the verbose JSON representation of a block (verbosity 1).
@@ -280,9 +278,7 @@ impl Client {
280278
pub fn get_block_verbose(&self, block_hash: &BlockHash) -> Result<GetBlockVerboseOne, Error> {
281279
let block_info: v30::GetBlockVerboseOne =
282280
self.call("getblock", &[json!(block_hash), json!(1)])?;
283-
block_info
284-
.into_model()
285-
.map_err(Error::GetBlockVerboseOneError)
281+
block_info.into_model().map_err(Error::GetBlockVerboseOne)
286282
}
287283
}
288284

src/client/v28.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Client {
2727
self.call("getblockheader", &[json!(block_hash)])?;
2828
header_info
2929
.into_model()
30-
.map_err(Error::GetBlockHeaderVerboseError)
30+
.map_err(Error::GetBlockHeaderVerbose)
3131
}
3232

3333
/// Retrieves the verbose JSON representation of a block (verbosity 1).
@@ -44,6 +44,6 @@ impl Client {
4444
self.call("getblock", &[json!(block_hash), json!(1)])?;
4545
block_info
4646
.into_model()
47-
.map_err(Error::GetBlockVerboseOneError)
47+
.map_err(Error::GetBlockVerboseOne)
4848
}
4949
}

src/error.rs

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! Error types for the Bitcoin RPC client.
22
3-
use bitcoin::{
4-
consensus::encode::FromHexError,
5-
hex::{HexToArrayError, HexToBytesError},
6-
};
3+
use bitcoin::{consensus::encode::FromHexError, hex::HexToArrayError};
74
#[cfg(feature = "28_0")]
85
use corepc_types::v17::{GetBlockHeaderVerboseError, GetBlockVerboseOneError};
96
#[cfg(not(feature = "28_0"))]
@@ -22,13 +19,13 @@ pub enum Error {
2219
DecodeHex(FromHexError),
2320

2421
/// Error converting `GetBlockVersboseOne` type into the model type
25-
GetBlockVerboseOneError(GetBlockVerboseOneError),
22+
GetBlockVerboseOne(GetBlockVerboseOneError),
2623

2724
/// Error modeling [`GetBlockHeaderVerbose`](corepc_types::model::GetBlockHeaderVerbose).
28-
GetBlockHeaderVerboseError(GetBlockHeaderVerboseError),
25+
GetBlockHeaderVerbose(GetBlockHeaderVerboseError),
2926

3027
/// Error modeling [`GetBlockFilter`](corepc_types::model::GetBlockFilter)
31-
GetBlockFilterError(GetBlockFilterError),
28+
GetBlockFilter(GetBlockFilterError),
3229

3330
/// Missing authentication credentials.
3431
MissingAuthentication,
@@ -42,9 +39,6 @@ pub enum Error {
4239
/// JSON-RPC error from the server.
4340
JsonRpc(jsonrpc::Error),
4441

45-
/// Hex decoding error for byte vectors (used in get_block, etc.)
46-
HexToBytes(HexToBytesError),
47-
4842
/// Hash parsing error.
4943
HexToArray(HexToArrayError),
5044

@@ -55,7 +49,7 @@ pub enum Error {
5549
Io(io::Error),
5650

5751
/// Error when converting an integer type to a smaller type due to overflow.
58-
Overflow(TryFromIntError),
52+
TryFromInt(TryFromIntError),
5953
}
6054

6155
impl fmt::Display for Error {
@@ -66,33 +60,15 @@ impl fmt::Display for Error {
6660
}
6761
Error::InvalidCookieFile => write!(f, "invalid cookie file"),
6862
Error::InvalidResponse(e) => write!(f, "invalid response: {e}"),
69-
Error::HexToBytes(e) => write!(f, "Hex to bytes error: {e}"),
7063
Error::HexToArray(e) => write!(f, "Hash parsing eror: {e}"),
7164
Error::JsonRpc(e) => write!(f, "JSON-RPC error: {e}"),
7265
Error::Json(e) => write!(f, "JSON error: {e}"),
7366
Error::Io(e) => write!(f, "I/O error: {e}"),
7467
Error::DecodeHex(e) => write!(f, "Hex deserialization error: {e}"),
75-
Error::GetBlockHeaderVerboseError(e) => write!(f, "{e}"),
76-
Error::GetBlockVerboseOneError(e) => write!(f, "{e}"),
77-
Error::Overflow(e) => write!(f, "Integer conversion overflow error: {e}"),
78-
Error::GetBlockFilterError(e) => write!(f, "{e}"),
79-
}
80-
}
81-
}
82-
83-
impl std::error::Error for Error {
84-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
85-
match self {
86-
Error::JsonRpc(e) => Some(e),
87-
Error::Json(e) => Some(e),
88-
Error::Io(e) => Some(e),
89-
Error::HexToBytes(e) => Some(e),
90-
Error::HexToArray(e) => Some(e),
91-
Error::DecodeHex(e) => Some(e),
92-
Error::GetBlockVerboseOneError(e) => Some(e),
93-
Error::Overflow(e) => Some(e),
94-
Error::GetBlockFilterError(e) => Some(e),
95-
_ => None,
68+
Error::GetBlockHeaderVerbose(e) => write!(f, "{e}"),
69+
Error::GetBlockVerboseOne(e) => write!(f, "{e}"),
70+
Error::TryFromInt(e) => write!(f, "Integer conversion overflow error: {e}"),
71+
Error::GetBlockFilter(e) => write!(f, "{e}"),
9672
}
9773
}
9874
}
@@ -124,13 +100,13 @@ impl From<io::Error> for Error {
124100

125101
impl From<TryFromIntError> for Error {
126102
fn from(e: TryFromIntError) -> Self {
127-
Error::Overflow(e)
103+
Error::TryFromInt(e)
128104
}
129105
}
130106

131107
impl From<GetBlockVerboseOneError> for Error {
132108
fn from(e: GetBlockVerboseOneError) -> Self {
133-
Error::GetBlockVerboseOneError(e)
109+
Error::GetBlockVerboseOne(e)
134110
}
135111
}
136112

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ pub use client::{Auth, Client};
1111
pub use error::{Error, Result};
1212

1313
pub use jsonrpc;
14+
// Re-export corepc_types
15+
pub use corepc_types;

0 commit comments

Comments
 (0)