De/serialize custom TLVs on `{Inbound,Outbound}OnionPayload`
[rust-lightning] / lightning / src / util / ser_macros.rs
index 1744b923d5e92589eee665c5a1ef1e263b6cfd8b..41185b179ffa815fee9d785fab9c18a2ab1711da 100644 (file)
@@ -132,6 +132,16 @@ macro_rules! _check_encoded_tlv_order {
 /// [`Writer`]: crate::util::ser::Writer
 #[macro_export]
 macro_rules! encode_tlv_stream {
+       ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => {
+               $crate::_encode_tlv_stream!($stream, {$(($type, $field, $fieldty)),*})
+       }
+}
+
+/// Implementation of [`encode_tlv_stream`].
+/// This is exported for use by other exported macros, do not use directly.
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _encode_tlv_stream {
        ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => { {
                #[allow(unused_imports)]
                use $crate::{
@@ -153,7 +163,21 @@ macro_rules! encode_tlv_stream {
                                $crate::_check_encoded_tlv_order!(last_seen, $type, $fieldty);
                        )*
                }
-       } }
+       } };
+       ($stream: expr, $tlvs: expr) => { {
+               for tlv in $tlvs {
+                       let (typ, value): &&(u64, Vec<u8>) = tlv;
+                       $crate::_encode_tlv!($stream, *typ, *value, required_vec);
+               }
+
+               #[cfg(debug_assertions)] {
+                       let mut last_seen: Option<u64> = None;
+                       for tlv in $tlvs {
+                               let (typ, _): &&(u64, Vec<u8>) = tlv;
+                               $crate::_check_encoded_tlv_order!(last_seen, *typ, required_vec);
+                       }
+               }
+       } };
 }
 
 /// Adds the length of the serialized field to a [`LengthCalculatingWriter`].
@@ -210,18 +234,27 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
 #[macro_export]
 macro_rules! _encode_varint_length_prefixed_tlv {
        ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}) => { {
+               _encode_varint_length_prefixed_tlv!($stream, {$(($type, $field, $fieldty)),*}, &[])
+       } };
+       ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}, $extra_tlvs: expr) => { {
                use $crate::util::ser::BigSize;
+               use alloc::vec::Vec;
                let len = {
                        #[allow(unused_mut)]
                        let mut len = $crate::util::ser::LengthCalculatingWriter(0);
                        $(
                                $crate::_get_varint_length_prefixed_tlv_length!(len, $type, $field, $fieldty);
                        )*
+                       for tlv in $extra_tlvs {
+                               let (typ, value): &&(u64, Vec<u8>) = tlv;
+                               $crate::_get_varint_length_prefixed_tlv_length!(len, *typ, *value, required_vec);
+                       }
                        len.0
                };
                BigSize(len as u64).write($stream)?;
-               $crate::encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* });
-       } }
+               $crate::_encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* });
+               $crate::_encode_tlv_stream!($stream, $extra_tlvs);
+       } };
 }
 
 /// Errors if there are missing required TLV types between the last seen type and the type currently being processed.
@@ -785,7 +818,8 @@ macro_rules! _init_and_read_tlv_fields {
 ///
 /// For example,
 /// ```
-/// # use lightning::impl_writeable_tlv_based;
+/// # use lightning::{impl_writeable_tlv_based, _encode_varint_length_prefixed_tlv};
+/// # extern crate alloc;
 /// struct LightningMessage {
 ///    tlv_integer: u32,
 ///    tlv_default_integer: u32,