@@ -241,47 +241,59 @@ impl TryFrom<OfferTlvStream> for Offer {
241
241
type Error = SemanticError ;
242
242
243
243
fn try_from ( tlv_stream : OfferTlvStream ) -> Result < Self , Self :: Error > {
244
- if tlv_stream. description . is_none ( ) {
245
- return Err ( SemanticError :: MissingDescription ) ;
246
- }
244
+ let OfferTlvStream {
245
+ chains, currency, amount, description, features, absolute_expiry, paths, issuer,
246
+ quantity_min, quantity_max, recurrence, node_id, send_invoice, refund_for, signature,
247
+ } = tlv_stream;
247
248
248
- if tlv_stream. node_id . is_none ( ) && tlv_stream. paths . is_none ( ) {
249
- return Err ( SemanticError :: MissingDestination ) ;
250
- }
251
-
252
- if tlv_stream. node_id . is_some ( ) && tlv_stream. paths . is_some ( ) {
253
- return Err ( SemanticError :: DuplicateDestination ) ;
249
+ if description. is_none ( ) {
250
+ return Err ( SemanticError :: MissingDescription ) ;
254
251
}
255
252
256
- if let Some ( WithoutLength ( ref paths) ) = tlv_stream. paths {
257
- if paths. is_empty ( ) {
253
+ let destination = match ( node_id, paths) {
254
+ ( None , None ) => return Err ( SemanticError :: MissingDestination ) ,
255
+ ( Some ( _) , Some ( _) ) => return Err ( SemanticError :: DuplicateDestination ) ,
256
+ ( Some ( node_id) , None ) => Destination :: NodeId ( node_id) ,
257
+ ( None , Some ( WithoutLength ( paths) ) ) if paths. is_empty ( ) => {
258
258
return Err ( SemanticError :: MissingPaths ) ;
259
- }
260
- }
259
+ } ,
260
+ ( None , Some ( WithoutLength ( paths) ) ) => Destination :: Paths ( paths) ,
261
+ } ;
261
262
262
- if let Some ( HighZeroBytesDroppedVarInt ( quantity_min) ) = tlv_stream . quantity_min {
263
+ if let Some ( HighZeroBytesDroppedVarInt ( quantity_min) ) = quantity_min {
263
264
if quantity_min < 1 {
264
265
return Err ( SemanticError :: InvalidQuantity ) ;
265
266
}
266
267
267
- if let Some ( HighZeroBytesDroppedVarInt ( quantity_max) ) = tlv_stream . quantity_max {
268
+ if let Some ( HighZeroBytesDroppedVarInt ( quantity_max) ) = quantity_max {
268
269
if quantity_min > quantity_max {
269
270
return Err ( SemanticError :: InvalidQuantity ) ;
270
271
}
271
272
}
272
273
}
273
274
274
- if let Some ( HighZeroBytesDroppedVarInt ( quantity_max) ) = tlv_stream . quantity_max {
275
+ if let Some ( HighZeroBytesDroppedVarInt ( quantity_max) ) = quantity_max {
275
276
if quantity_max < 1 {
276
277
return Err ( SemanticError :: InvalidQuantity ) ;
277
278
}
278
279
}
279
280
280
- if tlv_stream . refund_for . is_some ( ) && tlv_stream . send_invoice . is_none ( ) {
281
+ if refund_for. is_some ( ) && send_invoice. is_none ( ) {
281
282
return Err ( SemanticError :: UnexpectedRefund ) ;
282
283
}
283
284
284
- Ok ( Offer { tlv_stream } )
285
+
286
+ Ok ( Offer {
287
+ description : description. unwrap ( ) . 0 ,
288
+ features,
289
+ absolute_expiry :
290
+ absolute_expiry. map ( |seconds_from_epoch| Duration :: from_secs ( seconds_from_epoch. 0 ) ) ,
291
+ issuer : issuer. map ( |issuer| issuer. 0 ) ,
292
+ destination,
293
+ quantity_min : quantity_min. map ( |quantity_min| quantity_min. 0 ) ,
294
+ quantity_max : quantity_max. map ( |quantity_max| quantity_max. 0 ) ,
295
+ signature,
296
+ } )
285
297
}
286
298
}
287
299
0 commit comments