Support deserializing an Option-al MaybeReadable

Prior to this change, our impl_writeable_tlv_based macros only supported
deserializing a MaybeReadable if it's non-Optional.
This commit is contained in:
Valentine Wallace 2023-02-13 16:49:22 -05:00
parent 2037a241f4
commit 52551a9fc8
No known key found for this signature in database
GPG key ID: FD3E106A2CE099B4

View file

@ -42,6 +42,9 @@ macro_rules! _encode_tlv {
($stream: expr, $type: expr, $field: expr, ignorable) => {
$crate::_encode_tlv!($stream, $type, $field, required);
};
($stream: expr, $type: expr, $field: expr, ignorable_option) => {
$crate::_encode_tlv!($stream, $type, $field, option);
};
($stream: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
$crate::_encode_tlv!($stream, $type, $field.map(|f| $encoding(f)), option);
};
@ -161,6 +164,9 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
($len: expr, $type: expr, $field: expr, ignorable) => {
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required);
};
($len: expr, $type: expr, $field: expr, ignorable_option) => {
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, option);
};
}
/// See the documentation of [`write_tlv_fields`].
@ -213,6 +219,9 @@ macro_rules! _check_decoded_tlv_order {
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ignorable) => {{
// no-op
}};
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ignorable_option) => {{
// no-op
}};
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
// no-op
}};
@ -252,6 +261,9 @@ macro_rules! _check_missing_tlv {
($last_seen_type: expr, $type: expr, $field: ident, ignorable) => {{
// no-op
}};
($last_seen_type: expr, $type: expr, $field: ident, ignorable_option) => {{
// no-op
}};
($last_seen_type: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
// no-op
}};
@ -283,6 +295,9 @@ macro_rules! _decode_tlv {
($reader: expr, $field: ident, ignorable) => {{
$field = $crate::util::ser::MaybeReadable::read(&mut $reader)?;
}};
($reader: expr, $field: ident, ignorable_option) => {{
$field = $crate::util::ser::MaybeReadable::read(&mut $reader)?;
}};
($reader: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
$field = Some($trait::read(&mut $reader $(, $read_arg)*)?);
}};
@ -623,6 +638,9 @@ macro_rules! _init_tlv_based_struct_field {
($field: ident, ignorable) => {
if $field.is_none() { return Ok(None); } else { $field.unwrap() }
};
($field: ident, ignorable_option) => {
$field
};
($field: ident, required) => {
$field.0.unwrap()
};
@ -655,6 +673,9 @@ macro_rules! _init_tlv_field_var {
($field: ident, ignorable) => {
let mut $field = None;
};
($field: ident, ignorable_option) => {
let mut $field = None;
};
}
/// Equivalent to running [`_init_tlv_field_var`] then [`read_tlv_fields`].