From 2bdeb3d759cc53c7b43feeddfe93baf03fdd97a0 Mon Sep 17 00:00:00 2001 From: emma31-dev Date: Sat, 5 Sep 2026 18:34:38 +0100 Subject: [PATCH] Refactor wallet DB directory setup into config loading Combine `prepare_wallet_db_dir` and `load_wallet_config` into a single function that returns the database path alongside wallet options and network, reducing code duplication in the `WalletRuntime` loader. Signed-off-by: emma31-dev --- src/utils/common.rs | 34 +++++++++++++--------------------- src/utils/runtime.rs | 9 +++------ 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/src/utils/common.rs b/src/utils/common.rs index 136c9873..20996c7b 100644 --- a/src/utils/common.rs +++ b/src/utils/common.rs @@ -112,22 +112,6 @@ pub(crate) fn prepare_home_dir(home_path: Option) -> Result Result { - let mut dir = home_path.to_owned(); - dir.push(wallet_name); - - if !dir.exists() { - std::fs::create_dir(&dir).map_err(|e| Error::Generic(e.to_string()))?; - } - - Ok(dir) -} - pub fn is_mnemonic(s: &str) -> bool { let word_count = s.split_whitespace().count(); (12..=24).contains(&word_count) && s.chars().all(|c| c.is_alphanumeric() || c.is_whitespace()) @@ -154,11 +138,19 @@ pub async fn trace_logger( } } -pub fn load_wallet_config( - home_dir: &Path, +/// Prepare wallet database directory and config. +pub fn prepare_wallet_db_dir_and_config( + home_path: &Path, wallet_name: &str, -) -> Result<(WalletOpts, Network), Error> { - let config = WalletConfig::load(home_dir)?.ok_or(Error::Generic(format!( +) -> Result<(std::path::PathBuf, WalletOpts, Network), Error> { + let mut dir = home_path.to_owned(); + dir.push(wallet_name); + + if !dir.exists() { + std::fs::create_dir(&dir).map_err(|e| Error::Generic(e.to_string()))?; + } + + let config = WalletConfig::load(home_path)?.ok_or(Error::Generic(format!( "No config found for wallet {wallet_name}", )))?; @@ -173,7 +165,7 @@ pub fn load_wallet_config( let network = Network::from_str(&wallet_config.network) .map_err(|_| Error::Generic("Invalid network in config".to_string()))?; - Ok((wallet_opts, network)) + Ok((dir, wallet_opts, network)) } #[cfg(feature = "silent-payments")] diff --git a/src/utils/runtime.rs b/src/utils/runtime.rs index 8beb5527..31e86273 100644 --- a/src/utils/runtime.rs +++ b/src/utils/runtime.rs @@ -7,9 +7,7 @@ use std::{ }; use crate::{ - error::BDKCliError as Error, - persister::new_wallet, - utils::{load_wallet_config, prepare_wallet_db_dir}, + error::BDKCliError as Error, persister::new_wallet, utils::prepare_wallet_db_dir_and_config, }; #[cfg(any(feature = "sqlite", feature = "redb"))] use { @@ -76,9 +74,8 @@ pub struct WalletRuntime { impl WalletRuntime { pub fn load(home_dir: &Path, wallet_name: &str) -> Result { - let (wallet_opts, network) = load_wallet_config(home_dir, wallet_name)?; - - let database_path = prepare_wallet_db_dir(home_dir, wallet_name)?; + let (database_path, wallet_opts, network) = + prepare_wallet_db_dir_and_config(home_dir, wallet_name)?; Ok(Self { wallet_name: wallet_name.to_string(),