+/// 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
+/// representing a TLV record has its type wrapped with an [`Option`]. A tuple consisting of a type
+/// and a serialization wrapper may be given in place of a type when custom serialization is
+/// required.
+///
+/// [`Readable`]: crate::util::ser::Readable
+/// [`Writeable`]: crate::util::ser::Writeable
+macro_rules! tlv_stream {
+ ($name:ident, $nameref:ident, {
+ $(($type:expr, $field:ident : $fieldty:tt)),* $(,)*
+ }) => {
+ #[derive(Debug)]
+ struct $name {
+ $(
+ $field: Option<tlv_record_type!($fieldty)>,
+ )*
+ }
+
+ pub(crate) struct $nameref<'a> {
+ $(
+ pub(crate) $field: Option<tlv_record_ref_type!($fieldty)>,
+ )*
+ }
+
+ impl<'a> $crate::util::ser::Writeable for $nameref<'a> {
+ fn write<W: $crate::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> {
+ encode_tlv_stream!(writer, {
+ $(($type, self.$field, (option, encoding: $fieldty))),*
+ });
+ Ok(())
+ }
+ }
+
+ impl $crate::util::ser::Readable for $name {
+ fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Self, $crate::ln::msgs::DecodeError> {
+ $(
+ init_tlv_field_var!($field, option);
+ )*
+ decode_tlv_stream!(reader, {
+ $(($type, $field, (option, encoding: $fieldty))),*
+ });
+
+ Ok(Self {
+ $(
+ $field: $field
+ ),*
+ })
+ }
+ }
+ }
+}
+
+macro_rules! tlv_record_type {
+ (($type:ty, $wrapper:ident)) => { $type };
+ ($type:ty) => { $type };
+}
+
+macro_rules! tlv_record_ref_type {
+ (char) => { char };
+ (u8) => { u8 };
+ ((u16, $wrapper: ident)) => { u16 };
+ ((u32, $wrapper: ident)) => { u32 };
+ ((u64, $wrapper: ident)) => { u64 };
+ (($type:ty, $wrapper:ident)) => { &'a $type };
+ ($type:ty) => { &'a $type };
+}
+