radix_transactions/model/hash/
display.rs

1use crate::internal_prelude::*;
2
3#[derive(Clone, Copy)]
4pub struct TransactionHashDisplayContext<'a> {
5    pub encoder: Option<&'a TransactionHashBech32Encoder>,
6}
7
8impl<'a> TransactionHashDisplayContext<'a> {
9    pub fn with_encoder(encoder: &'a TransactionHashBech32Encoder) -> Self {
10        TransactionHashDisplayContext {
11            encoder: Some(encoder),
12        }
13    }
14}
15
16impl<'a> From<&'a TransactionHashBech32Encoder> for TransactionHashDisplayContext<'a> {
17    fn from(encoder: &'a TransactionHashBech32Encoder) -> Self {
18        Self::with_encoder(encoder)
19    }
20}
21
22impl<'a> From<Option<&'a TransactionHashBech32Encoder>> for TransactionHashDisplayContext<'a> {
23    fn from(encoder: Option<&'a TransactionHashBech32Encoder>) -> Self {
24        Self { encoder }
25    }
26}
27
28macro_rules! impl_contextual_display {
29    ($($type: ty),* $(,)?) => {
30        $(
31            impl<'a> ContextualDisplay<TransactionHashDisplayContext<'a>> for $type {
32                type Error = fmt::Error;
33
34                fn contextual_format<F: fmt::Write>(
35                    &self,
36                    f: &mut F,
37                    context: &TransactionHashDisplayContext<'a>,
38                ) -> Result<(), Self::Error> {
39                    if let Some(encoder) = context.encoder {
40                        encoder.encode_to_fmt(f, self).map_err(|_| fmt::Error)
41                    } else {
42                        write!(f, "{}", self.as_inner_hash())
43                    }
44                }
45            }
46        )*
47    };
48}
49
50impl_contextual_display![
51    TransactionIntentHash,
52    SignedTransactionIntentHash,
53    IntentHash,
54    SubintentHash,
55    NotarizedTransactionHash,
56    SystemTransactionHash,
57    LedgerTransactionHash,
58];