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

88 lines
3.7 KiB
Rust

use num;
use num::bigint::BigInt;
use std::io::Error;
use super::value::IOValue;
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>;
}
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;
}
}
}