Skip to content

Commit

Permalink
Moves annotation delegating macro to a central location
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack Slayton committed Feb 13, 2024
1 parent aa2a29b commit a95864d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 70 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
76 changes: 21 additions & 55 deletions src/lazy/encoder/binary/v1_1/value_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use delegate::delegate;
use num_bigint::BigInt;
use num_traits::ToPrimitive;

use crate::lazy::encoder::binary::annotate_and_delegate;
use crate::lazy::encoder::binary::v1_1::container_writers::{
BinaryContainerWriter_1_1, BinaryListWriter_1_1, BinarySExpWriter_1_1, BinaryStructWriter_1_1,
};
Expand Down Expand Up @@ -882,73 +883,38 @@ impl<'value, 'top, SymbolType: AsRawSymbolTokenRef> ValueWriter
type SExpWriter<'a> = BinarySExpWriter_1_1<'value, 'top>;
type StructWriter<'a> = BinaryStructWriter_1_1<'value, 'top>;

fn write_null(self, _ion_type: IonType) -> IonResult<()> {
todo!()
}

fn write_bool(self, _value: bool) -> IonResult<()> {
todo!()
}

fn write_i64(self, _value: i64) -> IonResult<()> {
todo!()
}

fn write_int(self, _value: &Int) -> IonResult<()> {
todo!()
}

fn write_f32(self, _value: f32) -> IonResult<()> {
todo!()
}

fn write_f64(self, _value: f64) -> IonResult<()> {
todo!()
}

fn write_decimal(self, _value: &Decimal) -> IonResult<()> {
todo!()
}

fn write_timestamp(self, _value: &Timestamp) -> IonResult<()> {
todo!()
}

fn write_string(self, _value: impl AsRef<str>) -> IonResult<()> {
todo!()
}

fn write_symbol(self, _value: impl AsRawSymbolTokenRef) -> IonResult<()> {
todo!()
}

fn write_clob(self, _value: impl AsRef<[u8]>) -> IonResult<()> {
todo!()
}

fn write_blob(self, _value: impl AsRef<[u8]>) -> IonResult<()> {
todo!()
}
annotate_and_delegate!(
IonType => write_null,
bool => write_bool,
i64 => write_i64,
&Int => write_int,
f32 => write_f32,
f64 => write_f64,
&Decimal => write_decimal,
&Timestamp => write_timestamp,
impl AsRef<str> => write_string,
impl AsRawSymbolTokenRef => write_symbol,
impl AsRef<[u8]> => write_clob,
impl AsRef<[u8]> => write_blob,
);

fn write_list<F: for<'a> FnOnce(&mut Self::ListWriter<'a>) -> IonResult<()>>(
self,
_list_fn: F,
list_fn: F,
) -> IonResult<()> {
todo!()
self.encode_annotated(|value_writer| value_writer.write_list(list_fn))
}

fn write_sexp<F: for<'a> FnOnce(&mut Self::SExpWriter<'a>) -> IonResult<()>>(
self,
_sexp_fn: F,
sexp_fn: F,
) -> IonResult<()> {
todo!()
self.encode_annotated(|value_writer| value_writer.write_sexp(sexp_fn))
}

fn write_struct<F: for<'a> FnOnce(&mut Self::StructWriter<'a>) -> IonResult<()>>(
self,
_struct_fn: F,
struct_fn: F,
) -> IonResult<()> {
todo!()
self.encode_annotated(|value_writer| value_writer.write_struct(struct_fn))
}
}

Expand Down

0 comments on commit a95864d

Please sign in to comment.