Skip to content

Commit f6ce898

Browse files
committed
Use SemanticError in OfferBuilder::build
1 parent 333d5c4 commit f6ce898

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-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

@@ -550,6 +549,7 @@ mod tests {
550549
use core::time::Duration;
551550
use crate::ln::features::OfferFeatures;
552551
use crate::ln::msgs::MAX_VALUE_MSAT;
552+
use crate::offers::parse::SemanticError;
553553
use crate::onion_message::{BlindedHop, BlindedPath};
554554
use crate::util::ser::Writeable;
555555
use crate::util::string::PrintableString;
@@ -675,6 +675,10 @@ mod tests {
675675
assert_eq!(builder.offer.amount, Some(currency_amount.clone()));
676676
assert_eq!(tlv_stream.amount, Some(10));
677677
assert_eq!(tlv_stream.currency, Some(b"USD"));
678+
match builder.build() {
679+
Ok(_) => panic!("expected error"),
680+
Err(e) => assert_eq!(e, SemanticError::UnsupportedCurrency),
681+
}
678682

679683
let offer = OfferBuilder::new("foo".into(), pubkey(42))
680684
.amount(currency_amount.clone())
@@ -688,7 +692,7 @@ mod tests {
688692
let invalid_amount = Amount::Bitcoin { amount_msats: MAX_VALUE_MSAT + 1 };
689693
match OfferBuilder::new("foo".into(), pubkey(42)).amount(invalid_amount).build() {
690694
Ok(_) => panic!("expected error"),
691-
Err(e) => assert_eq!(e, ()),
695+
Err(e) => assert_eq!(e, SemanticError::InvalidAmount),
692696
}
693697
}
694698

lightning/src/offers/parse.rs

Lines changed: 4 additions & 0 deletions
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 signing pubkey was not provided.

0 commit comments

Comments
 (0)