Skip to content

Commit 1f37ef2

Browse files
committed
refactor(handlers): Apply app context
- apply app context with state to all the modules - cover case for commands when db is not required
1 parent d435c1b commit 1f37ef2

6 files changed

Lines changed: 153 additions & 193 deletions

File tree

src/handlers/config.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::client::ClientType;
1010
use crate::commands::WalletOpts;
1111
use crate::config::{WalletConfig, WalletConfigInner};
1212
use crate::error::BDKCliError as Error;
13+
use crate::handlers::Init;
1314
use crate::handlers::{AppCommand, AppContext};
1415
#[cfg(feature = "sqlite")]
1516
use crate::persister::DatabaseType;
@@ -27,10 +28,10 @@ pub struct SaveConfigCommand {
2728
pub(crate) wallet_opts: WalletOpts,
2829
}
2930

30-
impl AppCommand for SaveConfigCommand {
31+
impl AppCommand<AppContext<Init>> for SaveConfigCommand {
3132
type Output = StatusResult;
3233

33-
fn execute(&self, ctx: &mut AppContext<'_>) -> Result<Self::Output, Error> {
34+
fn execute(&self, ctx: &mut AppContext<Init>) -> Result<Self::Output, Error> {
3435
if ctx.network == Network::Bitcoin {
3536
eprintln!("WARNING: Configuring for Bitcoin MAINNET. Experimental software!");
3637
}
@@ -144,10 +145,10 @@ impl AppCommand for SaveConfigCommand {
144145
#[derive(Args, Debug, Clone, PartialEq)]
145146
pub struct ListWalletsCommand {}
146147

147-
impl AppCommand for ListWalletsCommand {
148+
impl AppCommand<AppContext<Init>> for ListWalletsCommand {
148149
type Output = WalletsListResult;
149150

150-
fn execute(&self, ctx: &mut AppContext) -> Result<Self::Output, Error> {
151+
fn execute(&self, ctx: &mut AppContext<Init>) -> Result<Self::Output, Error> {
151152
let config = match WalletConfig::load(&ctx.datadir)? {
152153
Some(cfg) => cfg,
153154
None => return Err(Error::Generic("No wallets configured yet.".into())),

src/handlers/descriptor.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::handlers::Init;
12
use crate::utils::types::DescriptorResult;
23
use crate::{
34
error::BDKCliError as Error,
@@ -38,10 +39,10 @@ pub struct DescriptorCommand {
3839
/// Optional key: xprv, xpub, or mnemonic phrase
3940
key: Option<String>,
4041
}
41-
impl AppCommand for DescriptorCommand {
42+
impl AppCommand<AppContext<Init>> for DescriptorCommand {
4243
type Output = DescriptorResult;
4344

44-
fn execute(&self, ctx: &mut AppContext) -> Result<Self::Output, Error> {
45+
fn execute(&self, ctx: &mut AppContext<Init>) -> Result<Self::Output, Error> {
4546
match &self.key {
4647
Some(key) => {
4748
if is_mnemonic(key) {
@@ -68,10 +69,10 @@ pub struct CompileCommand {
6869
}
6970

7071
#[cfg(feature = "compiler")]
71-
impl AppCommand for CompileCommand {
72+
impl AppCommand<AppContext<Init>> for CompileCommand {
7273
type Output = DescriptorResult;
7374

74-
fn execute(&self, _ctx: &mut AppContext) -> Result<Self::Output, Error> {
75+
fn execute(&self, _ctx: &mut AppContext<Init>) -> Result<Self::Output, Error> {
7576
let policy: Concrete<String> = Concrete::from_str(&self.policy)
7677
.map_err(|e| Error::Generic(format!("Invalid policy: {e}")))?;
7778

src/handlers/key.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::commands::KeySubCommand;
22
use crate::error::BDKCliError as Error;
3+
use crate::handlers::Init;
34
use crate::handlers::{AppCommand, AppContext};
45
use crate::utils::output::FormatOutput;
56
use crate::utils::types::KeyResult;
@@ -15,7 +16,7 @@ use bdk_wallet::miniscript::{self, Segwitv0};
1516
use clap::Parser;
1617

1718
impl KeySubCommand {
18-
pub fn execute(&self, ctx: &mut AppContext) -> Result<(), Error> {
19+
pub fn execute(&self, ctx: &mut AppContext<Init>) -> Result<(), Error> {
1920
match self {
2021
KeySubCommand::Generate(generate_key_command) => generate_key_command
2122
.execute(ctx)?
@@ -44,10 +45,10 @@ pub struct GenerateKeyCommand {
4445
password: Option<String>,
4546
}
4647

47-
impl AppCommand for GenerateKeyCommand {
48+
impl AppCommand<AppContext<Init>> for GenerateKeyCommand {
4849
type Output = KeyResult;
4950

50-
fn execute(&self, ctx: &mut AppContext) -> Result<Self::Output, Error> {
51+
fn execute(&self, ctx: &mut AppContext<Init>) -> Result<Self::Output, Error> {
5152
let secp = Secp256k1::new();
5253
let mnemonic_type = match self.word_count {
5354
12 => WordCount::Words12,
@@ -88,10 +89,10 @@ pub struct DeriveKeyCommand {
8889
path: DerivationPath,
8990
}
9091

91-
impl AppCommand for DeriveKeyCommand {
92+
impl AppCommand<AppContext<Init>> for DeriveKeyCommand {
9293
type Output = KeyResult;
9394

94-
fn execute(&self, ctx: &mut AppContext) -> Result<Self::Output, Error> {
95+
fn execute(&self, ctx: &mut AppContext<Init>) -> Result<Self::Output, Error> {
9596
let secp = Secp256k1::new();
9697

9798
let derived_xprv = &self.xprv.derive_priv(&secp, &self.path)?;
@@ -134,10 +135,10 @@ pub struct RestoreKeyCommand {
134135
password: Option<String>,
135136
}
136137

137-
impl AppCommand for RestoreKeyCommand {
138+
impl AppCommand<AppContext<Init>> for RestoreKeyCommand {
138139
type Output = KeyResult;
139140

140-
fn execute(&self, ctx: &mut AppContext) -> Result<Self::Output, Error> {
141+
fn execute(&self, ctx: &mut AppContext<Init>) -> Result<Self::Output, Error> {
141142
let secp = Secp256k1::new();
142143

143144
let mnemonic = Mnemonic::parse_in(Language::English, &self.mnemonic)?;

0 commit comments

Comments
 (0)