Skip to content

Commit cadc69c

Browse files
committed
Use SemanticError in OfferBuilder::build
1 parent 8e769d4 commit cadc69c

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(self) -> Result<Offer, ()> {
197+
pub fn build(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

@@ -552,6 +551,7 @@ mod tests {
552551
use core::time::Duration;
553552
use crate::ln::features::OfferFeatures;
554553
use crate::ln::msgs::MAX_VALUE_MSAT;
554+
use crate::offers::parse::SemanticError;
555555
use crate::onion_message::{BlindedHop, BlindedPath};
556556
use crate::util::ser::Writeable;
557557
use crate::util::string::PrintableString;
@@ -673,6 +673,10 @@ mod tests {
673673
assert_eq!(builder.offer.amount, Some(currency_amount.clone()));
674674
assert_eq!(tlv_stream.amount, Some(10));
675675
assert_eq!(tlv_stream.currency, Some(b"USD"));
676+
match builder.build() {
677+
Ok(_) => panic!("expected error"),
678+
Err(e) => assert_eq!(e, SemanticError::UnsupportedCurrency),
679+
}
676680

677681
let offer = OfferBuilder::new("foo".into(), pubkey(42))
678682
.amount(currency_amount.clone())
@@ -686,7 +690,7 @@ mod tests {
686690
let invalid_amount = Amount::Bitcoin { amount_msats: MAX_VALUE_MSAT + 1 };
687691
match OfferBuilder::new("foo".into(), pubkey(42)).amount(invalid_amount).build() {
688692
Ok(_) => panic!("expected error"),
689-
Err(e) => assert_eq!(e, ()),
693+
Err(e) => assert_eq!(e, SemanticError::InvalidAmount),
690694
}
691695
}
692696

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 node id was not provided.

0 commit comments

Comments
 (0)