Skip to content

Commit 6d46114

Browse files
committed
Use SemanticError in OfferBuilder::build
1 parent 397bf7b commit 6d46114

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

lightning/src/offers/offer.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
//! .issuer("Foo Bar".to_string())
4848
//! .path(create_blinded_path())
4949
//! .path(create_another_blinded_path())
50-
//! .build()
51-
//! .unwrap();
50+
//! .build()?;
5251
//!
5352
//! // Encode as a bech32 string for use in a QR code.
5453
//! let encoded_offer = offer.to_string();
@@ -213,17 +212,17 @@ impl OfferBuilder {
213212
}
214213

215214
/// Builds an [`Offer`] from the builder's settings.
216-
pub fn build(self) -> Result<Offer, ()> {
215+
pub fn build(self) -> Result<Offer, SemanticError> {
217216
if let Some(Amount::Currency { .. }) = self.offer.amount {
218-
return Err(());
217+
return Err(SemanticError::UnsupportedCurrency);
219218
}
220219

221220
if self.offer.amount_msats() > MAX_VALUE_MSAT {
222-
return Err(());
221+
return Err(SemanticError::InvalidAmount);
223222
}
224223

225224
if self.offer.quantity_min() > self.offer.quantity_max() {
226-
return Err(());
225+
return Err(SemanticError::InvalidQuantity);
227226
}
228227

229228
let mut bytes = Vec::new();
@@ -559,6 +558,7 @@ mod tests {
559558
use core::time::Duration;
560559
use ln::features::OfferFeatures;
561560
use ln::msgs::MAX_VALUE_MSAT;
561+
use offers::parse::SemanticError;
562562
use onion_message::{BlindedHop, BlindedPath};
563563
use util::ser::Writeable;
564564

@@ -679,7 +679,7 @@ mod tests {
679679
assert_eq!(tlv_stream.currency, Some(b"USD"));
680680
match builder.build() {
681681
Ok(_) => panic!("expected error"),
682-
Err(e) => assert_eq!(e, ()),
682+
Err(e) => assert_eq!(e, SemanticError::UnsupportedCurrency),
683683
}
684684

685685
let offer = OfferBuilder::new("foo".into(), pubkey(42))
@@ -694,7 +694,7 @@ mod tests {
694694
let invalid_amount = Amount::Bitcoin { amount_msats: MAX_VALUE_MSAT + 1 };
695695
match OfferBuilder::new("foo".into(), pubkey(42)).amount(invalid_amount).build() {
696696
Ok(_) => panic!("expected error"),
697-
Err(e) => assert_eq!(e, ()),
697+
Err(e) => assert_eq!(e, SemanticError::InvalidAmount),
698698
}
699699
}
700700

@@ -920,11 +920,10 @@ mod tests {
920920
assert_eq!(tlv_stream.quantity_min, None);
921921
assert_eq!(tlv_stream.quantity_max, Some(9));
922922

923-
assert!(OfferBuilder::new("foo".into(), pubkey(42))
924-
.quantity_range(ten..five)
925-
.build()
926-
.is_err()
927-
);
923+
match OfferBuilder::new("foo".into(), pubkey(42)).quantity_range(ten..five).build() {
924+
Ok(_) => panic!("expected error"),
925+
Err(e) => assert_eq!(e, SemanticError::InvalidQuantity),
926+
}
928927
}
929928
}
930929

lightning/src/offers/parse.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ pub enum SemanticError {
7979
UnsupportedChain,
8080
/// An amount was expected but was missing.
8181
MissingAmount,
82+
/// An amount exceeded the maximum number of bitcoin.
83+
InvalidAmount,
8284
/// A currency was provided that is not supported.
8385
UnsupportedCurrency,
8486
/// A required description was not provided.

0 commit comments

Comments
 (0)