Skip to content

Commit bc2a01e

Browse files
committed
f - remove Option from some Offer method return values
1 parent 9eb3b94 commit bc2a01e

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

lightning/src/offers/offer.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ impl OfferBuilder {
8585
/// Use a different pubkey per offer to avoid correlating offers.
8686
pub fn new(description: String, signing_pubkey: PublicKey) -> Self {
8787
let offer = OfferContents {
88-
chains: None, metadata: None, amount: None, description, features: None,
89-
absolute_expiry: None, issuer: None, paths: None, quantity_min: None,
90-
quantity_max: None, signing_pubkey: Some(signing_pubkey),
88+
chains: None, metadata: None, amount: None, description,
89+
features: OfferFeatures::empty(), absolute_expiry: None, issuer: None, paths: None,
90+
quantity_min: None, quantity_max: None, signing_pubkey: Some(signing_pubkey),
9191
};
9292
OfferBuilder { offer }
9393
}
@@ -127,7 +127,7 @@ impl OfferBuilder {
127127
/// Successive calls to this method will override the previous setting.
128128
#[cfg(test)]
129129
pub fn features(mut self, features: OfferFeatures) -> Self {
130-
self.offer.features = Some(features);
130+
self.offer.features = features;
131131
self
132132
}
133133

@@ -350,13 +350,17 @@ impl OfferContents {
350350
),
351351
};
352352

353+
let features = {
354+
if self.features == OfferFeatures::empty() { None } else { Some(&self.features) }
355+
};
356+
353357
OfferTlvStreamRef {
354358
chains: self.chains.as_ref(),
355359
metadata: self.metadata.as_ref(),
356360
currency,
357361
amount,
358362
description: Some(&self.description),
359-
features: self.features.as_ref(),
363+
features,
360364
absolute_expiry: self.absolute_expiry.map(|duration| duration.as_secs()),
361365
paths: self.paths.as_ref(),
362366
issuer: self.issuer.as_ref(),
@@ -443,11 +447,11 @@ mod tests {
443447
assert_eq!(offer.metadata(), None);
444448
assert_eq!(offer.amount(), None);
445449
assert_eq!(offer.description(), "foo");
446-
assert_eq!(offer.features(), None);
450+
assert_eq!(offer.features(), &OfferFeatures::empty());
447451
assert_eq!(offer.absolute_expiry(), None);
448452
#[cfg(feature = "std")]
449453
assert!(!offer.is_expired());
450-
assert_eq!(offer.paths(), None);
454+
assert_eq!(offer.paths(), &[]);
451455
assert_eq!(offer.issuer(), None);
452456
assert_eq!(offer.quantity_min(), 1);
453457
assert_eq!(offer.quantity_max(), 1);
@@ -553,19 +557,19 @@ mod tests {
553557
#[test]
554558
fn builds_offer_with_features() {
555559
let offer = OfferBuilder::new("foo".into(), pubkey(42))
556-
.features(OfferFeatures::empty())
560+
.features(OfferFeatures::unknown())
557561
.build()
558562
.unwrap();
559-
assert_eq!(offer.features(), Some(&OfferFeatures::empty()));
560-
assert_eq!(offer.as_tlv_stream().features, Some(&OfferFeatures::empty()));
563+
assert_eq!(offer.features(), &OfferFeatures::unknown());
564+
assert_eq!(offer.as_tlv_stream().features, Some(&OfferFeatures::unknown()));
561565

562566
let offer = OfferBuilder::new("foo".into(), pubkey(42))
563567
.features(OfferFeatures::unknown())
564568
.features(OfferFeatures::empty())
565569
.build()
566570
.unwrap();
567-
assert_eq!(offer.features(), Some(&OfferFeatures::empty()));
568-
assert_eq!(offer.as_tlv_stream().features, Some(&OfferFeatures::empty()));
571+
assert_eq!(offer.features(), &OfferFeatures::empty());
572+
assert_eq!(offer.as_tlv_stream().features, None);
569573
}
570574

571575
#[test]
@@ -620,7 +624,7 @@ mod tests {
620624
.build()
621625
.unwrap();
622626
let tlv_stream = offer.as_tlv_stream();
623-
assert_eq!(offer.paths(), Some(&paths));
627+
assert_eq!(offer.paths(), paths.as_slice());
624628
assert_eq!(offer.signing_pubkey(), pubkey(42));
625629
assert_ne!(pubkey(42), pubkey(44));
626630
assert_eq!(tlv_stream.paths, Some(&paths));
@@ -633,15 +637,15 @@ mod tests {
633637
.issuer("bar".into())
634638
.build()
635639
.unwrap();
636-
assert_eq!(offer.issuer(), Some(&String::from("bar")));
640+
assert_eq!(offer.issuer(), Some("bar"));
637641
assert_eq!(offer.as_tlv_stream().issuer, Some(&String::from("bar")));
638642

639643
let offer = OfferBuilder::new("foo".into(), pubkey(42))
640644
.issuer("bar".into())
641645
.issuer("baz".into())
642646
.build()
643647
.unwrap();
644-
assert_eq!(offer.issuer(), Some(&String::from("baz")));
648+
assert_eq!(offer.issuer(), Some("baz"));
645649
assert_eq!(offer.as_tlv_stream().issuer, Some(&String::from("baz")));
646650
}
647651

0 commit comments

Comments
 (0)