Skip to content

Commit 2a5f168

Browse files
committed
Refactor: 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 0d64c86 commit 2a5f168

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
@@ -970,9 +970,43 @@ macro_rules! invoice_request_respond_with_derived_signing_pubkey_methods { (
970970
}
971971
} }
972972

973+
macro_rules! fields_accessor {
974+
($self:ident, $inner:expr) => {
975+
/// Fetch the [`InvoiceRequestFields`] for this verified invoice.
976+
///
977+
/// These are fields which we expect to be useful when receiving a payment for this invoice
978+
/// request, and include the returned [`InvoiceRequestFields`] in the
979+
/// [`PaymentContext::Bolt12Offer`].
980+
///
981+
/// [`PaymentContext::Bolt12Offer`]: crate::blinded_path::payment::PaymentContext::Bolt12Offer
982+
pub fn fields(&$self) -> InvoiceRequestFields {
983+
let InvoiceRequestContents {
984+
payer_signing_pubkey,
985+
inner: InvoiceRequestContentsWithoutPayerSigningPubkey {
986+
quantity,
987+
payer_note,
988+
..
989+
},
990+
} = &$inner;
991+
992+
InvoiceRequestFields {
993+
payer_signing_pubkey: *payer_signing_pubkey,
994+
quantity: *quantity,
995+
payer_note_truncated: payer_note
996+
.clone()
997+
// Truncate the payer note to `PAYER_NOTE_LIMIT` bytes, rounding
998+
// down to the nearest valid UTF-8 code point boundary.
999+
.map(|s| UntrustedString(string_truncate_safe(s, PAYER_NOTE_LIMIT))),
1000+
human_readable_name: $self.offer_from_hrn().clone(),
1001+
}
1002+
}
1003+
};
1004+
}
1005+
9731006
impl VerifiedInvoiceRequest {
9741007
offer_accessors!(self, self.inner.contents.inner.offer);
9751008
invoice_request_accessors!(self, self.inner.contents);
1009+
fields_accessor!(self, self.inner.contents);
9761010
#[cfg(not(c_bindings))]
9771011
invoice_request_respond_with_explicit_signing_pubkey_methods!(
9781012
self,
@@ -997,31 +1031,6 @@ impl VerifiedInvoiceRequest {
9971031
self.inner,
9981032
InvoiceWithDerivedSigningPubkeyBuilder
9991033
);
1000-
1001-
/// Fetch the [`InvoiceRequestFields`] for this verified invoice.
1002-
///
1003-
/// These are fields which we expect to be useful when receiving a payment for this invoice
1004-
/// request, and include the returned [`InvoiceRequestFields`] in the
1005-
/// [`PaymentContext::Bolt12Offer`].
1006-
///
1007-
/// [`PaymentContext::Bolt12Offer`]: crate::blinded_path::payment::PaymentContext::Bolt12Offer
1008-
pub fn fields(&self) -> InvoiceRequestFields {
1009-
let InvoiceRequestContents {
1010-
payer_signing_pubkey,
1011-
inner: InvoiceRequestContentsWithoutPayerSigningPubkey { quantity, payer_note, .. },
1012-
} = &self.inner.contents;
1013-
1014-
InvoiceRequestFields {
1015-
payer_signing_pubkey: *payer_signing_pubkey,
1016-
quantity: *quantity,
1017-
payer_note_truncated: payer_note
1018-
.clone()
1019-
// Truncate the payer note to `PAYER_NOTE_LIMIT` bytes, rounding
1020-
// down to the nearest valid UTF-8 code point boundary.
1021-
.map(|s| UntrustedString(string_truncate_safe(s, PAYER_NOTE_LIMIT))),
1022-
human_readable_name: self.offer_from_hrn().clone(),
1023-
}
1024-
}
10251034
}
10261035

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

0 commit comments

Comments
 (0)