Skip to content

Commit bd93eda

Browse files
committed
Refactor wallet directory and config preparation
Split the combined helper into separate functions for preparing the wallet database directory and loading wallet config.
1 parent ffa9fc3 commit bd93eda

2 files changed

Lines changed: 26 additions & 16 deletions

File tree

‎src/utils/common.rs‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@ pub(crate) fn prepare_home_dir(home_path: Option<PathBuf>) -> Result<PathBuf, Er
112112
Ok(dir)
113113
}
114114

115+
/// Prepare wallet database directory.
116+
#[allow(dead_code)]
117+
pub(crate) fn prepare_wallet_db_dir(
118+
home_path: &Path,
119+
wallet_name: &str,
120+
) -> Result<std::path::PathBuf, Error> {
121+
let mut dir = home_path.to_owned();
122+
dir.push(wallet_name);
123+
124+
if !dir.exists() {
125+
std::fs::create_dir(&dir).map_err(|e| Error::Generic(e.to_string()))?;
126+
}
127+
128+
Ok(dir)
129+
}
130+
115131
pub fn is_mnemonic(s: &str) -> bool {
116132
let word_count = s.split_whitespace().count();
117133
(12..=24).contains(&word_count) && s.chars().all(|c| c.is_alphanumeric() || c.is_whitespace())
@@ -138,19 +154,12 @@ pub async fn trace_logger(
138154
}
139155
}
140156

141-
/// Prepare wallet database directory and config.
142-
pub fn prepare_wallet_db_dir_and_config(
143-
home_path: &Path,
157+
/// Prepare wallet database directory.
158+
pub fn load_wallet_config(
159+
home_dir: &Path,
144160
wallet_name: &str,
145-
) -> Result<(std::path::PathBuf, WalletOpts, Network), Error> {
146-
let mut dir = home_path.to_owned();
147-
dir.push(wallet_name);
148-
149-
if !dir.exists() {
150-
std::fs::create_dir(&dir).map_err(|e| Error::Generic(e.to_string()))?;
151-
}
152-
153-
let config = WalletConfig::load(home_path)?.ok_or(Error::Generic(format!(
161+
) -> Result<(WalletOpts, Network), Error> {
162+
let config = WalletConfig::load(home_dir)?.ok_or(Error::Generic(format!(
154163
"No config found for wallet {wallet_name}",
155164
)))?;
156165

@@ -165,7 +174,7 @@ pub fn prepare_wallet_db_dir_and_config(
165174
let network = Network::from_str(&wallet_config.network)
166175
.map_err(|_| Error::Generic("Invalid network in config".to_string()))?;
167176

168-
Ok((dir, wallet_opts, network))
177+
Ok((wallet_opts, network))
169178
}
170179

171180
#[cfg(feature = "silent-payments")]

‎src/utils/runtime.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
};
88

99
use crate::{
10-
error::BDKCliError as Error, persister::new_wallet, utils::prepare_wallet_db_dir_and_config,
10+
error::BDKCliError as Error, persister::new_wallet, utils::{prepare_wallet_db_dir, load_wallet_config},
1111
};
1212
#[cfg(any(feature = "sqlite", feature = "redb"))]
1313
use {
@@ -74,8 +74,9 @@ pub struct WalletRuntime {
7474

7575
impl WalletRuntime {
7676
pub fn load(home_dir: &Path, wallet_name: &str) -> Result<Self, Error> {
77-
let (database_path, wallet_opts, network) =
78-
prepare_wallet_db_dir_and_config(home_dir, wallet_name)?;
77+
let (wallet_opts, network) = load_wallet_config(home_dir, wallet_name)?;
78+
79+
let database_path = prepare_wallet_db_dir(home_dir, wallet_name)?;
7980

8081
Ok(Self {
8182
wallet_name: wallet_name.to_string(),

0 commit comments

Comments
 (0)