Skip to content

Commit 5064485

Browse files
committed
Use SemanticError in OfferBuilder::build
1 parent 8fd7637 commit 5064485

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

lightning/src/offers/offer.rs

+10-6
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

@@ -562,6 +561,7 @@ mod tests {
562561
use core::time::Duration;
563562
use crate::ln::features::OfferFeatures;
564563
use crate::ln::msgs::MAX_VALUE_MSAT;
564+
use crate::offers::parse::SemanticError;
565565
use crate::onion_message::{BlindedHop, BlindedPath};
566566
use crate::util::ser::Writeable;
567567
use crate::util::string::PrintableString;
@@ -687,6 +687,10 @@ mod tests {
687687
assert_eq!(builder.offer.amount, Some(currency_amount.clone()));
688688
assert_eq!(tlv_stream.amount, Some(10));
689689
assert_eq!(tlv_stream.currency, Some(b"USD"));
690+
match builder.build() {
691+
Ok(_) => panic!("expected error"),
692+
Err(e) => assert_eq!(e, SemanticError::UnsupportedCurrency),
693+
}
690694

691695
let offer = OfferBuilder::new("foo".into(), pubkey(42))
692696
.amount(currency_amount.clone())
@@ -700,7 +704,7 @@ mod tests {
700704
let invalid_amount = Amount::Bitcoin { amount_msats: MAX_VALUE_MSAT + 1 };
701705
match OfferBuilder::new("foo".into(), pubkey(42)).amount(invalid_amount).build() {
702706
Ok(_) => panic!("expected error"),
703-
Err(e) => assert_eq!(e, ()),
707+
Err(e) => assert_eq!(e, SemanticError::InvalidAmount),
704708
}
705709
}
706710

lightning/src/offers/parse.rs

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ pub enum ParseError {
7474
pub enum SemanticError {
7575
/// An amount was expected but was missing.
7676
MissingAmount,
77+
/// An amount exceeded the maximum number of bitcoin.
78+
InvalidAmount,
79+
/// A currency was provided that is not supported.
80+
UnsupportedCurrency,
7781
/// A required description was not provided.
7882
MissingDescription,
7983
/// A node id was not provided.

0 commit comments

Comments
 (0)