Skip to content

Commit 3a6d7b8

Browse files
committed
Use SemanticError in OfferBuilder::build
1 parent 60d7ffc commit 3a6d7b8

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

lightning/src/offers/offer.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@
4848
//! .issuer("Foo Bar".to_string())
4949
//! .path(create_blinded_path())
5050
//! .path(create_another_blinded_path())
51-
//! .build()
52-
//! .unwrap();
51+
//! .build()?;
5352
//!
5453
//! // Encode as a bech32 string for use in a QR code.
5554
//! let encoded_offer = offer.to_string();
@@ -195,14 +194,14 @@ impl OfferBuilder {
195194
}
196195

197196
/// Builds an [`Offer`] from the builder's settings.
198-
pub fn build(mut self) -> Result<Offer, ()> {
197+
pub fn build(mut self) -> Result<Offer, SemanticError> {
199198
match self.offer.amount {
200199
Some(Amount::Bitcoin { amount_msats }) => {
201200
if amount_msats > MAX_VALUE_MSAT {
202-
return Err(());
201+
return Err(SemanticError::InvalidAmount);
203202
}
204203
},
205-
Some(Amount::Currency { .. }) => unreachable!(),
204+
Some(Amount::Currency { .. }) => return Err(SemanticError::UnsupportedCurrency),
206205
None => {},
207206
}
208207

@@ -553,6 +552,7 @@ mod tests {
553552
use core::time::Duration;
554553
use crate::ln::features::OfferFeatures;
555554
use crate::ln::msgs::MAX_VALUE_MSAT;
555+
use crate::offers::parse::SemanticError;
556556
use crate::onion_message::{BlindedHop, BlindedPath};
557557
use crate::util::ser::Writeable;
558558
use crate::util::string::PrintableString;
@@ -678,6 +678,10 @@ mod tests {
678678
assert_eq!(builder.offer.amount, Some(currency_amount.clone()));
679679
assert_eq!(tlv_stream.amount, Some(10));
680680
assert_eq!(tlv_stream.currency, Some(b"USD"));
681+
match builder.build() {
682+
Ok(_) => panic!("expected error"),
683+
Err(e) => assert_eq!(e, SemanticError::UnsupportedCurrency),
684+
}
681685

682686
let offer = OfferBuilder::new("foo".into(), pubkey(42))
683687
.amount(currency_amount.clone())
@@ -691,7 +695,7 @@ mod tests {
691695
let invalid_amount = Amount::Bitcoin { amount_msats: MAX_VALUE_MSAT + 1 };
692696
match OfferBuilder::new("foo".into(), pubkey(42)).amount(invalid_amount).build() {
693697
Ok(_) => panic!("expected error"),
694-
Err(e) => assert_eq!(e, ()),
698+
Err(e) => assert_eq!(e, SemanticError::InvalidAmount),
695699
}
696700
}
697701

lightning/src/offers/parse.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pub enum SemanticError {
9898
MissingAmount,
9999
/// The amount exceeded the total bitcoin supply.
100100
InvalidAmount,
101+
/// A currency was provided that is not supported.
102+
UnsupportedCurrency,
101103
/// A required description was not provided.
102104
MissingDescription,
103105
/// A signing pubkey was not provided.

0 commit comments

Comments
 (0)