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

158 lines
6.5 KiB
Rust

use num;
use num::bigint::BigInt;
use std::io::Error;
use super::signed_integer::SignedIntegerRepr;
use super::value::{Domain, Value, NestedValue, IOValue, UnwrappedIOValue, Float, Double};
pub type Result<T> = std::result::Result<T, Error>;
pub trait Writer {
type Pointer;
type Annotation;
type Compound;
type Dictionary;
type StreamedAtom;
type KeyPointer;
fn supports_streaming(&self) -> bool;
fn align(&mut self, natural_chunksize: u64) -> Result<()>;
fn start_annotation(&mut self) -> Result<Self::Annotation>;
fn extend_annotation(&mut self, a: &mut Self::Annotation, annotation: &IOValue) -> Result<()>;
fn end_annotation(&mut self, a: Self::Annotation, value_p: Self::Pointer) -> Result<Self::Pointer>;
fn write_bool(&mut self, v: bool) -> Result<Self::Pointer>;
fn write_f32(&mut self, v: f32) -> Result<Self::Pointer>;
fn write_f64(&mut self, v: f64) -> Result<Self::Pointer>;
fn write_i8(&mut self, v: i8) -> Result<Self::Pointer>;
fn write_u8(&mut self, v: u8) -> Result<Self::Pointer>;
fn write_i16(&mut self, v: i16) -> Result<Self::Pointer>;
fn write_u16(&mut self, v: u16) -> Result<Self::Pointer>;
fn write_i32(&mut self, v: i32) -> Result<Self::Pointer>;
fn write_u32(&mut self, v: u32) -> Result<Self::Pointer>;
fn write_i64(&mut self, v: i64) -> Result<Self::Pointer>;
fn write_u64(&mut self, v: u64) -> Result<Self::Pointer>;
fn write_i128(&mut self, v: i128) -> Result<Self::Pointer>;
fn write_u128(&mut self, v: u128) -> Result<Self::Pointer>;
fn write_int(&mut self, v: &BigInt) -> Result<Self::Pointer>;
fn write_string(&mut self, v: &str) -> Result<Self::Pointer>;
fn write_bytes(&mut self, v: &[u8]) -> Result<Self::Pointer>;
fn write_symbol(&mut self, v: &str) -> Result<Self::Pointer>;
fn stream_string(&mut self) -> Result<Option<Self::StreamedAtom>>;
fn stream_bytes(&mut self) -> Result<Option<Self::StreamedAtom>>;
fn stream_symbol(&mut self) -> Result<Option<Self::StreamedAtom>>;
fn extend_atom(&mut self, s: &mut Self::StreamedAtom, bs: &[u8]) -> Result<()>;
fn end_atom(&mut self, s: Self::StreamedAtom) -> Result<Self::Pointer>;
fn start_record(&mut self, field_count: u64) -> Result<Self::Compound>;
fn start_sequence(&mut self, item_count: u64) -> Result<Self::Compound>;
fn start_set(&mut self, item_count: u64) -> Result<Self::Compound>;
fn stream_record(&mut self) -> Result<Option<Self::Compound>>;
fn stream_sequence(&mut self) -> Result<Option<Self::Compound>>;
fn stream_set(&mut self) -> Result<Option<Self::Compound>>;
fn extend_compound(&mut self, s: &mut Self::Compound, value_p: Self::Pointer) -> Result<()>;
fn end_compound(&mut self, s: Self::Compound) -> Result<Self::Pointer>;
fn start_dictionary(&mut self, entry_count: u64) -> Result<Self::Dictionary>;
fn stream_dictionary(&mut self) -> Result<Option<Self::Dictionary>>;
fn extend_dictionary_key(&mut self, s: &mut Self::Dictionary, key_p: Self::Pointer) -> Result<Self::KeyPointer>;
fn extend_dictionary_value(&mut self, s: &mut Self::Dictionary, key_p: Self::KeyPointer, value_p: Self::Pointer) -> Result<()>;
fn end_dictionary(&mut self, s: Self::Dictionary) -> Result<Self::Pointer>;
//---------------------------------------------------------------------------
fn write(&mut self, v: &IOValue) -> Result<Self::Pointer> {
match v.annotations().maybe_slice() {
None => self.write_value(v.value()),
Some(anns) => {
let mut a = self.start_annotation()?;
for ann in anns {
self.extend_annotation(&mut a, ann)?;
}
let p = self.write_value(v.value())?;
self.end_annotation(a, p)
}
}
}
fn write_value(&mut self, v: &UnwrappedIOValue) -> Result<Self::Pointer> {
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(ref n) => self.write_int(n),
}
Value::String(ref s) => self.write_string(s),
Value::ByteString(ref bs) => self.write_bytes(bs),
Value::Symbol(ref s) => self.write_symbol(s),
Value::Record(r) => {
let mut c = self.start_record(r.arity() as u64)?;
let p = self.write(r.label())?;
self.extend_compound(&mut c, p)?;
for f in r.fields() {
let p = self.write(f)?;
self.extend_compound(&mut c, p)?;
}
self.end_compound(c)
}
Value::Sequence(ref vs) => {
let mut c = self.start_sequence(vs.len() as u64)?;
for v in vs {
let p = self.write(v)?;
self.extend_compound(&mut c, p)?;
}
self.end_compound(c)
}
Value::Set(ref vs) => {
let mut c = self.start_set(vs.len() as u64)?;
for v in vs {
let p = self.write(v)?;
self.extend_compound(&mut c, p)?;
}
self.end_compound(c)
}
Value::Dictionary(ref vs) => {
let mut d = self.start_dictionary(vs.len() as u64)?;
for (k, v) in vs {
let p = self.write(k)?;
let kp = self.extend_dictionary_key(&mut d, p)?;
let p = self.write(v)?;
self.extend_dictionary_value(&mut d, kp, p)?;
}
self.end_dictionary(d)
}
Value::Domain(ref d) => self.write(&d.as_preserves()?)
}
}
}
pub fn bigvarint<W: std::io::Write>(w: &mut W, mut v: u64) -> Result<()> {
loop {
if v < (2 << 31) {
return w.write_all(&(v as u32).to_le_bytes());
} else {
w.write_all(&(((v & 0x7fffffff) + (2 << 31)) as u32).to_le_bytes())?;
v = v >> 31;
}
}
}
pub fn varint<W: std::io::Write>(w: &mut W, mut v: u64) -> Result<()> {
loop {
if v < 128 {
return w.write_all(&[v as u8])
} else {
w.write_all(&[((v & 0x7f) + 128) as u8])?;
v = v >> 7;
}
}
}