Skip to content

Commit 561ca37

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 ac8f897 commit 561ca37

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

fuzz/src/full_stack.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ use bitcoin::secp256k1::{self, Message, PublicKey, Scalar, Secp256k1, SecretKey}
8080

8181
use lightning::util::dyn_signer::DynSigner;
8282

83-
use std::collections::VecDeque;
8483
use std::cell::RefCell;
8584
use std::cmp;
85+
use std::collections::VecDeque;
8686
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
8787
use std::sync::{Arc, Mutex};
8888

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 VerifiedInvoiceRequest {
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 VerifiedInvoiceRequest {
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)