]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Use SemanticError in OfferBuilder::build
authorJeffrey Czyz <jkczyz@gmail.com>
Wed, 21 Sep 2022 18:09:06 +0000 (13:09 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Fri, 18 Nov 2022 17:33:06 +0000 (11:33 -0600)
lightning/src/offers/offer.rs
lightning/src/offers/parse.rs

index d941b69377cce51c262f0721a3f3da53d38fbbed..ebab6599f79ae6a7a0ac201ecbae96a169229d7f 100644 (file)
@@ -48,8 +48,7 @@
 //!     .issuer("Foo Bar".to_string())
 //!     .path(create_blinded_path())
 //!     .path(create_another_blinded_path())
-//!     .build()
-//!     .unwrap();
+//!     .build()?;
 //!
 //! // Encode as a bech32 string for use in a QR code.
 //! let encoded_offer = offer.to_string();
@@ -195,14 +194,14 @@ impl OfferBuilder {
        }
 
        /// Builds an [`Offer`] from the builder's settings.
-       pub fn build(mut self) -> Result<Offer, ()> {
+       pub fn build(mut self) -> Result<Offer, SemanticError> {
                match self.offer.amount {
                        Some(Amount::Bitcoin { amount_msats }) => {
                                if amount_msats > MAX_VALUE_MSAT {
-                                       return Err(());
+                                       return Err(SemanticError::InvalidAmount);
                                }
                        },
-                       Some(Amount::Currency { .. }) => unreachable!(),
+                       Some(Amount::Currency { .. }) => return Err(SemanticError::UnsupportedCurrency),
                        None => {},
                }
 
@@ -553,6 +552,7 @@ mod tests {
        use core::time::Duration;
        use crate::ln::features::OfferFeatures;
        use crate::ln::msgs::MAX_VALUE_MSAT;
+       use crate::offers::parse::SemanticError;
        use crate::onion_message::{BlindedHop, BlindedPath};
        use crate::util::ser::Writeable;
        use crate::util::string::PrintableString;
@@ -678,6 +678,10 @@ mod tests {
                assert_eq!(builder.offer.amount, Some(currency_amount.clone()));
                assert_eq!(tlv_stream.amount, Some(10));
                assert_eq!(tlv_stream.currency, Some(b"USD"));
+               match builder.build() {
+                       Ok(_) => panic!("expected error"),
+                       Err(e) => assert_eq!(e, SemanticError::UnsupportedCurrency),
+               }
 
                let offer = OfferBuilder::new("foo".into(), pubkey(42))
                        .amount(currency_amount.clone())
@@ -691,7 +695,7 @@ mod tests {
                let invalid_amount = Amount::Bitcoin { amount_msats: MAX_VALUE_MSAT + 1 };
                match OfferBuilder::new("foo".into(), pubkey(42)).amount(invalid_amount).build() {
                        Ok(_) => panic!("expected error"),
-                       Err(e) => assert_eq!(e, ()),
+                       Err(e) => assert_eq!(e, SemanticError::InvalidAmount),
                }
        }
 
index c9d568a9007aa09c6b90740f0c3ad75ed28ecd85..032e95b3cc52199a095c29a78c064abed2b6c40f 100644 (file)
@@ -98,6 +98,8 @@ pub enum SemanticError {
        MissingAmount,
        /// The amount exceeded the total bitcoin supply.
        InvalidAmount,
+       /// A currency was provided that is not supported.
+       UnsupportedCurrency,
        /// A required description was not provided.
        MissingDescription,
        /// A signing pubkey was not provided.