preserves/implementations/rust/preserves/src/value/writer.rs

169 lines
5.9 KiB
Rust

use num::bigint::BigInt;
use std::io;
use super::DomainEncode;
use super::boundary as B;
use super::signed_integer::SignedIntegerRepr;
use super::repr::{Value, NestedValue, Float, Double};
pub trait Writer: Sized {
fn start_annotations(&mut self) -> io::Result<()>;
fn end_annotations(&mut self) -> io::Result<()>;
fn write_bool(&mut self, v: bool) -> io::Result<()>;
fn write_f32(&mut self, v: f32) -> io::Result<()>;
fn write_f64(&mut self, v: f64) -> io::Result<()>;
fn write_i8(&mut self, v: i8) -> io::Result<()>;
fn write_u8(&mut self, v: u8) -> io::Result<()>;
fn write_i16(&mut self, v: i16) -> io::Result<()>;
fn write_u16(&mut self, v: u16) -> io::Result<()>;
fn write_i32(&mut self, v: i32) -> io::Result<()>;
fn write_u32(&mut self, v: u32) -> io::Result<()>;
fn write_i64(&mut self, v: i64) -> io::Result<()>;
fn write_u64(&mut self, v: u64) -> io::Result<()>;
fn write_i128(&mut self, v: i128) -> io::Result<()>;
fn write_u128(&mut self, v: u128) -> io::Result<()>;
fn write_int(&mut self, v: &BigInt) -> io::Result<()>;
fn write_string(&mut self, v: &str) -> io::Result<()>;
fn write_bytes(&mut self, v: &[u8]) -> io::Result<()>;
fn write_symbol(&mut self, v: &str) -> io::Result<()>;
fn boundary(&mut self, b: &B::Type) -> io::Result<()>;
fn start_record(&mut self) -> io::Result<()>;
fn end_record(&mut self) -> io::Result<()>;
fn start_sequence(&mut self) -> io::Result<()>;
fn end_sequence(&mut self) -> io::Result<()>;
fn start_set(&mut self) -> io::Result<()>;
fn end_set(&mut self) -> io::Result<()>;
fn start_dictionary(&mut self) -> io::Result<()>;
fn end_dictionary(&mut self) -> io::Result<()>;
fn start_embedded(&mut self) -> io::Result<()>;
fn end_embedded(&mut self) -> io::Result<()>;
fn flush(&mut self) -> io::Result<()>;
//---------------------------------------------------------------------------
#[inline(always)]
fn write<N: NestedValue, Enc: DomainEncode<N::Embedded>>(
&mut self,
enc: &mut Enc,
v: &N,
) -> io::Result<()> {
self.inner_write(enc, v)
}
fn inner_write<N: NestedValue, Enc: DomainEncode<N::Embedded>>(
&mut self,
enc: &mut Enc,
v: &N,
) -> io::Result<()> {
match v.annotations().maybe_slice() {
None => {
self.write_value(enc, v.value())?;
}
Some(anns) => {
self.start_annotations()?;
let mut b = B::Type::default();
for ann in anns {
b.shift(Some(B::Item::Annotation));
self.boundary(&b)?;
self.inner_write(enc, ann)?;
}
b.shift(Some(B::Item::AnnotatedValue));
self.boundary(&b)?;
self.write_value(enc, v.value())?;
b.shift(None);
self.boundary(&b)?;
self.end_annotations()?;
}
}
Ok(())
}
fn write_value<N: NestedValue, Enc: DomainEncode<N::Embedded>>(
&mut self,
enc: &mut Enc,
v: &Value<N>,
) -> io::Result<()> {
match v {
Value::Boolean(b) => self.write_bool(*b),
Value::Float(Float(f)) => self.write_f32(*f),
Value::Double(Double(d)) => self.write_f64(*d),
Value::SignedInteger(n) => match n.repr() {
SignedIntegerRepr::I128(i) => self.write_i128(*i),
SignedIntegerRepr::U128(u) => self.write_u128(*u),
SignedIntegerRepr::Big(n) => self.write_int(n),
}
Value::String(s) => self.write_string(s),
Value::ByteString(bs) => self.write_bytes(bs),
Value::Symbol(s) => self.write_symbol(s),
Value::Record(r) => {
self.start_record()?;
let mut b = B::start(B::Item::RecordLabel);
self.boundary(&b)?;
self.inner_write(enc, r.label())?;
for f in r.fields() {
b.shift(Some(B::Item::RecordField));
self.boundary(&b)?;
self.inner_write(enc, f)?;
}
b.shift(None);
self.boundary(&b)?;
self.end_record()
}
Value::Sequence(vs) => {
self.start_sequence()?;
let mut b = B::Type::default();
for v in vs {
b.shift(Some(B::Item::SequenceValue));
self.boundary(&b)?;
self.inner_write(enc, v)?;
}
b.shift(None);
self.boundary(&b)?;
self.end_sequence()
}
Value::Set(vs) => {
self.start_set()?;
let mut b = B::Type::default();
for v in vs {
b.shift(Some(B::Item::SetValue));
self.boundary(&b)?;
self.inner_write(enc, v)?;
}
b.shift(None);
self.boundary(&b)?;
self.end_set()
}
Value::Dictionary(vs) => {
self.start_dictionary()?;
let mut b = B::Type::default();
for (k, v) in vs {
b.shift(Some(B::Item::DictionaryKey));
self.boundary(&b)?;
self.inner_write(enc, k)?;
b.shift(Some(B::Item::DictionaryValue));
self.boundary(&b)?;
self.inner_write(enc, v)?;
}
b.shift(None);
self.boundary(&b)?;
self.end_dictionary()
}
Value::Embedded(d) => {
self.start_embedded()?;
enc.encode_embedded(self, d)?;
self.end_embedded()
}
}
}
}