Skip to content

Commit 96ae457

Browse files
committed
f: Convert fields function to macro
In the following commits we will introduce `fields` function for other types as well, so to keep code DRY we convert the function to a macro.
1 parent 827da23 commit 96ae457

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -961,9 +961,43 @@ macro_rules! invoice_request_respond_with_derived_signing_pubkey_methods { (
961961
}
962962
} }
963963

964+
macro_rules! fields_accessor {
965+
($self:ident, $inner:expr) => {
966+
/// Fetch the [`InvoiceRequestFields`] for this verified invoice.
967+
///
968+
/// These are fields which we expect to be useful when receiving a payment for this invoice
969+
/// request, and include the returned [`InvoiceRequestFields`] in the
970+
/// [`PaymentContext::Bolt12Offer`].
971+
///
972+
/// [`PaymentContext::Bolt12Offer`]: crate::blinded_path::payment::PaymentContext::Bolt12Offer
973+
pub fn fields(&$self) -> InvoiceRequestFields {
974+
let InvoiceRequestContents {
975+
payer_signing_pubkey,
976+
inner: InvoiceRequestContentsWithoutPayerSigningPubkey {
977+
quantity,
978+
payer_note,
979+
..
980+
},
981+
} = &$inner;
982+
983+
InvoiceRequestFields {
984+
payer_signing_pubkey: *payer_signing_pubkey,
985+
quantity: *quantity,
986+
payer_note_truncated: payer_note
987+
.clone()
988+
// Truncate the payer note to `PAYER_NOTE_LIMIT` bytes, rounding
989+
// down to the nearest valid UTF-8 code point boundary.
990+
.map(|s| UntrustedString(string_truncate_safe(s, PAYER_NOTE_LIMIT))),
991+
human_readable_name: $self.offer_from_hrn().clone(),
992+
}
993+
}
994+
};
995+
}
996+
964997
impl VerifiedInvoiceRequestLegacy {
965998
offer_accessors!(self, self.inner.contents.inner.offer);
966999
invoice_request_accessors!(self, self.inner.contents);
1000+
fields_accessor!(self, self.inner.contents);
9671001
#[cfg(not(c_bindings))]
9681002
invoice_request_respond_with_explicit_signing_pubkey_methods!(
9691003
self,
@@ -988,31 +1022,6 @@ impl VerifiedInvoiceRequestLegacy {
9881022
self.inner,
9891023
InvoiceWithDerivedSigningPubkeyBuilder
9901024
);
991-
992-
/// Fetch the [`InvoiceRequestFields`] for this verified invoice.
993-
///
994-
/// These are fields which we expect to be useful when receiving a payment for this invoice
995-
/// request, and include the returned [`InvoiceRequestFields`] in the
996-
/// [`PaymentContext::Bolt12Offer`].
997-
///
998-
/// [`PaymentContext::Bolt12Offer`]: crate::blinded_path::payment::PaymentContext::Bolt12Offer
999-
pub fn fields(&self) -> InvoiceRequestFields {
1000-
let InvoiceRequestContents {
1001-
payer_signing_pubkey,
1002-
inner: InvoiceRequestContentsWithoutPayerSigningPubkey { quantity, payer_note, .. },
1003-
} = &self.inner.contents;
1004-
1005-
InvoiceRequestFields {
1006-
payer_signing_pubkey: *payer_signing_pubkey,
1007-
quantity: *quantity,
1008-
payer_note_truncated: payer_note
1009-
.clone()
1010-
// Truncate the payer note to `PAYER_NOTE_LIMIT` bytes, rounding
1011-
// down to the nearest valid UTF-8 code point boundary.
1012-
.map(|s| UntrustedString(string_truncate_safe(s, PAYER_NOTE_LIMIT))),
1013-
human_readable_name: self.offer_from_hrn().clone(),
1014-
}
1015-
}
10161025
}
10171026

10181027
/// `String::truncate(new_len)` panics if you split inside a UTF-8 code point,

0 commit comments

Comments
 (0)