Skip to content

Commit

Permalink
binary working
Browse files Browse the repository at this point in the history
  • Loading branch information
zslayton committed Dec 5, 2023
1 parent b5edae9 commit e070328
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 113 deletions.
20 changes: 15 additions & 5 deletions src/lazy/encoder/annotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Annotated<'a, T: ?Sized, A> {
annotations: &'a [A],
}

/// Provides implementors with an extension method ([`annotate`](Annotate::annotate)) that allows
/// Provides implementors with an extension method ([`annotate`](Annotate::annotated_with)) that allows
/// them to be serialized with an associated sequence of annotations.
pub trait Annotate {
/// Pairs a reference to the provided value with a slice containing annotations.
Expand All @@ -24,7 +24,7 @@ pub trait Annotate {
/// let mut buffer = vec![];
/// let mut writer = LazyRawTextWriter_1_0::new(&mut buffer);
///
/// writer.write(42_usize.annotate(&["foo", "bar", "baz"]))?.flush()?;
/// writer.write(42_usize.annotated_with(&["foo", "bar", "baz"]))?.flush()?;
///
/// let expected = Element::read_one("foo::bar::baz::42")?;
/// let actual = Element::read_one(&buffer)?;
Expand All @@ -33,7 +33,7 @@ pub trait Annotate {
///# Ok(())
///# }
/// ```
fn annotate<'a, A: AsRawSymbolTokenRef>(
fn annotated_with<'a, A: AsRawSymbolTokenRef>(
&'a self,
annotations: &'a [A],
) -> Annotated<'a, Self, A>;
Expand All @@ -42,9 +42,9 @@ pub trait Annotate {
// Any Rust value that can be serialized as an Ion value can call `annotate`.
impl<T> Annotate for T
where
T: WriteAsIonValue,
T: ?Sized + WriteAsIonValue,
{
fn annotate<'a, A: AsRawSymbolTokenRef>(
fn annotated_with<'a, A: AsRawSymbolTokenRef>(
&'a self,
annotations: &'a [A],
) -> Annotated<'a, Self, A> {
Expand All @@ -67,3 +67,13 @@ where
self.value.write_as_ion_value(value_writer)
}
}

impl<'annotations, T, A> WriteAsIon for &Annotated<'annotations, T, A>
where
T: WriteAsIonValue,
A: AsRawSymbolTokenRef,
{
fn write_as_ion<V: AnnotatableValueWriter>(&self, writer: V) -> IonResult<()> {
(*self).write_as_ion(writer)
}
}
25 changes: 9 additions & 16 deletions src/lazy/encoder/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,10 @@ impl<'value, 'top> BinaryContainerWriter_1_0<'value, 'top> {
}
}

// pub(crate) fn write_values_by_ref<F>(&mut self, write_fn: F) -> IonResult<()>
// where
// F: for<'a> Fn(BinaryContainerValuesWriter_1_0<'a>) -> IonResult<BumpVec<'a, u8>>,
// {
// let container_values_writer = BinaryContainerValuesWriter_1_0::new(self.allocator);
// let encoded_values = write_fn(container_values_writer)?;
// self.write_header_and_encoded_body(encoded_values.as_slice())
// }

pub fn write_values<'a, F>(mut self, write_fn: F) -> IonResult<()>
where
'top: 'a,
F: Fn(BinaryContainerValuesWriter_1_0<'a>) -> IonResult<BumpVec<'a, u8>>,
F: FnOnce(BinaryContainerValuesWriter_1_0<'a>) -> IonResult<BumpVec<'a, u8>>,
{
let container_values_writer = BinaryContainerValuesWriter_1_0::new(self.allocator);
let encoded_values = write_fn(container_values_writer)?;
Expand Down Expand Up @@ -279,7 +270,7 @@ impl<'value, 'top> BinaryListWriter_1_0<'value, 'top> {
pub fn write_values<'a, F>(self, write_fn: F) -> IonResult<()>
where
'top: 'a,
F: Fn(&mut BinaryListValuesWriter_1_0<'a>) -> IonResult<()>,
F: FnOnce(&mut BinaryListValuesWriter_1_0<'a>) -> IonResult<()>,
{
self.container_writer
.write_values(|container_values_writer| {
Expand All @@ -306,11 +297,13 @@ impl<'value> BinarySExpValuesWriter_1_0<'value> {
}

impl<'value> MakeValueWriter for BinarySExpValuesWriter_1_0<'value> {
type ValueWriter<'a> = BinaryAnnotatableValueWriter_1_0<'a, 'a> where Self: 'a;
type ValueWriter<'a> = BinaryAnnotatableValueWriter_1_0<'a, 'value> where Self: 'a;

fn value_writer(&mut self) -> Self::ValueWriter<'_> {
todo!()
// this probably doesn't make sense?
BinaryAnnotatableValueWriter_1_0::new(
&*self.values_writer.allocator,
&mut self.values_writer.buffer,
)
}
}

Expand All @@ -336,7 +329,7 @@ impl<'value, 'top> BinarySExpWriter_1_0<'value, 'top> {
pub fn write_values<'a, F>(self, write_fn: F) -> IonResult<()>
where
'top: 'a,
F: Fn(&mut BinarySExpValuesWriter_1_0<'a>) -> IonResult<()>,
F: FnOnce(&mut BinarySExpValuesWriter_1_0<'a>) -> IonResult<()>,
{
self.container_writer
.write_values(|container_values_writer| {
Expand Down Expand Up @@ -407,7 +400,7 @@ impl<'value, 'top> BinaryStructWriter_1_0<'value, 'top> {
pub fn write_fields<'a, F>(self, write_fn: F) -> IonResult<()>
where
'top: 'a,
F: Fn(&mut BinaryStructFieldsWriter_1_0<'a>) -> IonResult<()>,
F: FnOnce(&mut BinaryStructFieldsWriter_1_0<'a>) -> IonResult<()>,
{
self.container_writer
.write_values(|container_values_writer| {
Expand Down
117 changes: 82 additions & 35 deletions src/lazy/encoder/binary/value_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,26 +268,30 @@ impl<'value, 'top> BinaryValueWriter_1_0<'value, 'top> {
)))
}

fn write_list<F: for<'a> Fn(&mut <Self as ValueWriter>::ListWriter<'a>) -> IonResult<()>>(
fn write_list<
F: for<'a> FnOnce(&mut <Self as ValueWriter>::ListWriter<'a>) -> IonResult<()>,
>(
mut self,
list_fn: F,
) -> IonResult<()> {
// let mut list_writer = self.list_writer()?;
self.list_writer().write_values(list_fn)
}
fn write_sexp<F: for<'a> Fn(&mut <Self as ValueWriter>::SExpWriter<'a>) -> IonResult<()>>(
fn write_sexp<
F: for<'a> FnOnce(&mut <Self as ValueWriter>::SExpWriter<'a>) -> IonResult<()>,
>(
mut self,
sexp_fn: F,
) -> IonResult<()> {
self.sexp_writer()?.write_values(&sexp_fn)
self.sexp_writer()?.write_values(sexp_fn)
}
fn write_struct<
F: for<'a> Fn(&mut <Self as ValueWriter>::StructWriter<'a>) -> IonResult<()>,
F: for<'a> FnOnce(&mut <Self as ValueWriter>::StructWriter<'a>) -> IonResult<()>,
>(
mut self,
struct_fn: F,
) -> IonResult<()> {
self.struct_writer()?.write_fields(&struct_fn)
self.struct_writer()?.write_fields(struct_fn)
// let mut struct_writer = self.struct_writer()?;
// struct_fn(&mut struct_writer)?;
// struct_writer.end()
Expand Down Expand Up @@ -315,16 +319,16 @@ impl<'value, 'top> ValueWriter for BinaryValueWriter_1_0<'value, 'top> {
fn write_symbol<A: AsRawSymbolTokenRef>(self, value: A) -> IonResult<()>;
fn write_clob<A: AsRef<[u8]>>(self, value: A) -> IonResult<()>;
fn write_blob<A: AsRef<[u8]>>(self, value: A) -> IonResult<()>;
fn write_list<F: for<'a> Fn(&mut Self::ListWriter<'a>) -> IonResult<()>>(
fn write_list<F: for<'a> FnOnce(&mut Self::ListWriter<'a>) -> IonResult<()>>(
self,
list_fn: F,
) -> IonResult<()>;
fn write_sexp<F: for<'a> Fn(&mut Self::SExpWriter<'a>) -> IonResult<()>>(
fn write_sexp<F: for<'a> FnOnce(&mut Self::SExpWriter<'a>) -> IonResult<()>>(
self,
sexp_fn: F,
) -> IonResult<()>;
fn write_struct<
F: for<'a> Fn(&mut Self::StructWriter<'a>) -> IonResult<()>,
F: for<'a> FnOnce(&mut Self::StructWriter<'a>) -> IonResult<()>,
>(
self,
struct_fn: F,
Expand Down Expand Up @@ -399,7 +403,7 @@ impl<'value, 'top, SymbolType: AsRawSymbolTokenRef>
{
fn encode_annotated<F>(self, encode_value_fn: F) -> IonResult<()>
where
F: for<'a> Fn(BinaryAnnotatedValueWriter_1_0<'a, 'top>) -> IonResult<()>,
F: for<'a> FnOnce(BinaryAnnotatedValueWriter_1_0<'a, 'top>) -> IonResult<()>,
{
let allocator = &*self.allocator;
let buffer = allocator.alloc_with(|| BumpVec::new_in(allocator));
Expand Down Expand Up @@ -516,23 +520,23 @@ impl<'value, 'top, SymbolType: AsRawSymbolTokenRef> ValueWriter
self.encode_annotated(|value_writer| value_writer.write_blob(&value))
}

fn write_list<F: for<'a> Fn(&mut Self::ListWriter<'a>) -> IonResult<()>>(
fn write_list<F: for<'a> FnOnce(&mut Self::ListWriter<'a>) -> IonResult<()>>(
self,
list_fn: F,
) -> IonResult<()> {
self.encode_annotated(move |value_writer| value_writer.write_list(&list_fn))
self.encode_annotated(move |value_writer| value_writer.write_list(list_fn))
}
fn write_sexp<F: for<'a> Fn(&mut Self::SExpWriter<'a>) -> IonResult<()>>(
fn write_sexp<F: for<'a> FnOnce(&mut Self::SExpWriter<'a>) -> IonResult<()>>(
self,
sexp_fn: F,
) -> IonResult<()> {
self.encode_annotated(|value_writer| value_writer.write_sexp(&sexp_fn))
self.encode_annotated(|value_writer| value_writer.write_sexp(sexp_fn))
}
fn write_struct<F: for<'a> Fn(&mut Self::StructWriter<'a>) -> IonResult<()>>(
fn write_struct<F: for<'a> FnOnce(&mut Self::StructWriter<'a>) -> IonResult<()>>(
self,
struct_fn: F,
) -> IonResult<()> {
self.encode_annotated(|value_writer| value_writer.write_struct(&struct_fn))
self.encode_annotated(|value_writer| value_writer.write_struct(struct_fn))
}
}

Expand Down Expand Up @@ -577,16 +581,16 @@ impl<'value, 'top: 'value> ValueWriter for BinaryAnnotatedValueWriter_1_0<'value
fn write_symbol<A: AsRawSymbolTokenRef>(mut self, value: A) -> IonResult<()>;
fn write_clob<A: AsRef<[u8]>>(mut self, value: A) -> IonResult<()>;
fn write_blob<A: AsRef<[u8]>>(mut self, value: A) -> IonResult<()>;
fn write_list<F: for<'a> Fn(&mut Self::ListWriter<'a>) -> IonResult<()>>(
fn write_list<F: for<'a> FnOnce(&mut Self::ListWriter<'a>) -> IonResult<()>>(
mut self,
list_fn: F,
) -> IonResult<()>;
fn write_sexp<F: for<'a> Fn(&mut Self::SExpWriter<'a>) -> IonResult<()>>(
fn write_sexp<F: for<'a> FnOnce(&mut Self::SExpWriter<'a>) -> IonResult<()>>(
mut self,
sexp_fn: F,
) -> IonResult<()>;
fn write_struct<
F: for<'a> Fn(&mut Self::StructWriter<'a>) -> IonResult<()>,
F: for<'a> FnOnce(&mut Self::StructWriter<'a>) -> IonResult<()>,
>(
mut self,
struct_fn: F,
Expand All @@ -598,7 +602,9 @@ impl<'value, 'top: 'value> ValueWriter for BinaryAnnotatedValueWriter_1_0<'value
mod tests {
use crate::lazy::encoder::annotate::Annotate;
use crate::lazy::encoder::binary::LazyRawBinaryWriter_1_0;
use crate::lazy::encoder::{AnnotatableValueWriter, LazyRawWriter, SequenceWriter};
use crate::lazy::encoder::{
AnnotatableValueWriter, LazyRawWriter, MakeValueWriter, SequenceWriter, ValueWriter,

Check warning on line 606 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, experimental)

unused import: `LazyRawWriter`

Check warning on line 606 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, experimental)

unused import: `SequenceWriter`

Check warning on line 606 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, all)

unused import: `LazyRawWriter`

Check warning on line 606 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, all)

unused import: `SequenceWriter`

Check warning on line 606 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

unused import: `LazyRawWriter`

Check warning on line 606 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

unused import: `SequenceWriter`
};
use crate::raw_symbol_token_ref::AsRawSymbolTokenRef;
use crate::{Element, IonData, IonResult, RawSymbolTokenRef, Timestamp};

Expand Down Expand Up @@ -801,13 +807,17 @@ mod tests {
"#;
let test = |writer: &mut LazyRawBinaryWriter_1_0<&mut Vec<u8>>| {
writer
.write(1.annotate(&[4]))?
.write(false.annotate(&[5]))?
.write(3f32.annotate(&[6, 7]))?
.write("foo".annotate(&[8, 5]))?
.write(4usize.as_raw_symbol_token_ref().annotate(&[1]))?
.write(Timestamp::with_ymd(2023, 11, 9).build()?.annotate(&[3]))?
.write((&[0xE0u8, 0x01, 0x00, 0xEA][..]).annotate(&[2]))?;
.write(1.annotated_with(&[4]))?
.write(false.annotated_with(&[5]))?
.write(3f32.annotated_with(&[6, 7]))?
.write("foo".annotated_with(&[8, 5]))?
.write(4usize.as_raw_symbol_token_ref().annotated_with(&[1]))?
.write(
Timestamp::with_ymd(2023, 11, 9)
.build()?
.annotated_with(&[3]),
)?
.write((&[0xE0u8, 0x01, 0x00, 0xEA][..]).annotated_with(&[2]))?;
Ok(())
};
writer_test(expected, test)
Expand All @@ -832,15 +842,52 @@ mod tests {
)
"#;
let test = |writer: &mut LazyRawBinaryWriter_1_0<&mut Vec<u8>>| {
writer // value_writer().without_annotations().write_list()
// .write_list(|list| Ok(()))?
.write(1.annotate(&[4]))?
.write(false.annotate(&[5]))?
.write(3f32.annotate(&[6, 7]))?
.write("foo".annotate(&[8, 5]))?
.write(4usize.as_raw_symbol_token_ref().annotate(&[1]))?
.write(Timestamp::with_ymd(2023, 11, 9).build()?.annotate(&[3]))?
.write((&[0xE0u8, 0x01, 0x00, 0xEA][..]).annotate(&[2]))?;
let empty_list: &[i32] = &[];
writer.write(empty_list);

Check warning on line 846 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, experimental)

unused `Result` that must be used

Check warning on line 846 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, all)

unused `Result` that must be used

Check warning on line 846 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

unused `Result` that must be used
writer.write(empty_list.annotated_with(&[4]))?;
writer.write([1, 2, 3].annotated_with(&[4]))?;
writer.write([1, 2, 3].annotated_with(&[4, 7]))?;
writer.write([[1, 2, 3].annotated_with(&[4, 7])].annotated_with(&[4, 7]))?;

// ()
writer.value_writer().write_sexp(|_sexp| Ok(()))?;

// $4::()
writer
.value_writer()
.with_annotations(&[4])
.write_sexp(|list| Ok(()))?;

Check warning on line 859 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, experimental)

unused variable: `list`

Check warning on line 859 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, all)

unused variable: `list`

// $4::(1 2 3)
writer
.value_writer()
.with_annotations(&[4])
.write_sexp(|sexp| {
sexp.write(1)?.write(2)?.write(3)?;
Ok(())
})?;

// $4::$7::()
writer
.value_writer()
.with_annotations(&[4, 7])
.write_sexp(|sexp| Ok(()))?;

Check warning on line 874 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, experimental)

unused variable: `sexp`

Check warning on line 874 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, all)

unused variable: `sexp`

Check warning on line 874 in src/lazy/encoder/binary/value_writer.rs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

unused variable: `sexp`

// $4::$7::(
// $4::$7::(1 2 3)
// )
writer
.value_writer()
.with_annotations(&[4, 7])
.write_sexp(|sexp| {
sexp.value_writer()
.with_annotations(&[4, 7])
.write_sexp(|inner_sexp| {
inner_sexp.write(1)?.write(2)?.write(3)?;
Ok(())
})?;
Ok(())
})?;
Ok(())
};
writer_test(expected, test)
Expand Down
Loading

0 comments on commit e070328

Please sign in to comment.