Skip to content

Commit 53d4986

Browse files
refactor: clean up errors on wallet constructors
1 parent 4d4d8db commit 53d4986

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

bdk-ffi/src/error.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,23 @@ impl From<BdkCreateWithPersistError<chain::rusqlite::Error>> for CreateWithPersi
10191019
}
10201020
}
10211021

1022+
impl From<BdkCreateWithPersistError<PersistenceError>> for CreateWithPersistError {
1023+
fn from(error: BdkCreateWithPersistError<PersistenceError>) -> Self {
1024+
match error {
1025+
BdkCreateWithPersistError::Persist(e) => CreateWithPersistError::Persist {
1026+
error_message: e.to_string(),
1027+
},
1028+
BdkCreateWithPersistError::Descriptor(e) => CreateWithPersistError::Descriptor {
1029+
error_message: e.to_string(),
1030+
},
1031+
// Objects cannot currently be used in enumerations
1032+
BdkCreateWithPersistError::DataAlreadyExists(_e) => {
1033+
CreateWithPersistError::DataAlreadyExists
1034+
}
1035+
}
1036+
}
1037+
}
1038+
10221039
impl From<AddUtxoError> for CreateTxError {
10231040
fn from(error: AddUtxoError) -> Self {
10241041
match error {
@@ -1248,6 +1265,21 @@ impl From<BdkLoadWithPersistError<chain::rusqlite::Error>> for LoadWithPersistEr
12481265
}
12491266
}
12501267

1268+
impl From<BdkLoadWithPersistError<PersistenceError>> for LoadWithPersistError {
1269+
fn from(error: BdkLoadWithPersistError<PersistenceError>) -> Self {
1270+
match error {
1271+
BdkLoadWithPersistError::Persist(e) => LoadWithPersistError::Persist {
1272+
error_message: e.to_string(),
1273+
},
1274+
BdkLoadWithPersistError::InvalidChangeSet(e) => {
1275+
LoadWithPersistError::InvalidChangeSet {
1276+
error_message: e.to_string(),
1277+
}
1278+
}
1279+
}
1280+
}
1281+
}
1282+
12511283
impl From<BdkSqliteError> for PersistenceError {
12521284
fn from(error: BdkSqliteError) -> Self {
12531285
PersistenceError::Reason {

bdk-ffi/src/wallet.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ impl Wallet {
5858
.network(network)
5959
.lookahead(lookahead)
6060
.create_wallet(deref)
61-
.map_err(|e| CreateWithPersistError::Persist {
62-
error_message: e.to_string(),
63-
})?;
61+
.map_err(CreateWithPersistError::from)?;
6462

6563
Ok(Wallet {
6664
inner_mutex: Mutex::new(wallet),
@@ -100,9 +98,7 @@ impl Wallet {
10098
.network(network)
10199
.lookahead(lookahead)
102100
.create_wallet(deref)
103-
.map_err(|e| CreateWithPersistError::Persist {
104-
error_message: e.to_string(),
105-
})?;
101+
.map_err(CreateWithPersistError::from)?;
106102

107103
Ok(Wallet {
108104
inner_mutex: Mutex::new(wallet),
@@ -134,9 +130,7 @@ impl Wallet {
134130
.network(network)
135131
.lookahead(lookahead)
136132
.create_wallet(deref)
137-
.map_err(|e| CreateWithPersistError::Persist {
138-
error_message: e.to_string(),
139-
})?;
133+
.map_err(CreateWithPersistError::from)?;
140134

141135
Ok(Wallet {
142136
inner_mutex: Mutex::new(wallet),
@@ -164,9 +158,7 @@ impl Wallet {
164158
.lookahead(lookahead)
165159
.extract_keys()
166160
.load_wallet(deref)
167-
.map_err(|e| LoadWithPersistError::Persist {
168-
error_message: e.to_string(),
169-
})?
161+
.map_err(LoadWithPersistError::from)?
170162
.ok_or(LoadWithPersistError::CouldNotLoad)?;
171163

172164
Ok(Wallet {

0 commit comments

Comments
 (0)