Skip to content

Commit

Permalink
Implements writing FlexSym annotation sequences (#711)
Browse files Browse the repository at this point in the history
* Moves annotation delegating macro to a central location
* Removes vestigial copy of the 1.0 binary writer impl
* Implements writing FlexSym annotation sequences
  • Loading branch information
zslayton committed Feb 13, 2024
1 parent aa2a29b commit 30f6edb
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 1,018 deletions.
19 changes: 19 additions & 0 deletions src/lazy/encoder/binary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
pub mod v1_0;
pub mod v1_1;

/// Takes a series of `TYPE => METHOD` pairs, generating a function for each that calls the host
/// type's `encode_annotated` method to encode an annotations sequence and then delegates encoding
/// the value to the corresponding value writer method.
// This macro is used in the v1_0 and v1_1 binary writer implementations, which both define an
// `encode_annotated` method. That method is not codified (for example: in a trait); this relies
// solely on convention between the two.
macro_rules! annotate_and_delegate {
// End of iteration
() => {};
// Recurses one argument pair at a time
($value_type:ty => $method:ident, $($rest:tt)*) => {
fn $method(self, value: $value_type) -> IonResult<()> {
self.encode_annotated(|value_writer| value_writer.$method(value))
}
annotate_and_delegate!($($rest)*);
};
}
pub(crate) use annotate_and_delegate;
17 changes: 2 additions & 15 deletions src/lazy/encoder/binary/v1_0/value_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::binary::timestamp::TimestampBinaryEncoder;
use crate::binary::uint;
use crate::binary::uint::DecodedUInt;
use crate::binary::var_uint::VarUInt;
use crate::lazy::encoder::binary::annotate_and_delegate;
use crate::lazy::encoder::binary::v1_0::container_writers::{
BinaryContainerWriter_1_0, BinaryListValuesWriter_1_0, BinaryListWriter_1_0,
BinarySExpValuesWriter_1_0, BinarySExpWriter_1_0, BinaryStructFieldsWriter_1_0,
Expand Down Expand Up @@ -463,20 +464,6 @@ impl<'value, 'top, SymbolType: AsRawSymbolTokenRef> Sealed
// No methods, precludes implementations outside the crate.
}

/// Takes a series of `TYPE => METHOD` pairs, generating a function for each that calls the
/// corresponding value writer method and then prefixes the encoded result with an annotations wrapper.
macro_rules! delegate_and_annotate {
// End of iteration
() => {};
// Recurses one argument pair at a time
($value_type:ty => $method:ident, $($rest:tt)*) => {
fn $method(self, value: $value_type) -> IonResult<()> {
self.encode_annotated(|value_writer| value_writer.$method(value))
}
delegate_and_annotate!($($rest)*);
};
}

impl<'value, 'top, SymbolType: AsRawSymbolTokenRef> ValueWriter
for BinaryAnnotationsWrapperWriter<'value, 'top, SymbolType>
{
Expand All @@ -485,7 +472,7 @@ impl<'value, 'top, SymbolType: AsRawSymbolTokenRef> ValueWriter

type StructWriter<'a> = BinaryStructFieldsWriter_1_0<'a>;

delegate_and_annotate!(
annotate_and_delegate!(
IonType => write_null,
bool => write_bool,
i64 => write_i64,
Expand Down
Loading

0 comments on commit 30f6edb

Please sign in to comment.