Skip to content

Commit d9521fe

Browse files
evanlinjinclaude
andcommitted
refactor!: Move client type aliases into client module
Move async/blocking type aliases from lib.rs into the client module. Delete 4 unused aliases (AsyncEventSender, AsyncRequestReceiver, BlockingRequestReceiver, BlockingEventSender). Re-export only the client structs and error types at the crate root. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0f4c565 commit d9521fe

2 files changed

Lines changed: 52 additions & 75 deletions

File tree

src/client.rs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
11
use crate::pending_request::{PendingRequest, RequestExt};
22
use crate::*;
33

4+
// --- Async client type aliases ---
5+
6+
/// The sending half of the channel used to enqueue one or more requests from [`AsyncClient`].
7+
///
8+
/// These requests are processed and forwarded to [`RequestTracker::track_request`] to be assigned an ID and serialized.
9+
pub type AsyncRequestSender = futures::channel::mpsc::UnboundedSender<RawOneOrMany<PendingRequest>>;
10+
11+
/// The error returned by [`AsyncClient::send_request`] when a request fails.
12+
///
13+
/// This may occur if the server responds with an error, the request is canceled, or the client is shut down.
14+
pub type AsyncRequestError = crate::request::Error<AsyncRequestSendError>;
15+
16+
/// The error that occurs when a request cannot be sent into the async request channel.
17+
///
18+
/// This typically means the client's background task has shut down or the queue is disconnected.
19+
pub type AsyncRequestSendError = futures::channel::mpsc::TrySendError<RawOneOrMany<PendingRequest>>;
20+
21+
/// The receiving half of the internal event stream, returned to users of [`AsyncClient`].
22+
///
23+
/// This yields all incoming [`Event`]s from the Electrum server, including notifications and responses.
24+
pub type AsyncEventReceiver = futures::channel::mpsc::UnboundedReceiver<Event>;
25+
26+
// --- Blocking client type aliases ---
27+
28+
/// Channel sender for sending blocking requests from [`BlockingClient`] to the write thread.
29+
pub type BlockingRequestSender = std::sync::mpsc::Sender<RawOneOrMany<PendingRequest>>;
30+
31+
/// Error returned by [`BlockingClient::send_request`] if the request fails or is canceled.
32+
pub type BlockingRequestError = crate::request::Error<BlockingRequestSendError>;
33+
34+
/// Error that occurs when a blocking request cannot be sent to the internal request channel.
35+
///
36+
/// Typically indicates that the client has been shut down.
37+
pub type BlockingRequestSendError = std::sync::mpsc::SendError<RawOneOrMany<PendingRequest>>;
38+
39+
/// Channel receiver used to receive [`Event`]s from the Electrum server.
40+
pub type BlockingEventReceiver = std::sync::mpsc::Receiver<Event>;
41+
442
/// An asynchronous Electrum client built on the [`futures`] I/O ecosystem.
543
///
644
/// This client allows sending JSON-RPC requests and receiving [`Event`]s from an Electrum server
@@ -21,7 +59,7 @@ use crate::*;
2159
/// [`Event`]: crate::Event
2260
/// [`AsyncBufRead`]: futures::io::AsyncBufRead
2361
/// [`AsyncWrite`]: futures::io::AsyncWrite
24-
/// [`AsyncEventReceiver`]: crate::AsyncEventReceiver
62+
/// [`AsyncEventReceiver`]: crate::client::AsyncEventReceiver
2563
#[derive(Debug, Clone)]
2664
pub struct AsyncClient {
2765
tx: AsyncRequestSender,
@@ -48,7 +86,7 @@ impl AsyncClient {
4886
/// - A `Future`: the client worker loop. This must be polled (e.g., via `tokio::spawn`)
4987
/// to drive the connection.
5088
///
51-
/// [`AsyncEventReceiver`]: crate::AsyncEventReceiver
89+
/// [`AsyncEventReceiver`]: crate::client::AsyncEventReceiver
5290
/// [`Event`]: crate::Event
5391
pub fn new<R, W>(
5492
reader: R,
@@ -116,7 +154,7 @@ impl AsyncClient {
116154
/// - A `Future`: the client worker loop. This must be spawned or polled to keep the client
117155
/// alive.
118156
///
119-
/// [`AsyncEventReceiver`]: crate::AsyncEventReceiver
157+
/// [`AsyncEventReceiver`]: crate::client::AsyncEventReceiver
120158
/// [`Event`]: crate::Event
121159
/// [`AsyncClient::new`]: crate::AsyncClient::new
122160
#[cfg(feature = "tokio")]
@@ -179,7 +217,7 @@ impl AsyncClient {
179217
///
180218
/// [`send_request`]: Self::send_request
181219
/// [`Event`]: crate::Event
182-
/// [`AsyncEventReceiver`]: crate::AsyncEventReceiver
220+
/// [`AsyncEventReceiver`]: crate::client::AsyncEventReceiver
183221
/// [`AsyncRequestSendError`]: crate::AsyncRequestSendError
184222
pub fn send_event_request<Req>(&self, request: Req) -> Result<(), AsyncRequestSendError>
185223
where
@@ -209,7 +247,7 @@ impl AsyncClient {
209247
/// [`BatchRequest`]: crate::BatchRequest
210248
/// [`BatchRequest::request`]: crate::BatchRequest::request
211249
/// [`BatchRequest::event_request`]: crate::BatchRequest::event_request
212-
/// [`AsyncEventReceiver`]: crate::AsyncEventReceiver
250+
/// [`AsyncEventReceiver`]: crate::client::AsyncEventReceiver
213251
pub fn send_batch(&self, batch_req: BatchRequest) -> Result<bool, AsyncRequestSendError> {
214252
match batch_req.into_inner() {
215253
Some(batch) => self.tx.unbounded_send(batch).map(|_| true),
@@ -229,7 +267,7 @@ impl AsyncClient {
229267
/// Use the associated [`BlockingEventReceiver`] to receive [`Event`]s emitted by the server.
230268
///
231269
/// [`Event`]: crate::Event
232-
/// [`BlockingEventReceiver`]: crate::BlockingEventReceiver
270+
/// [`BlockingEventReceiver`]: crate::client::BlockingEventReceiver
233271
#[derive(Debug, Clone)]
234272
pub struct BlockingClient {
235273
tx: BlockingRequestSender,
@@ -258,7 +296,7 @@ impl BlockingClient {
258296
/// used to monitor or explicitly join the background threads if desired.
259297
///
260298
/// [`Event`]: crate::Event
261-
/// [`BlockingEventReceiver`]: crate::BlockingEventReceiver
299+
/// [`BlockingEventReceiver`]: crate::client::BlockingEventReceiver
262300
/// [`JoinHandle`]: std::thread::JoinHandle
263301
pub fn new<R, W>(
264302
reader: R,
@@ -347,7 +385,7 @@ impl BlockingClient {
347385
/// Returns [`BlockingRequestSendError`] if the request could not be queued for sending.
348386
///
349387
/// [`Event`]: crate::Event
350-
/// [`BlockingEventReceiver`]: crate::BlockingEventReceiver
388+
/// [`BlockingEventReceiver`]: crate::client::BlockingEventReceiver
351389
/// [`BlockingRequestSendError`]: crate::BlockingRequestSendError
352390
pub fn send_event_request<Req>(&self, request: Req) -> Result<(), BlockingRequestSendError>
353391
where
@@ -377,7 +415,7 @@ impl BlockingClient {
377415
/// [`BatchRequest`]: crate::BatchRequest
378416
/// [`BatchRequest::request`]: crate::BatchRequest::request
379417
/// [`BatchRequest::event_request`]: crate::BatchRequest::event_request
380-
/// [`BlockingEventReceiver`]: crate::BlockingEventReceiver
418+
/// [`BlockingEventReceiver`]: crate::client::BlockingEventReceiver
381419
pub fn send_batch(&self, batch_req: BatchRequest) -> Result<bool, BlockingRequestSendError> {
382420
match batch_req.into_inner() {
383421
Some(batch) => self.tx.send(batch).map(|_| true),

src/lib.rs

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![doc = include_str!("../README.md")]
2-
mod client;
3-
pub use client::*;
2+
pub mod client;
3+
pub use client::{
4+
AsyncClient, AsyncRequestError, AsyncRequestSendError, BlockingClient, BlockingRequestError,
5+
BlockingRequestSendError,
6+
};
47
mod custom_serde;
58
mod hash_types;
69
pub mod io;
@@ -28,67 +31,3 @@ pub type MethodAndParams = (CowStr, Vec<serde_json::Value>);
2831

2932
/// A server response that is either a success (`Ok`) or a JSON-RPC error (`Err`).
3033
pub type ResponseResult<Resp> = Result<Resp, ResponseError>;
31-
32-
/// Internal type aliases for asynchronous client components.
33-
mod async_aliases {
34-
use super::*;
35-
use futures::channel::mpsc::{TrySendError, UnboundedReceiver, UnboundedSender};
36-
use pending_request::PendingRequest;
37-
38-
/// The sending half of the channel used to enqueue one or more requests from [`AsyncClient`].
39-
///
40-
/// These requests are processed and forwarded to [`RequestTracker::track_request`] to be assigned an ID and serialized.
41-
pub type AsyncRequestSender = UnboundedSender<RawOneOrMany<PendingRequest>>;
42-
43-
/// The receiving half of the request channel used internally by the async client.
44-
///
45-
/// Requests sent by [`AsyncClient`] are dequeued here and forwarded to [`RequestTracker::track_request`].
46-
pub type AsyncRequestReceiver = UnboundedReceiver<RawOneOrMany<PendingRequest>>;
47-
48-
/// The error returned by [`AsyncClient::send_request`] when a request fails.
49-
///
50-
/// This may occur if the server responds with an error, the request is canceled, or the client is shut down.
51-
pub type AsyncRequestError = request::Error<AsyncRequestSendError>;
52-
53-
/// The error that occurs when a request cannot be sent into the async request channel.
54-
///
55-
/// This typically means the client's background task has shut down or the queue is disconnected.
56-
pub type AsyncRequestSendError = TrySendError<RawOneOrMany<PendingRequest>>;
57-
58-
/// The sending half of the internal event stream, used to emit [`Event`]s from the client worker loop.
59-
pub type AsyncEventSender = UnboundedSender<Event>;
60-
61-
/// The receiving half of the internal event stream, returned to users of [`AsyncClient`].
62-
///
63-
/// This yields all incoming [`Event`]s from the Electrum server, including notifications and responses.
64-
pub type AsyncEventReceiver = UnboundedReceiver<Event>;
65-
}
66-
pub use async_aliases::*;
67-
68-
/// Internal type aliases for blocking client components.
69-
mod blocking_aliases {
70-
use super::*;
71-
use pending_request::PendingRequest;
72-
use std::sync::mpsc::{Receiver, SendError, Sender};
73-
74-
/// Channel sender for sending blocking requests from [`BlockingClient`] to the write thread.
75-
pub type BlockingRequestSender = Sender<RawOneOrMany<PendingRequest>>;
76-
77-
/// Channel receiver used by the write thread to dequeue pending requests.
78-
pub type BlockingRequestReceiver = Receiver<RawOneOrMany<PendingRequest>>;
79-
80-
/// Error returned by [`BlockingClient::send_request`] if the request fails or is canceled.
81-
pub type BlockingRequestError = request::Error<BlockingRequestSendError>;
82-
83-
/// Error that occurs when a blocking request cannot be sent to the internal request channel.
84-
///
85-
/// Typically indicates that the client has been shut down.
86-
pub type BlockingRequestSendError = SendError<RawOneOrMany<PendingRequest>>;
87-
88-
/// Channel sender used by the read thread to emit [`Event`]s.
89-
pub type BlockingEventSender = Sender<Event>;
90-
91-
/// Channel receiver used to receive [`Event`]s from the Electrum server.
92-
pub type BlockingEventReceiver = Receiver<Event>;
93-
}
94-
pub use blocking_aliases::*;

0 commit comments

Comments
 (0)