From e6b3bc7f1cad9243c9cd9e281e8520ac57f83847 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 29 Sep 2022 16:57:09 +0000 Subject: [PATCH] redo stuff some --- lightning/src/offers/offer.rs | 6 +-- lightning/src/util/ser_macros.rs | 68 ++++++++++++-------------------- 2 files changed, 29 insertions(+), 45 deletions(-) diff --git a/lightning/src/offers/offer.rs b/lightning/src/offers/offer.rs index b5efd6be..1e52ee0f 100644 --- a/lightning/src/offers/offer.rs +++ b/lightning/src/offers/offer.rs @@ -436,16 +436,16 @@ pub type CurrencyCode = [u8; 3]; tlv_stream!(OfferTlvStream, OfferTlvStreamRef, { (2, chains: (Vec, WithoutLength)), (4, metadata: (Vec, WithoutLength)), - (6, currency: CurrencyCode), + (6, currency: (CurrencyCode)), (8, amount: (u64, HighZeroBytesDroppedBigSize)), (10, description: (String, WithoutLength)), - (12, features: OfferFeatures), + (12, features: (OfferFeatures)), (14, absolute_expiry: (u64, HighZeroBytesDroppedBigSize)), (16, paths: (Vec, WithoutLength)), (18, issuer: (String, WithoutLength)), (20, quantity_min: (u64, HighZeroBytesDroppedBigSize)), (22, quantity_max: (u64, HighZeroBytesDroppedBigSize)), - (24, node_id: PublicKey), + (24, node_id: (PublicKey)), }); #[cfg(test)] diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index f6799841..764cf139 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -26,14 +26,6 @@ macro_rules! encode_tlv { field.write($stream)?; } }; - ($stream: expr, $type: expr, $field: expr, (tlv_record, $fieldty:tt)) => { - if let Some(field) = $field { - let field: encoded_tlv_record_ref_type!($fieldty) = From::from(field); - BigSize($type).write($stream)?; - BigSize(field.serialized_length() as u64).write($stream)?; - field.write($stream)?; - } - }; } macro_rules! encode_tlv_stream { @@ -450,6 +442,22 @@ macro_rules! impl_writeable_tlv_based { } } +macro_rules! _tlv_stream_impl_writeable { + ($nameref:ident, { + $(($type:expr, $field:ident : ($fieldty: ty $(, $serwrapper: ident)?))),* $(,)* + }) => { + impl<'a> ::util::ser::Writeable for $nameref<'a> { + fn write(&self, writer: &mut W) -> Result<(), $crate::io::Error> { + encode_tlv_stream!(writer, { + $(($type, self.$field $(.map(|a| $serwrapper(a)))?, option)),* + }); + Ok(()) + } + } + } +} + + /// Defines a struct for a TLV stream and a similar struct using references for non-primitive types, /// implementing [`Readable`] for the former and [`Writeable`] for the latter. Useful as an /// intermediary format when reading or writing a type encoded as a TLV stream. Note that each field @@ -474,14 +482,7 @@ macro_rules! tlv_stream { )* } - impl<'a> ::util::ser::Writeable for $nameref<'a> { - fn write(&self, writer: &mut W) -> Result<(), $crate::io::Error> { - encode_tlv_stream!(writer, { - $(($type, self.$field, (tlv_record, $fieldty))),* - }); - Ok(()) - } - } + _tlv_stream_impl_writeable!($nameref, {$(($type, $field: $fieldty)),*}); impl ::util::ser::Readable for $name { fn read(reader: &mut R) -> Result { @@ -506,25 +507,26 @@ macro_rules! tlv_record_type { (($type:ty, $wrapper:ident)) => { $type }; - ($type:ty) => { + (($type:ty)) => { $type }; } macro_rules! tlv_record_ref_type { - (u8) => { + ((u8)) => { u8 }; - (char) => { + ((char)) => { char }; - (($type:ty, HighZeroBytesDroppedBigSize)) => { - $type - }; + ((u8, $wrapper: ident)) => { u8 }; + ((u16, $wrapper: ident)) => { u16 }; + ((u32, $wrapper: ident)) => { u32 }; + ((u64, $wrapper: ident)) => { u64 }; (($type:ty, $wrapper:ident)) => { &'a $type }; - ($type:ty) => { + (($type:ty)) => { &'a $type }; } @@ -533,29 +535,11 @@ macro_rules! encoded_tlv_record_type { (($type:ty, $wrapper:ident)) => { $wrapper<$type> }; - ($type:ty) => { + (($type:ty)) => { $type }; } -macro_rules! encoded_tlv_record_ref_type { - (u8) => { - u8 - }; - (char) => { - char - }; - (($type:ty, HighZeroBytesDroppedBigSize)) => { - HighZeroBytesDroppedBigSize<$type> - }; - (($type:ty, $wrapper:ident)) => { - $wrapper<&$type> - }; - ($type:ty) => { - &$type - }; -} - macro_rules! _impl_writeable_tlv_based_enum_common { ($st: ident, $(($variant_id: expr, $variant_name: ident) => {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} -- 2.30.2