Skip to content

Commit 3c00f1a

Browse files
committed
feat: add error messages for CreateWithPersistError Display implementation
1 parent c799b52 commit 3c00f1a

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

wallet/src/wallet/persisted.rs

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ use core::{
66
pin::Pin,
77
};
88

9-
use alloc::boxed::Box;
9+
use alloc::{boxed::Box, string::ToString};
1010
use chain::Merge;
1111

12-
use crate::{descriptor::DescriptorError, ChangeSet, CreateParams, LoadParams, Wallet};
12+
use crate::{
13+
descriptor::{calc_checksum, DescriptorError},
14+
ChangeSet, CreateParams, LoadParams, Wallet,
15+
};
1316

1417
/// Trait that persists [`PersistedWallet`].
1518
///
@@ -361,16 +364,63 @@ pub enum CreateWithPersistError<E> {
361364
impl<E: fmt::Display> fmt::Display for CreateWithPersistError<E> {
362365
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
363366
match self {
364-
Self::Persist(err) => fmt::Display::fmt(err, f),
365-
Self::DataAlreadyExists(changeset) => write!(
366-
f,
367-
"Cannot create wallet in persister which already contains wallet data: {:?}",
368-
changeset
369-
),
370-
Self::Descriptor(err) => fmt::Display::fmt(&err, f),
367+
Self::Persist(err) => write!(f, "{}", err),
368+
Self::DataAlreadyExists(changeset) => {
369+
write!(
370+
f,
371+
"Cannot create wallet in a persister which already contains data: "
372+
)?;
373+
changeset_info(f, changeset)
374+
}
375+
Self::Descriptor(err) => {
376+
write!(f, "{err}")
377+
}
371378
}
372379
}
373380
}
374381

375382
#[cfg(feature = "std")]
376383
impl<E: fmt::Debug + fmt::Display> std::error::Error for CreateWithPersistError<E> {}
384+
385+
/// Helper function to display basic information about a [`ChangeSet`].
386+
fn changeset_info(f: &mut fmt::Formatter<'_>, changeset: &ChangeSet) -> fmt::Result {
387+
let network = changeset
388+
.network
389+
.as_ref()
390+
.map_or("None".to_string(), |n| n.to_string());
391+
392+
let descriptor_checksum = changeset
393+
.descriptor
394+
.as_ref()
395+
.and_then(|d| calc_checksum(&d.to_string()).ok())
396+
.unwrap_or_else(|| "None".to_string());
397+
398+
let change_descriptor_checksum = changeset
399+
.change_descriptor
400+
.as_ref()
401+
.and_then(|d| calc_checksum(&d.to_string()).ok())
402+
.unwrap_or_else(|| "None".to_string());
403+
404+
let tx_count = changeset.tx_graph.txs.len();
405+
406+
let anchor_count = changeset.tx_graph.anchors.len();
407+
408+
let block_count = if let Some(&count) = changeset.local_chain.blocks.keys().last() {
409+
count
410+
} else {
411+
0
412+
};
413+
414+
writeln!(f, " Network: {}", network)?;
415+
writeln!(f, " Descriptor Checksum: {}", descriptor_checksum)?;
416+
writeln!(
417+
f,
418+
" Change Descriptor Checksum: {}",
419+
change_descriptor_checksum
420+
)?;
421+
writeln!(f, " Transaction Count: {}", tx_count)?;
422+
writeln!(f, " Anchor Count: {}", anchor_count)?;
423+
writeln!(f, " Block Count: {}", block_count)?;
424+
425+
Ok(())
426+
}

0 commit comments

Comments
 (0)