From a601712da0020972dd569bc9bd803251ebb69886 Mon Sep 17 00:00:00 2001 From: rustaceanrob Date: Thu, 12 Mar 2026 14:42:18 +0000 Subject: [PATCH] Move client state into isolated module --- src/builder.rs | 2 +- src/lib.rs | 47 +++++++++++++++++++++++++---------------------- tests/client.rs | 2 +- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 600d733..c89ddb6 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -53,7 +53,7 @@ use bdk_wallet::{ pub use bip157::Builder; use bip157::{chain::ChainState, HeaderCheckpoint}; -use crate::{Idle, LightClient, LoggingSubscribers, ScanType, UpdateSubscriber}; +use crate::{state::Idle, LightClient, LoggingSubscribers, ScanType, UpdateSubscriber}; const IMPOSSIBLE_REORG_DEPTH: usize = 7; diff --git a/src/lib.rs b/src/lib.rs index 49c1483..2619d13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,27 +64,30 @@ pub use bip157::UnboundedReceiver; pub use builder::BuilderExt; pub mod builder; -/// Client state when idle. -pub struct Idle; -/// Client state when subscribed to events. -pub struct Subscribed; -/// Client state when active. -pub struct Active; +/// State of the light client. +pub mod state { + /// Client state when idle. + pub struct Idle; + /// Client state when subscribed to events. + pub struct Subscribed; + /// Client state when active. + pub struct Active; +} mod sealed { pub trait Sealed {} } -impl sealed::Sealed for Idle {} -impl sealed::Sealed for Subscribed {} -impl sealed::Sealed for Active {} +impl sealed::Sealed for state::Idle {} +impl sealed::Sealed for state::Subscribed {} +impl sealed::Sealed for state::Active {} /// State of the client. pub trait State: sealed::Sealed {} -impl State for Idle {} -impl State for Subscribed {} -impl State for Active {} +impl State for state::Idle {} +impl State for state::Subscribed {} +impl State for state::Active {} /// Subscribe to events, notably #[derive(Debug)] @@ -115,13 +118,13 @@ pub struct LightClient { _marker: core::marker::PhantomData, } -impl LightClient { +impl LightClient { fn new( requester: Requester, logging: LoggingSubscribers, update: UpdateSubscriber, node: bip157::Node, - ) -> LightClient { + ) -> LightClient { LightClient { requester, logging_subscribers: Some(logging), @@ -143,7 +146,7 @@ impl LightClient { pub fn subscribe( mut self, ) -> ( - LightClient, + LightClient, LoggingSubscribers, UpdateSubscriber, ) { @@ -162,7 +165,7 @@ impl LightClient { } } -impl LightClient { +impl LightClient { /// Start fetching data for the wallet on a dedicated [`tokio::task`]. This will continually /// run until terminated or no peers could be found. /// @@ -170,7 +173,7 @@ impl LightClient { /// /// If there is no [`tokio::runtime::Runtime`] to drive execution. Common in synchronous /// setups. - pub fn start(mut self) -> LightClient { + pub fn start(mut self) -> LightClient { let node = core::mem::take(&mut self.node).expect("cannot start twice."); tokio::task::spawn(async move { node.run().await }); LightClient { @@ -184,7 +187,7 @@ impl LightClient { /// Take the underlying node process to run in a custom way. Examples include using a dedicated /// [`tokio::runtime::Runtime`] or [`tokio::runtime::Handle`] to drive execution. - pub fn managed_start(mut self) -> (LightClient, Node) { + pub fn managed_start(mut self) -> (LightClient, Node) { let node = core::mem::take(&mut self.node).expect("cannot start twice."); let client = LightClient { requester: self.requester, @@ -197,20 +200,20 @@ impl LightClient { } } -impl LightClient { +impl LightClient { /// The client is active and may now handle requests with a [`Requester`]. pub fn requester(self) -> Requester { self.requester } } -impl From> for Requester { - fn from(value: LightClient) -> Self { +impl From> for Requester { + fn from(value: LightClient) -> Self { value.requester } } -impl AsRef for LightClient { +impl AsRef for LightClient { fn as_ref(&self) -> &Requester { &self.requester } diff --git a/tests/client.rs b/tests/client.rs index 5f6ee0c..299d16b 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -1,5 +1,5 @@ // #![allow(unused)] -use bdk_kyoto::Idle; +use bdk_kyoto::state::Idle; use std::net::IpAddr; use std::path::PathBuf; use std::time::Duration;