Skip to content

Commit 0d8d0ec

Browse files
committed
feat: add error messages for CreateWithPersistError Display implementation
1 parent b37355a commit 0d8d0ec

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

wallet/src/wallet/persisted.rs

+45-9
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ use core::{
66
pin::Pin,
77
};
88

9-
use alloc::boxed::Box;
9+
use alloc::{
10+
boxed::Box,
11+
string::{String, ToString},
12+
};
1013
use chain::Merge;
1114

12-
use crate::{descriptor::DescriptorError, ChangeSet, CreateParams, LoadParams, Wallet};
15+
use crate::{
16+
descriptor::{calc_checksum, DescriptorError},
17+
ChangeSet, CreateParams, LoadParams, Wallet,
18+
};
1319

1420
/// Trait that persists [`PersistedWallet`].
1521
///
@@ -363,16 +369,46 @@ pub enum CreateWithPersistError<E> {
363369
impl<E: fmt::Display> fmt::Display for CreateWithPersistError<E> {
364370
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
365371
match self {
366-
Self::Persist(err) => fmt::Display::fmt(err, f),
367-
Self::DataAlreadyExists(changeset) => write!(
368-
f,
369-
"Cannot create wallet in persister which already contains wallet data: {:?}",
370-
changeset
371-
),
372-
Self::Descriptor(err) => fmt::Display::fmt(&err, f),
372+
Self::Persist(err) => write!(f, "{}", err),
373+
Self::DataAlreadyExists(changeset) => {
374+
write!(
375+
f,
376+
"Cannot create wallet in a persister which already contains data: {}",
377+
changeset_info(changeset)
378+
)
379+
}
380+
Self::Descriptor(err) => {
381+
write!(f, "{err}")
382+
}
373383
}
374384
}
375385
}
376386

377387
#[cfg(feature = "std")]
378388
impl<E: fmt::Debug + fmt::Display> std::error::Error for CreateWithPersistError<E> {}
389+
390+
/// Helper function to display basic information about a [`ChangeSet`].
391+
fn changeset_info(changeset: &ChangeSet) -> String {
392+
let network = match &changeset.network {
393+
Some(network) => network.to_string(),
394+
None => "None".to_string(),
395+
};
396+
397+
let mut checksum = String::new();
398+
if let Some(desc) = &changeset.descriptor {
399+
if let Ok(ck) = calc_checksum(&desc.to_string()) {
400+
checksum += ck.as_str();
401+
}
402+
}
403+
if let Some(desc) = &changeset.change_descriptor {
404+
if let Ok(ck) = calc_checksum(&desc.to_string()) {
405+
checksum += ck.as_str();
406+
}
407+
}
408+
// Don't return an empty checksum
409+
if checksum.is_empty() {
410+
checksum = "None".to_string();
411+
}
412+
413+
format!("Network: {}, Descriptor Checksum: {}", network, checksum)
414+
}

0 commit comments

Comments
 (0)