forked from syndicate-lang/preserves
Remove placeholders from spec and implementations 2/5
Enormous refactoring in Rust implementation. Direct deserialization. Zero-copy deserialization in some cases. Much faster.
This commit is contained in:
parent
e7ef060695
commit
b5f4c3a498
|
@ -16,3 +16,4 @@ num = "0.2"
|
||||||
num_enum = "0.4.1"
|
num_enum = "0.4.1"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_bytes = "0.11"
|
serde_bytes = "0.11"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
|
|
@ -7,3 +7,7 @@ clippy-watch:
|
||||||
|
|
||||||
inotifytest:
|
inotifytest:
|
||||||
inotifytest sh -c 'reset; cargo build && RUST_BACKTRACE=1 cargo test -- --nocapture'
|
inotifytest sh -c 'reset; cargo build && RUST_BACKTRACE=1 cargo test -- --nocapture'
|
||||||
|
|
||||||
|
debug-tests:
|
||||||
|
cargo test --no-run
|
||||||
|
gdb --args $$(cargo test 3>&1 1>&2 2>&3 3>&- | grep Running | awk '{print $$2}') --test-threads=1
|
||||||
|
|
|
@ -0,0 +1,338 @@
|
||||||
|
use serde::Deserialize;
|
||||||
|
use serde::de::{Visitor, SeqAccess, MapAccess, EnumAccess, VariantAccess, DeserializeSeed};
|
||||||
|
use std::borrow::Cow;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use super::value::reader::{Reader, BinaryReader, IOBinarySource, BytesBinarySource, CompoundBody};
|
||||||
|
|
||||||
|
pub use super::error::Error;
|
||||||
|
|
||||||
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
pub struct Deserializer<'de, 'r, R: Reader<'de>> {
|
||||||
|
pub read: &'r mut R,
|
||||||
|
phantom: PhantomData<&'de ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_bytes<'de, T>(bytes: &'de [u8]) ->
|
||||||
|
Result<T>
|
||||||
|
where
|
||||||
|
T: Deserialize<'de>
|
||||||
|
{
|
||||||
|
from_reader(&mut BinaryReader::new(BytesBinarySource::new(bytes)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_read<'de, 'r, IOR: std::io::Read, T>(read: &'r mut IOR) ->
|
||||||
|
Result<T>
|
||||||
|
where
|
||||||
|
T: Deserialize<'de>
|
||||||
|
{
|
||||||
|
from_reader(&mut BinaryReader::new(IOBinarySource::new(read)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_reader<'r, 'de, R: Reader<'de>, T>(read: &'r mut R) ->
|
||||||
|
Result<T>
|
||||||
|
where
|
||||||
|
T: Deserialize<'de>
|
||||||
|
{
|
||||||
|
let mut de = Deserializer::from_reader(read);
|
||||||
|
let t = T::deserialize(&mut de)?;
|
||||||
|
Ok(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'r, 'de, R: Reader<'de>> Deserializer<'de, 'r, R> {
|
||||||
|
pub fn from_reader(read: &'r mut R) -> Self {
|
||||||
|
Deserializer { read, phantom: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'r, 'de, 'a, R: Reader<'de>> serde::de::Deserializer<'de> for &'a mut Deserializer<'de, 'r, R>
|
||||||
|
{
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
// Won't support this here -- use value::de::Deserializer for this
|
||||||
|
Err(Error::CannotDeserializeAny)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_bool(self.read.next_boolean()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_i8(self.read.next_i8()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_i16(self.read.next_i16()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_i32(self.read.next_i32()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_i64(self.read.next_i64()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_u8(self.read.next_u8()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_u16(self.read.next_u16()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_u32(self.read.next_u32()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_u64(self.read.next_u64()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_f32(self.read.next_float()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_f64(self.read.next_double()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_char(self.read.next_char()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
match self.read.next_str()? {
|
||||||
|
Cow::Borrowed(s) => visitor.visit_borrowed_str(s),
|
||||||
|
Cow::Owned(s) => visitor.visit_str(&s),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
self.deserialize_str(visitor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
match self.read.next_bytestring()? {
|
||||||
|
Cow::Borrowed(bs) => visitor.visit_borrowed_bytes(bs),
|
||||||
|
Cow::Owned(bs) => visitor.visit_bytes(&bs),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_byte_buf(self.read.next_bytestring()?.into_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
let (is_some, mut compound_body) = self.read.open_option()?;
|
||||||
|
let result = if is_some {
|
||||||
|
compound_body.ensure_more_expected(self.read)?;
|
||||||
|
visitor.visit_some(&mut *self)?
|
||||||
|
} else {
|
||||||
|
visitor.visit_none::<Error>()?
|
||||||
|
};
|
||||||
|
compound_body.ensure_complete(self.read)?;
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
let mut compound_body = self.read.open_simple_record("tuple", Some(0))?;
|
||||||
|
let result = visitor.visit_unit::<Error>()?;
|
||||||
|
compound_body.ensure_complete(self.read)?;
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_unit_struct<V>(self, name: &'static str, visitor: V)
|
||||||
|
-> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
let mut compound_body = self.read.open_simple_record(name, Some(0))?;
|
||||||
|
let result = visitor.visit_unit::<Error>()?;
|
||||||
|
compound_body.ensure_complete(self.read)?;
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V)
|
||||||
|
-> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
match super::value::magic::transmit_input_value(
|
||||||
|
name, || Ok(self.read.demand_next(false)?.into_owned()))?
|
||||||
|
{
|
||||||
|
Some(v) => visitor.visit_u64(v),
|
||||||
|
None => {
|
||||||
|
let mut compound_body = self.read.open_simple_record(name, Some(1))?;
|
||||||
|
compound_body.ensure_more_expected(self.read)?;
|
||||||
|
let result = visitor.visit_newtype_struct(&mut *self)?;
|
||||||
|
compound_body.ensure_complete(self.read)?;
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de> {
|
||||||
|
// Hack around serde's model: Deserialize *sets* as sequences,
|
||||||
|
// too, and reconstruct them as Rust Sets on the visitor side.
|
||||||
|
let compound_body = self.read.open_sequence_or_set()?;
|
||||||
|
visitor.visit_seq(Seq::new(self, compound_body))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
let compound_body = self.read.open_simple_record("tuple", Some(len))?;
|
||||||
|
visitor.visit_seq(Seq::new(self, compound_body))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_tuple_struct<V>(self, name: &'static str, len: usize, visitor: V)
|
||||||
|
-> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
let compound_body = self.read.open_simple_record(name, Some(len))?;
|
||||||
|
visitor.visit_seq(Seq::new(self, compound_body))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de> {
|
||||||
|
let compound_body = self.read.open_dictionary()?;
|
||||||
|
visitor.visit_map(Seq::new(self, compound_body))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_struct<V>(self,
|
||||||
|
name: &'static str,
|
||||||
|
fields: &'static [&'static str],
|
||||||
|
visitor: V)
|
||||||
|
-> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
let compound_body = self.read.open_simple_record(name, Some(fields.len()))?;
|
||||||
|
visitor.visit_seq(Seq::new(self, compound_body))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_enum<V>(self,
|
||||||
|
_name: &'static str,
|
||||||
|
_variants: &'static [&'static str],
|
||||||
|
visitor: V)
|
||||||
|
-> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_enum(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
match self.read.next_symbol()? {
|
||||||
|
Cow::Borrowed(s) => visitor.visit_borrowed_str(s),
|
||||||
|
Cow::Owned(s) => visitor.visit_str(&s),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_none()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Seq<'de, 'r, 'a, R: Reader<'de>> {
|
||||||
|
de: &'a mut Deserializer<'de, 'r, R>,
|
||||||
|
compound_body: CompoundBody,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de, 'r, 'a, R: Reader<'de>> Seq<'de, 'r, 'a, R> {
|
||||||
|
fn new(de: &'a mut Deserializer<'de, 'r, R>, compound_body: CompoundBody) -> Self {
|
||||||
|
Seq { de, compound_body }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_item<T>(&mut self, seed: T) ->
|
||||||
|
Result<Option<T::Value>> where T: DeserializeSeed<'de>
|
||||||
|
{
|
||||||
|
match self.compound_body.more_expected(self.de.read)? {
|
||||||
|
false => Ok(None),
|
||||||
|
true => Ok(Some(seed.deserialize(&mut *self.de)?)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de, 'r, 'a, R: Reader<'de>> SeqAccess<'de> for Seq<'de, 'r, 'a, R> {
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn next_element_seed<T>(&mut self, seed: T) ->
|
||||||
|
Result<Option<T::Value>> where T: DeserializeSeed<'de>
|
||||||
|
{
|
||||||
|
self.next_item(seed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de, 'r, 'a, R: Reader<'de>> MapAccess<'de> for Seq<'de, 'r, 'a, R> {
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn next_key_seed<K>(&mut self, seed: K) ->
|
||||||
|
Result<Option<K::Value>> where K: DeserializeSeed<'de>
|
||||||
|
{
|
||||||
|
self.next_item(seed)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_value_seed<V>(&mut self, seed: V) ->
|
||||||
|
Result<V::Value> where V: DeserializeSeed<'de>
|
||||||
|
{
|
||||||
|
match self.next_item(seed)? {
|
||||||
|
Some(item) => Ok(item),
|
||||||
|
None => Err(Error::MissingItem),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de, 'r, 'a, R: Reader<'de>> EnumAccess<'de> for &'a mut Deserializer<'de, 'r, R> {
|
||||||
|
type Error = Error;
|
||||||
|
type Variant = Seq<'de, 'r, 'a, R>;
|
||||||
|
|
||||||
|
fn variant_seed<V>(self, seed: V)
|
||||||
|
-> Result<(V::Value, Self::Variant)> where V: DeserializeSeed<'de>
|
||||||
|
{
|
||||||
|
let mut compound_body = self.read.open_record(None)?;
|
||||||
|
compound_body.ensure_more_expected(self.read)?;
|
||||||
|
let variant = seed.deserialize(&mut *self)?;
|
||||||
|
Ok((variant, Seq::new(self, compound_body)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de, 'r, 'a, R: Reader<'de>> VariantAccess<'de> for Seq<'de, 'r, 'a, R> {
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
fn unit_variant(mut self) -> Result<()> {
|
||||||
|
self.compound_body.ensure_complete(self.de.read)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn newtype_variant_seed<T>(mut self, seed: T) -> Result<T::Value> where T: DeserializeSeed<'de> {
|
||||||
|
match self.next_item(seed)? {
|
||||||
|
None => Err(Error::MissingItem),
|
||||||
|
Some(v) => {
|
||||||
|
self.compound_body.ensure_complete(self.de.read)?;
|
||||||
|
Ok(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value> where V: Visitor<'de> {
|
||||||
|
visitor.visit_seq(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn struct_variant<V>(self, _fields: &'static [&'static str], visitor: V)
|
||||||
|
-> Result<V::Value> where V: Visitor<'de>
|
||||||
|
{
|
||||||
|
visitor.visit_seq(self)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,30 +1,135 @@
|
||||||
|
use num::bigint::BigInt;
|
||||||
|
use std::convert::From;
|
||||||
|
use crate::value::IOValue;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Error {
|
pub enum Error {
|
||||||
inner: std::io::Error,
|
Io(std::io::Error),
|
||||||
|
Message(String),
|
||||||
|
InvalidUnicodeScalar(u32),
|
||||||
|
NumberOutOfRange(BigInt),
|
||||||
|
CannotDeserializeAny,
|
||||||
|
MissingCloseDelimiter,
|
||||||
|
MissingItem,
|
||||||
|
Expected(ExpectedKind, Received),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::convert::From<Error> for std::io::Error {
|
#[derive(Debug)]
|
||||||
fn from(e: Error) -> Self {
|
pub enum Received {
|
||||||
e.inner
|
ReceivedSomethingElse,
|
||||||
|
ReceivedRecordWithLabel(String),
|
||||||
|
ReceivedOtherValue(IOValue),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum ExpectedKind {
|
||||||
|
Boolean,
|
||||||
|
Float,
|
||||||
|
Double,
|
||||||
|
|
||||||
|
SignedInteger,
|
||||||
|
String,
|
||||||
|
ByteString,
|
||||||
|
Symbol,
|
||||||
|
|
||||||
|
Record(Option<usize>),
|
||||||
|
SimpleRecord(&'static str, Option<usize>),
|
||||||
|
Sequence,
|
||||||
|
Set,
|
||||||
|
Dictionary,
|
||||||
|
|
||||||
|
SequenceOrSet, // Because of hacking up serde's data model: see open_sequence_or_set etc.
|
||||||
|
|
||||||
|
Option,
|
||||||
|
UnicodeScalar,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::io::Error> for Error {
|
||||||
|
fn from(e: std::io::Error) -> Self {
|
||||||
|
Error::Io(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::convert::From<std::io::Error> for Error {
|
impl From<Error> for std::io::Error {
|
||||||
fn from(inner: std::io::Error) -> Self {
|
fn from(e: Error) -> Self {
|
||||||
Error { inner }
|
match e {
|
||||||
|
Error::Io(ioe) => ioe,
|
||||||
|
Error::Message(str) => std::io::Error::new(std::io::ErrorKind::Other, str),
|
||||||
|
_ => std::io::Error::new(std::io::ErrorKind::Other, e.to_string()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::ser::Error for Error {
|
impl serde::ser::Error for Error {
|
||||||
fn custom<T: std::fmt::Display>(msg: T) -> Self {
|
fn custom<T: std::fmt::Display>(msg: T) -> Self {
|
||||||
Error { inner: std::io::Error::new(std::io::ErrorKind::Other, msg.to_string()) }
|
Self::Message(msg.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Error {
|
impl serde::de::Error for Error {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
|
fn custom<T: std::fmt::Display>(msg: T) -> Self {
|
||||||
self.inner.fmt(f)
|
Self::Message(msg.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for Error {}
|
impl std::error::Error for Error {}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{:?}", self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
pub fn is_io_error(e: &Error) -> bool {
|
||||||
|
if let Error::Io(_) = e { true } else { false }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn eof() -> Error {
|
||||||
|
Error::Io(io_eof())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_eof_error(e: &Error) -> bool {
|
||||||
|
if let Error::Io(ioe) = e {
|
||||||
|
is_eof_io_error(ioe)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn syntax_error(s: &str) -> Error {
|
||||||
|
Error::Io(io_syntax_error(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_syntax_error(e: &Error) -> bool {
|
||||||
|
if let Error::Io(ioe) = e {
|
||||||
|
is_syntax_io_error(ioe)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
pub fn io_eof() -> std::io::Error {
|
||||||
|
std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "EOF")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_eof_io_error(e: &std::io::Error) -> bool {
|
||||||
|
match e.kind() {
|
||||||
|
std::io::ErrorKind::UnexpectedEof => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn io_syntax_error(s: &str) -> std::io::Error {
|
||||||
|
std::io::Error::new(std::io::ErrorKind::InvalidData, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_syntax_io_error(e: &std::io::Error) -> bool {
|
||||||
|
match e.kind() {
|
||||||
|
std::io::ErrorKind::InvalidData => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,13 +1,18 @@
|
||||||
|
#[macro_use]
|
||||||
|
extern crate lazy_static;
|
||||||
|
|
||||||
pub mod de;
|
pub mod de;
|
||||||
pub mod ser;
|
pub mod ser;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
pub mod set;
|
||||||
pub mod symbol;
|
pub mod symbol;
|
||||||
pub mod value;
|
pub mod value;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod dom {
|
mod dom {
|
||||||
use super::value::{
|
use super::value::{
|
||||||
Value, IOValue, NestedValue, PlainValue, Domain, Codec,
|
Value, IOValue, NestedValue, PlainValue, Domain,
|
||||||
|
encoder::encode_bytes,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Hash, Clone, Ord, PartialEq, Eq, PartialOrd)]
|
#[derive(Debug, Hash, Clone, Ord, PartialEq, Eq, PartialOrd)]
|
||||||
|
@ -31,7 +36,7 @@ mod dom {
|
||||||
Value::Domain(Dom::One).wrap(),
|
Value::Domain(Dom::One).wrap(),
|
||||||
Value::from(2).wrap()])
|
Value::from(2).wrap()])
|
||||||
.wrap();
|
.wrap();
|
||||||
assert_eq!(Codec::without_placeholders().encode_bytes(&v.to_io_value()).unwrap(),
|
assert_eq!(encode_bytes(&v.to_io_value()).unwrap(),
|
||||||
[147, 49, 100, 255, 255, 255, 255, 50]);
|
[147, 49, 100, 255, 255, 255, 255, 50]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +45,7 @@ mod dom {
|
||||||
Value::Domain(Dom::Two).wrap(),
|
Value::Domain(Dom::Two).wrap(),
|
||||||
Value::from(2).wrap()])
|
Value::from(2).wrap()])
|
||||||
.wrap();
|
.wrap();
|
||||||
assert_eq!(Codec::without_placeholders().encode_bytes(&v.to_io_value()).unwrap(),
|
assert_eq!(encode_bytes(&v.to_io_value()).unwrap(),
|
||||||
[147, 49, 120, 68, 111, 109, 58, 58, 84, 119, 111, 50]);
|
[147, 49, 120, 68, 111, 109, 58, 58, 84, 119, 111, 50]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,14 +232,30 @@ mod value_tests {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod decoder_tests {
|
mod decoder_tests {
|
||||||
use crate::value::{Decoder, BinaryReader};
|
use crate::value::{Value, NestedValue, decoder};
|
||||||
use crate::value::{Value, NestedValue};
|
use crate::de::from_bytes;
|
||||||
|
use crate::error::{Error, ExpectedKind, is_eof_io_error};
|
||||||
|
|
||||||
|
fn expect_number_out_of_range<T: core::fmt::Debug>(r: Result<T, Error>) {
|
||||||
|
match r {
|
||||||
|
Ok(v) => panic!("Expected NumberOutOfRange, but got a parse of {:?}", v),
|
||||||
|
Err(Error::NumberOutOfRange(_)) => (),
|
||||||
|
Err(e) => panic!("Expected NumberOutOfRange, but got an error of {:?}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expect_expected<T: core::fmt::Debug>(k: ExpectedKind, r: Result<T, Error>) {
|
||||||
|
match r {
|
||||||
|
Ok(v) => panic!("Expected Expected({:?}), but got a parse of {:?}", k, v),
|
||||||
|
Err(Error::Expected(k1, _)) if k1 == k => (),
|
||||||
|
Err(e) => panic!("Expected Expected({:?}, but got an error of {:?}", k, e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test] fn skip_annotations_noskip() {
|
#[test] fn skip_annotations_noskip() {
|
||||||
let mut buf = &b"\x0521"[..];
|
let mut buf = &b"\x0521"[..];
|
||||||
let r = BinaryReader::new(&mut buf);
|
let mut d = decoder::from_bytes(&mut buf);
|
||||||
let mut d = Decoder::new(r, None);
|
let v = d.demand_next().unwrap();
|
||||||
let v = d.next_or_err().unwrap();
|
|
||||||
assert_eq!(v.annotations().len(), 1);
|
assert_eq!(v.annotations().len(), 1);
|
||||||
assert_eq!(v.annotations()[0], Value::from(2).wrap());
|
assert_eq!(v.annotations()[0], Value::from(2).wrap());
|
||||||
assert_eq!(v.value(), &Value::from(1));
|
assert_eq!(v.value(), &Value::from(1));
|
||||||
|
@ -242,54 +263,248 @@ mod decoder_tests {
|
||||||
|
|
||||||
#[test] fn skip_annotations_skip() {
|
#[test] fn skip_annotations_skip() {
|
||||||
let mut buf = &b"\x0521"[..];
|
let mut buf = &b"\x0521"[..];
|
||||||
let r = BinaryReader::new(&mut buf);
|
let mut d = decoder::from_bytes(&mut buf);
|
||||||
let mut d = Decoder::new(r, None);
|
|
||||||
d.set_read_annotations(false);
|
d.set_read_annotations(false);
|
||||||
let v = d.next_or_err().unwrap();
|
let v = d.demand_next().unwrap();
|
||||||
assert_eq!(v.annotations().len(), 0);
|
assert_eq!(v.annotations().len(), 0);
|
||||||
assert_eq!(v.value(), &Value::from(1));
|
assert_eq!(v.value(), &Value::from(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn two_values_at_once() {
|
#[test] fn multiple_values_buf_advanced() {
|
||||||
let mut buf = &b"\x81tPing\x81tPong"[..];
|
let mut buf = &b"\x81tPing\x81tPong"[..];
|
||||||
assert_eq!(buf.len(), 12);
|
assert_eq!(buf.len(), 12);
|
||||||
let r = BinaryReader::new(&mut buf);
|
let mut d = decoder::from_bytes(&mut buf);
|
||||||
let mut d = Decoder::new(r, None);
|
assert_eq!(d.read.source.index, 0);
|
||||||
assert_eq!(d.next_or_err().unwrap().value(), &Value::simple_record("Ping", vec![]));
|
assert_eq!(d.demand_next().unwrap().value(), &Value::simple_record("Ping", vec![]));
|
||||||
assert_eq!(d.next_or_err().unwrap().value(), &Value::simple_record("Pong", vec![]));
|
assert_eq!(d.read.source.index, 6);
|
||||||
assert_eq!(buf.len(), 0);
|
assert_eq!(d.demand_next().unwrap().value(), &Value::simple_record("Pong", vec![]));
|
||||||
|
assert_eq!(d.read.source.index, 12);
|
||||||
|
assert!(if let None = d.next() { true } else { false });
|
||||||
|
assert!(if let Err(e) = d.demand_next() { is_eof_io_error(&e) } else { false });
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn buf_advanced() {
|
#[test] fn direct_i8_format_a_positive() { assert_eq!(from_bytes::<i8>(b"1").unwrap(), 1) }
|
||||||
let mut buf = &b"\x81tPing\x81tPong"[..];
|
#[test] fn direct_i8_format_a_zero() { assert_eq!(from_bytes::<i8>(b"0").unwrap(), 0) }
|
||||||
assert_eq!(buf.len(), 12);
|
#[test] fn direct_i8_format_a_negative() { assert_eq!(from_bytes::<i8>(b"?").unwrap(), -1) }
|
||||||
let mut r = BinaryReader::new(&mut buf);
|
#[test] fn direct_i8_format_b() { assert_eq!(from_bytes::<i8>(b"A\xfe").unwrap(), -2) }
|
||||||
let mut d = Decoder::new(&mut r, None);
|
#[test] fn direct_i8_format_b_too_long() { assert_eq!(from_bytes::<i8>(b"C\xff\xff\xfe").unwrap(), -2) }
|
||||||
assert_eq!(d.next_or_err().unwrap().value(), &Value::simple_record("Ping", vec![]));
|
#[test] fn direct_i8_format_b_much_too_long() { assert_eq!(from_bytes::<i8>(b"J\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe").unwrap(), -2) }
|
||||||
assert_eq!(buf.len(), 6);
|
#[test] fn direct_i8_format_c() { assert_eq!(from_bytes::<i8>(b"$a\xfe\x04").unwrap(), -2) }
|
||||||
let mut r = BinaryReader::new(&mut buf);
|
#[test] fn direct_i8_format_c_too_long() { assert_eq!(from_bytes::<i8>(b"$a\xffa\xfe\x04").unwrap(), -2) }
|
||||||
let mut d = Decoder::new(&mut r, None);
|
#[test] fn direct_i8_format_c_much_too_long() { assert_eq!(from_bytes::<i8>(b"$a\xffa\xffa\xffa\xffa\xffa\xffa\xffa\xffa\xffa\xffa\xffa\xfe\x04").unwrap(), -2) }
|
||||||
assert_eq!(d.next_or_err().unwrap().value(), &Value::simple_record("Pong", vec![]));
|
|
||||||
assert_eq!(buf.len(), 0);
|
#[test] fn direct_u8_format_a_positive() { assert_eq!(from_bytes::<u8>(b"1").unwrap(), 1) }
|
||||||
|
#[test] fn direct_u8_format_a_zero() { assert_eq!(from_bytes::<u8>(b"0").unwrap(), 0) }
|
||||||
|
#[test] fn direct_u8_format_b() { assert_eq!(from_bytes::<u8>(b"A1").unwrap(), 49) }
|
||||||
|
#[test] fn direct_u8_format_b_too_long() { assert_eq!(from_bytes::<u8>(b"D\0\0\01").unwrap(), 49) }
|
||||||
|
#[test] fn direct_u8_format_b_much_too_long() { assert_eq!(from_bytes::<u8>(b"J\0\0\0\0\0\0\0\0\01").unwrap(), 49) }
|
||||||
|
#[test] fn direct_u8_format_c() { assert_eq!(from_bytes::<u8>(b"$a1\x04").unwrap(), 49) }
|
||||||
|
#[test] fn direct_u8_format_c_too_long() { assert_eq!(from_bytes::<u8>(b"$a\0a1\x04").unwrap(), 49) }
|
||||||
|
#[test] fn direct_u8_format_c_much_too_long() { assert_eq!(from_bytes::<u8>(b"$a\0a\0a\0a\0a\0a\0a\0a\0a\0a\0a\0a1\x04").unwrap(), 49) }
|
||||||
|
|
||||||
|
#[test] fn direct_i16_format_a() { assert_eq!(from_bytes::<i16>(b">").unwrap(), -2) }
|
||||||
|
#[test] fn direct_i16_format_b() { assert_eq!(from_bytes::<i16>(b"B\xfe\xff").unwrap(), -257) }
|
||||||
|
|
||||||
|
#[test] fn direct_u8_wrong_format() {
|
||||||
|
expect_expected(ExpectedKind::SignedInteger, from_bytes::<u8>(b"Ubogus"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test] fn direct_u8_format_b_too_large() {
|
||||||
|
expect_number_out_of_range(from_bytes::<u8>(b"D\0\011"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test] fn direct_u8_format_c_too_large() {
|
||||||
|
expect_number_out_of_range(from_bytes::<u8>(b"$a1a1\x04"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test] fn direct_i8_format_b_too_large() {
|
||||||
|
expect_number_out_of_range(from_bytes::<i8>(b"B\xfe\xff"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test] fn direct_i8_format_c_too_large() {
|
||||||
|
expect_number_out_of_range(from_bytes::<u8>(b"$a\xfea\xff\x04"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test] fn direct_i16_format_b_too_large() {
|
||||||
|
expect_number_out_of_range(from_bytes::<i16>(b"C\xfe\xff\xff"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test] fn direct_i32_format_b_ok() {
|
||||||
|
assert_eq!(from_bytes::<i32>(b"C\xfe\xff\xff").unwrap(), -65537);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test] fn direct_i32_format_b_ok_2() {
|
||||||
|
assert_eq!(from_bytes::<i32>(b"D\xfe\xff\xff\xff").unwrap(), -16777217);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test] fn direct_i64_format_b() {
|
||||||
|
assert_eq!(from_bytes::<i64>(b"A\xff").unwrap(), -1);
|
||||||
|
assert_eq!(from_bytes::<i64>(b"C\xff\xff\xff").unwrap(), -1);
|
||||||
|
assert_eq!(from_bytes::<i64>(b"J\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff").unwrap(), -1);
|
||||||
|
assert_eq!(from_bytes::<i64>(b"A\xfe").unwrap(), -2);
|
||||||
|
assert_eq!(from_bytes::<i64>(b"C\xff\xfe\xff").unwrap(), -257);
|
||||||
|
assert_eq!(from_bytes::<i64>(b"C\xfe\xff\xff").unwrap(), -65537);
|
||||||
|
assert_eq!(from_bytes::<i64>(b"J\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff").unwrap(), -16777217);
|
||||||
|
assert_eq!(from_bytes::<i64>(b"J\xff\xff\xfe\xff\xff\xff\xff\xff\xff\xff").unwrap(), -72057594037927937);
|
||||||
|
expect_number_out_of_range(from_bytes::<i64>(b"J\xff\xff\x0e\xff\xff\xff\xff\xff\xff\xff"));
|
||||||
|
expect_number_out_of_range(from_bytes::<i64>(b"I\xff\x0e\xff\xff\xff\xff\xff\xff\xff"));
|
||||||
|
expect_number_out_of_range(from_bytes::<i64>(b"I\x80\x0e\xff\xff\xff\xff\xff\xff\xff"));
|
||||||
|
expect_number_out_of_range(from_bytes::<i64>(b"J\xff\x00\x0e\xff\xff\xff\xff\xff\xff\xff"));
|
||||||
|
assert_eq!(from_bytes::<i64>(b"H\xfe\xff\xff\xff\xff\xff\xff\xff").unwrap(), -72057594037927937);
|
||||||
|
assert_eq!(from_bytes::<i64>(b"H\x0e\xff\xff\xff\xff\xff\xff\xff").unwrap(), 1080863910568919039);
|
||||||
|
assert_eq!(from_bytes::<i64>(b"H\x80\0\0\0\0\0\0\0").unwrap(), -9223372036854775808);
|
||||||
|
assert_eq!(from_bytes::<i64>(b"H\0\0\0\0\0\0\0\0").unwrap(), 0);
|
||||||
|
assert_eq!(from_bytes::<i64>(b"@").unwrap(), 0);
|
||||||
|
assert_eq!(from_bytes::<i64>(b"H\x7f\xff\xff\xff\xff\xff\xff\xff").unwrap(), 9223372036854775807);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test] fn direct_u64_format_b() {
|
||||||
|
expect_number_out_of_range(from_bytes::<u64>(b"A\xff"));
|
||||||
|
assert_eq!(from_bytes::<u64>(b"B\0\xff").unwrap(), 255);
|
||||||
|
expect_number_out_of_range(from_bytes::<u64>(b"C\xff\xff\xff"));
|
||||||
|
assert_eq!(from_bytes::<u64>(b"D\0\xff\xff\xff").unwrap(), 0xffffff);
|
||||||
|
expect_number_out_of_range(from_bytes::<u64>(b"J\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"));
|
||||||
|
assert_eq!(from_bytes::<u64>(b"A\x02").unwrap(), 2);
|
||||||
|
assert_eq!(from_bytes::<u64>(b"C\x00\x01\x00").unwrap(), 256);
|
||||||
|
assert_eq!(from_bytes::<u64>(b"C\x01\x00\x00").unwrap(), 65536);
|
||||||
|
assert_eq!(from_bytes::<u64>(b"J\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00").unwrap(), 16777216);
|
||||||
|
assert_eq!(from_bytes::<u64>(b"J\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00").unwrap(), 72057594037927936);
|
||||||
|
assert_eq!(from_bytes::<u64>(b"J\x00\x00\xf2\x00\x00\x00\x00\x00\x00\x00").unwrap(), 0xf200000000000000);
|
||||||
|
assert_eq!(from_bytes::<u64>(b"J\x00\x00\x72\x00\x00\x00\x00\x00\x00\x00").unwrap(), 0x7200000000000000);
|
||||||
|
expect_number_out_of_range(from_bytes::<u64>(b"J\x00\xf2\x00\x00\x00\x00\x00\x00\x00\x00"));
|
||||||
|
assert_eq!(from_bytes::<u64>(b"I\x00\xf2\x00\x00\x00\x00\x00\x00\x00").unwrap(), 0xf200000000000000);
|
||||||
|
expect_number_out_of_range(from_bytes::<u64>(b"I\x7f\xf2\x00\x00\x00\x00\x00\x00\x00"));
|
||||||
|
expect_number_out_of_range(from_bytes::<u64>(b"J\x00\xff\xf2\x00\x00\x00\x00\x00\x00\x00"));
|
||||||
|
assert_eq!(from_bytes::<u64>(b"H\x01\x00\x00\x00\x00\x00\x00\x00").unwrap(), 72057594037927936);
|
||||||
|
assert_eq!(from_bytes::<u64>(b"H\x0e\xff\xff\xff\xff\xff\xff\xff").unwrap(), 1080863910568919039);
|
||||||
|
expect_number_out_of_range(from_bytes::<u64>(b"H\x80\0\0\0\0\0\0\0"));
|
||||||
|
assert_eq!(from_bytes::<u64>(b"I\0\x80\0\0\0\0\0\0\0").unwrap(), 9223372036854775808);
|
||||||
|
assert_eq!(from_bytes::<u64>(b"H\0\0\0\0\0\0\0\0").unwrap(), 0);
|
||||||
|
assert_eq!(from_bytes::<u64>(b"@").unwrap(), 0);
|
||||||
|
assert_eq!(from_bytes::<u64>(b"H\x7f\xff\xff\xff\xff\xff\xff\xff").unwrap(), 9223372036854775807);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod serde_tests {
|
||||||
|
use crate::symbol::Symbol;
|
||||||
|
use crate::de::from_bytes as deserialize_from_bytes;
|
||||||
|
use crate::value::de::from_value as deserialize_from_value;
|
||||||
|
use crate::value::encoder::encode_bytes;
|
||||||
|
use crate::value::to_value;
|
||||||
|
use crate::value::{Value, IOValue, Map, Set};
|
||||||
|
|
||||||
|
#[test] fn simple_to_value() {
|
||||||
|
use serde::Serialize;
|
||||||
|
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||||
|
struct Colour{ red: u8, green: u8, blue: u8 }
|
||||||
|
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||||
|
struct SimpleValue<'a>(String,
|
||||||
|
#[serde(with = "crate::symbol")] String,
|
||||||
|
Symbol,
|
||||||
|
#[serde(with = "crate::symbol")] String,
|
||||||
|
Symbol,
|
||||||
|
&'a str,
|
||||||
|
#[serde(with = "serde_bytes")] &'a [u8],
|
||||||
|
#[serde(with = "serde_bytes")] Vec<u8>,
|
||||||
|
Vec<bool>,
|
||||||
|
#[serde(with = "crate::set")] Set<String>,
|
||||||
|
i16,
|
||||||
|
IOValue,
|
||||||
|
Map<String, Colour>,
|
||||||
|
f32,
|
||||||
|
f64);
|
||||||
|
let mut str_set = Set::new();
|
||||||
|
str_set.insert("one".to_owned());
|
||||||
|
str_set.insert("two".to_owned());
|
||||||
|
str_set.insert("three".to_owned());
|
||||||
|
let mut colours = Map::new();
|
||||||
|
colours.insert("red".to_owned(), Colour { red: 255, green: 0, blue: 0 });
|
||||||
|
colours.insert("green".to_owned(), Colour { red: 0, green: 255, blue: 0 });
|
||||||
|
colours.insert("blue".to_owned(), Colour { red: 0, green: 0, blue: 255 });
|
||||||
|
let v = SimpleValue("hello".to_string(),
|
||||||
|
"sym1".to_string(),
|
||||||
|
Symbol("sym2".to_string()),
|
||||||
|
"sym3".to_string(),
|
||||||
|
Symbol("sym4".to_string()),
|
||||||
|
"world",
|
||||||
|
&b"slice"[..],
|
||||||
|
b"vec".to_vec(),
|
||||||
|
vec![false, true, false, true],
|
||||||
|
str_set,
|
||||||
|
12345,
|
||||||
|
Value::from("hi").wrap(),
|
||||||
|
colours,
|
||||||
|
12.345,
|
||||||
|
12.3456789);
|
||||||
|
println!("== v: {:#?}", v);
|
||||||
|
let w: IOValue = to_value(&v);
|
||||||
|
println!("== w: {:#?}", w);
|
||||||
|
let x = deserialize_from_value(&w).unwrap();
|
||||||
|
println!("== x: {:#?}", &x);
|
||||||
|
assert_eq!(v, x);
|
||||||
|
|
||||||
|
let expected_bytes = vec![
|
||||||
|
0x8f, 0x10, // Struct, 15 members + 1 label
|
||||||
|
0x7b, 0x53, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x56, 0x61, 0x6c, 0x75, 0x65, // SimpleValue
|
||||||
|
0x55, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // "hello"
|
||||||
|
0x74, 0x73, 0x79, 0x6d, 0x31, // sym1
|
||||||
|
0x74, 0x73, 0x79, 0x6d, 0x32, // sym2
|
||||||
|
0x74, 0x73, 0x79, 0x6d, 0x33, // sym3
|
||||||
|
0x74, 0x73, 0x79, 0x6d, 0x34, // sym4
|
||||||
|
0x55, 0x77, 0x6f, 0x72, 0x6c, 0x64, // "world"
|
||||||
|
0x65, 0x73, 0x6c, 0x69, 0x63, 0x65, // #"slice"
|
||||||
|
0x63, 0x76, 0x65, 0x63, // #"vec"
|
||||||
|
0x94, // Sequence, 4 items
|
||||||
|
0x0, // false
|
||||||
|
0x1, // true
|
||||||
|
0x0, // false
|
||||||
|
0x1, // true
|
||||||
|
0xa3, // Set, 3 items
|
||||||
|
0x53, 0x6f, 0x6e, 0x65,
|
||||||
|
0x55, 0x74, 0x68, 0x72, 0x65, 0x65,
|
||||||
|
0x53, 0x74, 0x77, 0x6f,
|
||||||
|
0x42, 0x30, 0x39, // 12345
|
||||||
|
0x52, 0x68, 0x69, // "hi"
|
||||||
|
0xb6, // Dictionary, 6 items = 3 key/value pairs
|
||||||
|
0x54, 0x62, 0x6c, 0x75, 0x65, // "blue"
|
||||||
|
0x84, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x75, 0x72, 0x30, 0x30, 0x42, 0x00, 0xff,
|
||||||
|
0x55, 0x67, 0x72, 0x65, 0x65, 0x6e, // "green"
|
||||||
|
0x84, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x75, 0x72, 0x30, 0x42, 0x00, 0xff, 0x30,
|
||||||
|
0x53, 0x72, 0x65, 0x64, // "red"
|
||||||
|
0x84, 0x76, 0x43, 0x6f, 0x6c, 0x6f, 0x75, 0x72, 0x42, 0x00, 0xff, 0x30, 0x30,
|
||||||
|
|
||||||
|
0x2, 0x41, 0x45, 0x85, 0x1f, // 12.345,
|
||||||
|
0x3, 0x40, 0x28, 0xb0, 0xfc, 0xd3, 0x24, 0xd5, 0xa2, // 12.3456789
|
||||||
|
];
|
||||||
|
|
||||||
|
let v_bytes_1 = encode_bytes(&w).unwrap();
|
||||||
|
println!("== w bytes = {:?}", v_bytes_1);
|
||||||
|
assert_eq!(expected_bytes, v_bytes_1);
|
||||||
|
|
||||||
|
let mut v_bytes_2 = Vec::new();
|
||||||
|
v.serialize(&mut crate::ser::Serializer::new(&mut v_bytes_2)).unwrap();
|
||||||
|
println!("== v bytes = {:?}", v_bytes_2);
|
||||||
|
assert_eq!(v_bytes_1, v_bytes_2);
|
||||||
|
|
||||||
|
let y = deserialize_from_bytes(&v_bytes_1).unwrap();
|
||||||
|
println!("== y: {:#?}", &y);
|
||||||
|
assert_eq!(v, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod samples_tests {
|
mod samples_tests {
|
||||||
use crate::symbol::Symbol;
|
use crate::symbol::Symbol;
|
||||||
use crate::value::{Codec, Decoder, BinaryReader};
|
use crate::error::{is_eof_io_error, is_syntax_io_error};
|
||||||
use crate::value::reader::is_syntax_error;
|
use crate::value::de::from_value as deserialize_from_value;
|
||||||
use crate::value::{Value, IOValue, Map};
|
use crate::value::decoder;
|
||||||
use crate::value::DecodePlaceholderMap;
|
use crate::value::encoder::encode_bytes;
|
||||||
use crate::value::to_value;
|
use crate::value::{IOValue, Map};
|
||||||
use crate::value::from_value;
|
use std::iter::Iterator;
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
|
||||||
struct ExpectedPlaceholderMapping(DecodePlaceholderMap);
|
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||||
struct TestCases {
|
struct TestCases {
|
||||||
decode_placeholders: ExpectedPlaceholderMapping,
|
|
||||||
tests: Map<Symbol, TestCase>
|
tests: Map<Symbol, TestCase>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -301,49 +516,55 @@ mod samples_tests {
|
||||||
DecodeTest(#[serde(with = "serde_bytes")] Vec<u8>, IOValue),
|
DecodeTest(#[serde(with = "serde_bytes")] Vec<u8>, IOValue),
|
||||||
ParseError(String),
|
ParseError(String),
|
||||||
ParseShort(String),
|
ParseShort(String),
|
||||||
|
ParseEOF(String),
|
||||||
DecodeError(#[serde(with = "serde_bytes")] Vec<u8>),
|
DecodeError(#[serde(with = "serde_bytes")] Vec<u8>),
|
||||||
DecodeShort(#[serde(with = "serde_bytes")] Vec<u8>),
|
DecodeShort(#[serde(with = "serde_bytes")] Vec<u8>),
|
||||||
|
DecodeEOF(#[serde(with = "serde_bytes")] Vec<u8>),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode_all<'de>(bytes: &'de [u8]) -> Result<Vec<IOValue>, std::io::Error> {
|
||||||
|
let d = decoder::from_bytes(bytes);
|
||||||
|
d.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn run() -> std::io::Result<()> {
|
#[test] fn run() -> std::io::Result<()> {
|
||||||
let mut fh = std::fs::File::open("../../tests/samples.bin").unwrap();
|
let mut fh = std::fs::File::open("../../tests/samples.bin").unwrap();
|
||||||
let r = BinaryReader::new(&mut fh);
|
let mut d = decoder::from_read(&mut fh);
|
||||||
let mut d = Decoder::new(r, None);
|
let tests: TestCases = deserialize_from_value(&d.next().unwrap().unwrap()).unwrap();
|
||||||
let tests: TestCases = from_value(&d.next_or_err().unwrap()).unwrap();
|
|
||||||
// println!("{:#?}", tests);
|
// println!("{:#?}", tests);
|
||||||
|
|
||||||
let codec = Codec::new(tests.decode_placeholders.0);
|
|
||||||
for (Symbol(ref name), ref case) in tests.tests {
|
for (Symbol(ref name), ref case) in tests.tests {
|
||||||
println!("{:?} ==> {:?}", name, case);
|
println!("{:?} ==> {:?}", name, case);
|
||||||
match case {
|
match case {
|
||||||
TestCase::Test(ref bin, ref val) => {
|
TestCase::Test(ref bin, ref val) => {
|
||||||
assert_eq!(&codec.decode_all(&mut &codec.encode_bytes(val)?[..])?, &[val.clone()]);
|
assert_eq!(&decode_all(&encode_bytes(val)?[..])?, &[val.clone()]);
|
||||||
assert_eq!(&codec.decode_all(&mut &bin[..])?, &[val.clone()]);
|
assert_eq!(&decode_all(&bin[..])?, &[val.clone()]);
|
||||||
assert_eq!(&codec.encode_bytes(val)?, bin);
|
assert_eq!(&encode_bytes(val)?, bin);
|
||||||
}
|
}
|
||||||
TestCase::NondeterministicTest(ref bin, ref val) => {
|
TestCase::NondeterministicTest(ref bin, ref val) => {
|
||||||
// The test cases in samples.txt are carefully
|
// The test cases in samples.txt are carefully
|
||||||
// written so that while strictly
|
// written so that while strictly
|
||||||
// "nondeterministic", the order of keys in
|
// "nondeterministic", the order of keys in
|
||||||
// dictionaries follows Preserves order.
|
// dictionaries follows Preserves order.
|
||||||
assert_eq!(&codec.decode_all(&mut &codec.encode_bytes(val)?[..])?, &[val.clone()]);
|
assert_eq!(&decode_all(&encode_bytes(val)?[..])?, &[val.clone()]);
|
||||||
assert_eq!(&codec.decode_all(&mut &bin[..])?, &[val.clone()]);
|
assert_eq!(&decode_all(&bin[..])?, &[val.clone()]);
|
||||||
assert_eq!(&codec.encode_bytes(val)?, bin);
|
assert_eq!(&encode_bytes(val)?, bin);
|
||||||
}
|
}
|
||||||
TestCase::StreamingTest(ref bin, ref val) => {
|
TestCase::StreamingTest(ref bin, ref val) => {
|
||||||
assert_eq!(&codec.decode_all(&mut &codec.encode_bytes(val)?[..])?, &[val.clone()]);
|
assert_eq!(&decode_all(&encode_bytes(val)?[..])?, &[val.clone()]);
|
||||||
assert_eq!(&codec.decode_all(&mut &bin[..])?, &[val.clone()]);
|
assert_eq!(&decode_all(&bin[..])?, &[val.clone()]);
|
||||||
}
|
}
|
||||||
TestCase::DecodeTest(ref bin, ref val) => {
|
TestCase::DecodeTest(ref bin, ref val) => {
|
||||||
assert_eq!(&codec.decode_all(&mut &codec.encode_bytes(val)?[..])?, &[val.clone()]);
|
assert_eq!(&decode_all(&encode_bytes(val)?[..])?, &[val.clone()]);
|
||||||
assert_eq!(&codec.decode_all(&mut &bin[..])?, &[val.clone()]);
|
assert_eq!(&decode_all(&bin[..])?, &[val.clone()]);
|
||||||
}
|
}
|
||||||
TestCase::ParseError(_) => (),
|
TestCase::ParseError(_) => (),
|
||||||
TestCase::ParseShort(_) => (),
|
TestCase::ParseShort(_) => (),
|
||||||
|
TestCase::ParseEOF(_) => (),
|
||||||
TestCase::DecodeError(ref bin) => {
|
TestCase::DecodeError(ref bin) => {
|
||||||
match codec.decode_all(&mut &bin[..]) {
|
match decode_all(&bin[..]) {
|
||||||
Ok(_) => panic!("Unexpected success"),
|
Ok(_) => panic!("Unexpected success"),
|
||||||
Err(e) => if is_syntax_error(&e) {
|
Err(e) => if is_syntax_io_error(&e) {
|
||||||
()
|
()
|
||||||
} else {
|
} else {
|
||||||
panic!("Unexpected error {:?}", e)
|
panic!("Unexpected error {:?}", e)
|
||||||
|
@ -351,51 +572,17 @@ mod samples_tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TestCase::DecodeShort(ref bin) => {
|
TestCase::DecodeShort(ref bin) => {
|
||||||
assert_eq!(codec.decode_all(&mut &bin[..])?.len(), 0);
|
assert!(if let Err(e) = decoder::from_bytes(bin).next().unwrap() {
|
||||||
|
is_eof_io_error(&e)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
TestCase::DecodeEOF(ref bin) => {
|
||||||
|
assert!(if let None = decoder::from_bytes(bin).next() { true } else { false })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn simple_to_value() {
|
|
||||||
use serde::Serialize;
|
|
||||||
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
||||||
struct SimpleValue<'a>(String,
|
|
||||||
#[serde(with = "crate::symbol")] String,
|
|
||||||
Symbol,
|
|
||||||
#[serde(with = "crate::symbol")] String,
|
|
||||||
Symbol,
|
|
||||||
&'a str,
|
|
||||||
#[serde(with = "serde_bytes")] &'a [u8],
|
|
||||||
#[serde(with = "serde_bytes")] Vec<u8>,
|
|
||||||
i16,
|
|
||||||
IOValue);
|
|
||||||
let v = SimpleValue("hello".to_string(),
|
|
||||||
"sym1".to_string(),
|
|
||||||
Symbol("sym2".to_string()),
|
|
||||||
"sym3".to_string(),
|
|
||||||
Symbol("sym4".to_string()),
|
|
||||||
"world",
|
|
||||||
&b"slice"[..],
|
|
||||||
b"vec".to_vec(),
|
|
||||||
12345,
|
|
||||||
Value::from("hi").wrap());
|
|
||||||
println!("== v: {:#?}", v);
|
|
||||||
let w: IOValue = to_value(&v);
|
|
||||||
println!("== w: {:#?}", w);
|
|
||||||
let x = from_value(&w).unwrap();
|
|
||||||
println!("== x: {:#?}", &x);
|
|
||||||
assert_eq!(v, x);
|
|
||||||
let mut placeholders = Map::new();
|
|
||||||
placeholders.insert(5, Value::symbol("sym1"));
|
|
||||||
placeholders.insert(500, Value::symbol("sym2"));
|
|
||||||
placeholders.insert(0, Value::symbol("SimpleValue"));
|
|
||||||
let v_bytes_1 = Codec::new(placeholders.clone()).encode_bytes(&w).unwrap();
|
|
||||||
let mut v_bytes_2 = Vec::new();
|
|
||||||
v.serialize(&mut crate::ser::Serializer::new(&mut v_bytes_2, Some(&crate::value::invert_map(&placeholders)))).unwrap();
|
|
||||||
println!("== w bytes = {:?}", v_bytes_1);
|
|
||||||
println!("== v bytes = {:?}", v_bytes_2);
|
|
||||||
assert_eq!(v_bytes_1, v_bytes_2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use super::value::writer::Writer;
|
use super::value::writer::Writer;
|
||||||
use super::value::{Value, EncodePlaceholderMap, Encoder};
|
use super::value::Encoder;
|
||||||
|
|
||||||
pub use super::error::Error;
|
pub use super::error::Error;
|
||||||
type Result<T> = std::result::Result<T, Error>;
|
type Result<T> = std::result::Result<T, Error>;
|
||||||
|
@ -8,19 +8,11 @@ type Result<T> = std::result::Result<T, Error>;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Serializer<'a, W: Writer> {
|
pub struct Serializer<'a, W: Writer> {
|
||||||
pub write: &'a mut W,
|
pub write: &'a mut W,
|
||||||
placeholders: Option<&'a EncodePlaceholderMap>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, W: Writer> Serializer<'a, W> {
|
impl<'a, W: Writer> Serializer<'a, W> {
|
||||||
pub fn new(write: &'a mut W, placeholders: Option<&'a EncodePlaceholderMap>) -> Self {
|
pub fn new(write: &'a mut W) -> Self {
|
||||||
Serializer { write, placeholders }
|
Serializer { write }
|
||||||
}
|
|
||||||
|
|
||||||
fn write_symbol(&mut self, s: &str) -> Result<()> {
|
|
||||||
match self.placeholders.as_ref().and_then(|m| m.get(&Value::symbol(s))) {
|
|
||||||
Some(&n) => Ok(self.write.write_placeholder_ref(n)?),
|
|
||||||
None => Ok(self.write.write_symbol(s)?),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +79,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
||||||
|
|
||||||
fn serialize_char(self, v: char) -> Result<()> {
|
fn serialize_char(self, v: char) -> Result<()> {
|
||||||
self.write.open_record(1)?;
|
self.write.open_record(1)?;
|
||||||
self.write_symbol("UnicodeScalar")?;
|
self.write.write_symbol("UnicodeScalar")?;
|
||||||
self.write.write_u32(v as u32)?;
|
self.write.write_u32(v as u32)?;
|
||||||
Ok(self.write.close_record()?)
|
Ok(self.write.close_record()?)
|
||||||
}
|
}
|
||||||
|
@ -102,26 +94,26 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
||||||
|
|
||||||
fn serialize_none(self) -> Result<()> {
|
fn serialize_none(self) -> Result<()> {
|
||||||
self.write.open_record(0)?;
|
self.write.open_record(0)?;
|
||||||
self.write_symbol("None")?;
|
self.write.write_symbol("None")?;
|
||||||
Ok(self.write.close_record()?)
|
Ok(self.write.close_record()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_some<T: ?Sized>(self, v: &T) -> Result<()> where T: Serialize {
|
fn serialize_some<T: ?Sized>(self, v: &T) -> Result<()> where T: Serialize {
|
||||||
self.write.open_record(1)?;
|
self.write.open_record(1)?;
|
||||||
self.write_symbol("Some")?;
|
self.write.write_symbol("Some")?;
|
||||||
v.serialize(&mut *self)?;
|
v.serialize(&mut *self)?;
|
||||||
Ok(self.write.close_record()?)
|
Ok(self.write.close_record()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit(self) -> Result<()> {
|
fn serialize_unit(self) -> Result<()> {
|
||||||
self.write.open_record(0)?;
|
self.write.open_record(0)?;
|
||||||
self.write_symbol("tuple")?;
|
self.write.write_symbol("tuple")?;
|
||||||
Ok(self.write.close_record()?)
|
Ok(self.write.close_record()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_unit_struct(self, name: &'static str) -> Result<()> {
|
fn serialize_unit_struct(self, name: &'static str) -> Result<()> {
|
||||||
self.write.open_record(0)?;
|
self.write.open_record(0)?;
|
||||||
self.write_symbol(name)?;
|
self.write.write_symbol(name)?;
|
||||||
Ok(self.write.close_record()?)
|
Ok(self.write.close_record()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +124,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
||||||
Result<()>
|
Result<()>
|
||||||
{
|
{
|
||||||
self.write.open_record(0)?;
|
self.write.open_record(0)?;
|
||||||
self.write_symbol(variant_name)?;
|
self.write.write_symbol(variant_name)?;
|
||||||
Ok(self.write.close_record()?)
|
Ok(self.write.close_record()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,13 +133,13 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
||||||
{
|
{
|
||||||
match super::value::magic::receive_output_value(name, value) {
|
match super::value::magic::receive_output_value(name, value) {
|
||||||
Some(v) => {
|
Some(v) => {
|
||||||
Encoder::new(self.write, self.placeholders).write(&v)?;
|
Encoder::new(self.write).write(&v)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
// TODO: This is apparently discouraged, and we should apparently just serialize `value`?
|
// TODO: This is apparently discouraged, and we should apparently just serialize `value`?
|
||||||
self.write.open_record(1)?;
|
self.write.open_record(1)?;
|
||||||
self.write_symbol(name)?;
|
self.write.write_symbol(name)?;
|
||||||
value.serialize(&mut *self)?;
|
value.serialize(&mut *self)?;
|
||||||
Ok(self.write.close_record()?)
|
Ok(self.write.close_record()?)
|
||||||
}
|
}
|
||||||
|
@ -162,7 +154,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
||||||
Result<()> where T: Serialize
|
Result<()> where T: Serialize
|
||||||
{
|
{
|
||||||
self.write.open_record(1)?;
|
self.write.open_record(1)?;
|
||||||
self.write_symbol(variant_name)?;
|
self.write.write_symbol(variant_name)?;
|
||||||
value.serialize(&mut *self)?;
|
value.serialize(&mut *self)?;
|
||||||
Ok(self.write.close_record()?)
|
Ok(self.write.close_record()?)
|
||||||
}
|
}
|
||||||
|
@ -177,7 +169,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
||||||
|
|
||||||
fn serialize_tuple(self, count: usize) -> Result<Self::SerializeTuple> {
|
fn serialize_tuple(self, count: usize) -> Result<Self::SerializeTuple> {
|
||||||
self.write.open_record(count)?;
|
self.write.open_record(count)?;
|
||||||
self.write_symbol("tuple")?;
|
self.write.write_symbol("tuple")?;
|
||||||
Ok(SerializeCompound { ser: self, count: Some(count) })
|
Ok(SerializeCompound { ser: self, count: Some(count) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,7 +177,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
||||||
Result<Self::SerializeTupleStruct>
|
Result<Self::SerializeTupleStruct>
|
||||||
{
|
{
|
||||||
self.write.open_record(count)?;
|
self.write.open_record(count)?;
|
||||||
self.write_symbol(name)?;
|
self.write.write_symbol(name)?;
|
||||||
Ok(SerializeCompound { ser: self, count: Some(count) })
|
Ok(SerializeCompound { ser: self, count: Some(count) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,7 +189,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
||||||
Result<Self::SerializeTupleVariant>
|
Result<Self::SerializeTupleVariant>
|
||||||
{
|
{
|
||||||
self.write.open_record(count)?;
|
self.write.open_record(count)?;
|
||||||
self.write_symbol(variant_name)?;
|
self.write.write_symbol(variant_name)?;
|
||||||
Ok(SerializeCompound { ser: self, count: Some(count) })
|
Ok(SerializeCompound { ser: self, count: Some(count) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,7 +203,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
||||||
|
|
||||||
fn serialize_struct(self, name: &'static str, count: usize) -> Result<Self::SerializeStruct> {
|
fn serialize_struct(self, name: &'static str, count: usize) -> Result<Self::SerializeStruct> {
|
||||||
self.write.open_record(count)?;
|
self.write.open_record(count)?;
|
||||||
self.write_symbol(name)?;
|
self.write.write_symbol(name)?;
|
||||||
Ok(SerializeCompound { ser: self, count: Some(count) })
|
Ok(SerializeCompound { ser: self, count: Some(count) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -223,7 +215,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
||||||
Result<Self::SerializeStructVariant>
|
Result<Self::SerializeStructVariant>
|
||||||
{
|
{
|
||||||
self.write.open_record(count)?;
|
self.write.open_record(count)?;
|
||||||
self.write_symbol(variant_name)?;
|
self.write.write_symbol(variant_name)?;
|
||||||
Ok(SerializeCompound { ser: self, count: Some(count) })
|
Ok(SerializeCompound { ser: self, count: Some(count) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -349,6 +341,6 @@ impl<'a, 'b, W: Writer> serde::ser::SerializeSeq for SerializeCompound<'a, 'b, W
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_writer<W: Writer, T: Serialize>(write: &mut W, value: &T) -> Result<()> {
|
pub fn to_writer<W: Writer, T: Serialize>(write: &mut W, value: &T) -> Result<()> {
|
||||||
let mut ser: Serializer<'_, W> = Serializer::new(write, None);
|
let mut ser: Serializer<'_, W> = Serializer::new(write);
|
||||||
value.serialize(&mut ser)
|
value.serialize(&mut ser)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
use crate::value::{self, to_value, IOValue, UnwrappedIOValue};
|
||||||
|
use std::iter::IntoIterator;
|
||||||
|
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
||||||
|
|
||||||
|
pub fn serialize<S, T, Item>(s: T, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
T: IntoIterator<Item = Item>,
|
||||||
|
Item: Serialize,
|
||||||
|
{
|
||||||
|
let s = s.into_iter()
|
||||||
|
.map(|item| to_value(item))
|
||||||
|
.collect::<value::Set<IOValue>>();
|
||||||
|
UnwrappedIOValue::from(s).wrap().serialize(serializer)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
T: Deserialize<'de>,
|
||||||
|
{
|
||||||
|
// Relies on the way we hack around serde's data model in de.rs and value/de.rs.
|
||||||
|
T::deserialize(deserializer)
|
||||||
|
}
|
|
@ -1,17 +1,17 @@
|
||||||
use crate::value::{Value, PlainValue, NestedValue, NullDomain};
|
use crate::value::{IOValue, UnwrappedIOValue, NestedValue};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
|
||||||
pub struct Symbol(pub String);
|
pub struct Symbol(pub String);
|
||||||
|
|
||||||
impl serde::Serialize for Symbol {
|
impl serde::Serialize for Symbol {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
||||||
Value::<PlainValue<NullDomain>, NullDomain>::symbol(&self.0).wrap().serialize(serializer)
|
UnwrappedIOValue::symbol(&self.0).wrap().serialize(serializer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de> serde::Deserialize<'de> for Symbol {
|
impl<'de> serde::Deserialize<'de> for Symbol {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
||||||
let v = PlainValue::<NullDomain>::deserialize(deserializer)?;
|
let v = IOValue::deserialize(deserializer)?;
|
||||||
let s = v.value().as_symbol().ok_or_else(|| serde::de::Error::custom("Expected symbol"))?;
|
let s = v.value().as_symbol().ok_or_else(|| serde::de::Error::custom("Expected symbol"))?;
|
||||||
Ok(Symbol(s.clone()))
|
Ok(Symbol(s.clone()))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,49 +0,0 @@
|
||||||
use std::io::{Read, Write, Error};
|
|
||||||
use super::{
|
|
||||||
decoder::{self, Decoder, DecodePlaceholderMap},
|
|
||||||
encoder::{Encoder, EncodePlaceholderMap},
|
|
||||||
invert_map,
|
|
||||||
reader::{BinaryReader, is_eof_error},
|
|
||||||
value::IOValue,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Codec {
|
|
||||||
pub decode_placeholders: Option<DecodePlaceholderMap>,
|
|
||||||
pub encode_placeholders: Option<EncodePlaceholderMap>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Codec {
|
|
||||||
pub fn new(decode_placeholders: DecodePlaceholderMap) -> Self {
|
|
||||||
let encode_placeholders = Some(invert_map(&decode_placeholders));
|
|
||||||
Codec { decode_placeholders: Some(decode_placeholders), encode_placeholders }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn without_placeholders() -> Self {
|
|
||||||
Codec { decode_placeholders: None, encode_placeholders: None }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decoder<'a, 'r, R: Read>(&'a self, read: &'r mut R) -> Decoder<'a, BinaryReader<'r, R>> {
|
|
||||||
Decoder::new(BinaryReader::new(read), self.decode_placeholders.as_ref())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn encoder<'a, 'w, W: Write>(&'a self, write: &'w mut W) -> Encoder<'w, 'a, W> {
|
|
||||||
Encoder::new(write, self.encode_placeholders.as_ref())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decode_all<'r, R: Read>(&self, read: &'r mut R) -> decoder::Result<Vec<IOValue>> {
|
|
||||||
let mut r = BinaryReader::new(read);
|
|
||||||
let vs: Vec<IOValue> = Decoder::new(&mut r, self.decode_placeholders.as_ref())
|
|
||||||
.collect::<decoder::Result<Vec<IOValue>>>()?;
|
|
||||||
match r.peek() {
|
|
||||||
Err(e) if is_eof_error(&e) => Ok(vs),
|
|
||||||
Err(e) => Err(e),
|
|
||||||
Ok(_) => Err(Error::new(std::io::ErrorKind::Other, "trailing bytes")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn encode_bytes(&self, v: &IOValue) -> std::io::Result<Vec<u8>> {
|
|
||||||
let mut buf: Vec<u8> = Vec::new();
|
|
||||||
self.encoder(&mut buf).write(v)?;
|
|
||||||
Ok(buf)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -19,6 +19,12 @@ impl From<InvalidOp> for std::io::Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<InvalidOp> for crate::error::Error {
|
||||||
|
fn from(v: InvalidOp) -> Self {
|
||||||
|
crate::error::Error::Io(v.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl TryFrom<u8> for Op {
|
impl TryFrom<u8> for Op {
|
||||||
type Error = InvalidOp;
|
type Error = InvalidOp;
|
||||||
fn try_from(v: u8) -> Result<Self, Self::Error> {
|
fn try_from(v: u8) -> Result<Self, Self::Error> {
|
||||||
|
|
|
@ -1,60 +1,11 @@
|
||||||
use crate::value::{Value, NestedValue, IOValue, UnwrappedIOValue, Map};
|
|
||||||
use crate::value::value::{Float, Double};
|
use crate::value::value::{Float, Double};
|
||||||
|
use crate::value::{Value, NestedValue, IOValue, UnwrappedIOValue, Map};
|
||||||
|
use crate::error::{Error, ExpectedKind, Received};
|
||||||
use num::traits::cast::ToPrimitive;
|
use num::traits::cast::ToPrimitive;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde::de::{Visitor, SeqAccess, MapAccess, EnumAccess, VariantAccess, DeserializeSeed};
|
use serde::de::{Visitor, SeqAccess, MapAccess, EnumAccess, VariantAccess, DeserializeSeed};
|
||||||
use std::convert::TryFrom;
|
|
||||||
use std::iter::Iterator;
|
use std::iter::Iterator;
|
||||||
|
|
||||||
pub mod error {
|
|
||||||
use num::bigint::BigInt;
|
|
||||||
use crate::value::IOValue;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Error {
|
|
||||||
Message(String),
|
|
||||||
InvalidUnicodeScalar(u32),
|
|
||||||
NumberTooLarge(BigInt),
|
|
||||||
CannotDeserializeAny,
|
|
||||||
Expected(ExpectedKind, IOValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum ExpectedKind {
|
|
||||||
Boolean,
|
|
||||||
Float,
|
|
||||||
Double,
|
|
||||||
|
|
||||||
SignedInteger,
|
|
||||||
String,
|
|
||||||
ByteString,
|
|
||||||
Symbol,
|
|
||||||
|
|
||||||
Record(Option<usize>),
|
|
||||||
SimpleRecord(&'static str, Option<usize>),
|
|
||||||
Option,
|
|
||||||
Sequence,
|
|
||||||
Dictionary,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl serde::de::Error for Error {
|
|
||||||
fn custom<T: std::fmt::Display>(msg: T) -> Self {
|
|
||||||
Self::Message(msg.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for Error {}
|
|
||||||
|
|
||||||
impl std::fmt::Display for Error {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(f, "{:?}", self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub use error::Error;
|
|
||||||
use error::ExpectedKind;
|
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
pub struct Deserializer<'de> {
|
pub struct Deserializer<'de> {
|
||||||
|
@ -76,7 +27,8 @@ impl<'de> Deserializer<'de> {
|
||||||
fn check<'a, T, F>(&'a mut self, f: F, k: ExpectedKind) -> Result<T>
|
fn check<'a, T, F>(&'a mut self, f: F, k: ExpectedKind) -> Result<T>
|
||||||
where F: FnOnce(&'de UnwrappedIOValue) -> Option<T>
|
where F: FnOnce(&'de UnwrappedIOValue) -> Option<T>
|
||||||
{
|
{
|
||||||
f(self.input.value()).ok_or_else(|| Error::Expected(k, self.input.clone()))
|
f(self.input.value()).ok_or_else(
|
||||||
|
|| Error::Expected(k, Received::ReceivedOtherValue(self.input.clone())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +46,7 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de>
|
||||||
Value::SignedInteger(ref i) =>
|
Value::SignedInteger(ref i) =>
|
||||||
match i.to_i64() {
|
match i.to_i64() {
|
||||||
None => match i.to_u64() {
|
None => match i.to_u64() {
|
||||||
None => Err(Error::NumberTooLarge(i.clone())),
|
None => Err(Error::NumberOutOfRange(i.clone())),
|
||||||
Some(n) => visitor.visit_u64(n),
|
Some(n) => visitor.visit_u64(n),
|
||||||
}
|
}
|
||||||
Some(n) => visitor.visit_i64(n),
|
Some(n) => visitor.visit_i64(n),
|
||||||
|
@ -109,11 +61,11 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de>
|
||||||
} else if v.is_simple_record("None", Some(0)) || v.is_simple_record("Some", Some(1)) {
|
} else if v.is_simple_record("None", Some(0)) || v.is_simple_record("Some", Some(1)) {
|
||||||
self.deserialize_option(visitor)
|
self.deserialize_option(visitor)
|
||||||
} else if v.is_simple_record("tuple", None) {
|
} else if v.is_simple_record("tuple", None) {
|
||||||
visitor.visit_seq(VecSeq::new(self, v.as_simple_record("tuple", None).unwrap()))
|
visitor.visit_seq(VecSeq::new(self, v.as_simple_record("tuple", None).unwrap().iter()))
|
||||||
} else {
|
} else {
|
||||||
Err(Error::CannotDeserializeAny)
|
Err(Error::CannotDeserializeAny)
|
||||||
}
|
}
|
||||||
Value::Sequence(ref v) => visitor.visit_seq(VecSeq::new(self, v)),
|
Value::Sequence(ref v) => visitor.visit_seq(VecSeq::new(self, v.iter())),
|
||||||
Value::Dictionary(ref d) => visitor.visit_map(DictMap::new(self, d)),
|
Value::Dictionary(ref d) => visitor.visit_map(DictMap::new(self, d)),
|
||||||
_ => Err(Error::CannotDeserializeAny),
|
_ => Err(Error::CannotDeserializeAny),
|
||||||
}
|
}
|
||||||
|
@ -121,79 +73,68 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de>
|
||||||
|
|
||||||
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
visitor.visit_bool(self.check(|v| v.as_boolean(), ExpectedKind::Boolean)?)
|
visitor.visit_bool(self.input.value().to_boolean()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
let i = self.check(|v| v.as_signedinteger(), ExpectedKind::SignedInteger)?;
|
visitor.visit_i8(self.input.value().to_i8()?)
|
||||||
visitor.visit_i8(i.to_i8().ok_or_else(|| Error::NumberTooLarge(i.clone()))?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
let i = self.check(|v| v.as_signedinteger(), ExpectedKind::SignedInteger)?;
|
visitor.visit_i16(self.input.value().to_i16()?)
|
||||||
visitor.visit_i16(i.to_i16().ok_or_else(|| Error::NumberTooLarge(i.clone()))?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
let i = self.check(|v| v.as_signedinteger(), ExpectedKind::SignedInteger)?;
|
visitor.visit_i32(self.input.value().to_i32()?)
|
||||||
visitor.visit_i32(i.to_i32().ok_or_else(|| Error::NumberTooLarge(i.clone()))?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
let i = self.check(|v| v.as_signedinteger(), ExpectedKind::SignedInteger)?;
|
visitor.visit_i64(self.input.value().to_i64()?)
|
||||||
visitor.visit_i64(i.to_i64().ok_or_else(|| Error::NumberTooLarge(i.clone()))?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
let i = self.check(|v| v.as_signedinteger(), ExpectedKind::SignedInteger)?;
|
visitor.visit_u8(self.input.value().to_u8()?)
|
||||||
visitor.visit_u8(i.to_u8().ok_or_else(|| Error::NumberTooLarge(i.clone()))?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
let i = self.check(|v| v.as_signedinteger(), ExpectedKind::SignedInteger)?;
|
visitor.visit_u16(self.input.value().to_u16()?)
|
||||||
visitor.visit_u16(i.to_u16().ok_or_else(|| Error::NumberTooLarge(i.clone()))?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
let i = self.check(|v| v.as_signedinteger(), ExpectedKind::SignedInteger)?;
|
visitor.visit_u32(self.input.value().to_u32()?)
|
||||||
visitor.visit_u32(i.to_u32().ok_or_else(|| Error::NumberTooLarge(i.clone()))?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
let i = self.check(|v| v.as_signedinteger(), ExpectedKind::SignedInteger)?;
|
visitor.visit_u64(self.input.value().to_u64()?)
|
||||||
visitor.visit_u64(i.to_u64().ok_or_else(|| Error::NumberTooLarge(i.clone()))?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
visitor.visit_f32(self.check(|v| v.as_float(), ExpectedKind::Float)?)
|
visitor.visit_f32(self.input.value().to_float()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
visitor.visit_f64(self.check(|v| v.as_double(), ExpectedKind::Double)?)
|
visitor.visit_f64(self.input.value().to_double()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
let fs = self.check(|v| v.as_simple_record("UnicodeScalar", Some(1)),
|
visitor.visit_char(self.input.value().to_char()?)
|
||||||
ExpectedKind::SimpleRecord("UnicodeScalar", Some(1)))?;
|
|
||||||
let c = fs[0].value().as_u32()
|
|
||||||
.ok_or_else(|| Error::Expected(ExpectedKind::SignedInteger, self.input.copy_via_id()))?;
|
|
||||||
visitor.visit_char(char::try_from(c).or(Err(Error::InvalidUnicodeScalar(c)))?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
visitor.visit_borrowed_str(&self.check(|v| v.as_string(), ExpectedKind::String)?)
|
let s: &'de str = &self.input.value().to_string()?;
|
||||||
|
visitor.visit_borrowed_str(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
@ -203,55 +144,46 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de>
|
||||||
|
|
||||||
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
visitor.visit_borrowed_bytes(&self.check(|v| v.as_bytestring(), ExpectedKind::ByteString)?)
|
let bs: &'de [u8] = &self.input.value().to_bytestring()?;
|
||||||
|
visitor.visit_borrowed_bytes(bs)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
visitor.visit_byte_buf(self.check(|v| v.as_bytestring(), ExpectedKind::ByteString)?.clone())
|
visitor.visit_byte_buf(self.input.value().to_bytestring()?.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
match self.input.value().as_simple_record("None", Some(0)) {
|
match self.input.value().to_option()? {
|
||||||
Some(_fs) => visitor.visit_none(),
|
None => visitor.visit_none(),
|
||||||
None => match self.input.value().as_simple_record("Some", Some(1)) {
|
Some(v) => {
|
||||||
Some(fs) => {
|
self.input = v;
|
||||||
self.input = &fs[0];
|
|
||||||
visitor.visit_some(self)
|
visitor.visit_some(self)
|
||||||
}
|
}
|
||||||
None => Err(Error::Expected(ExpectedKind::Option, self.input.copy_via_id()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
if self.input.value().is_simple_record("tuple", Some(0)) {
|
let _fs = self.input.value().to_simple_record("tuple", Some(0))?;
|
||||||
visitor.visit_unit()
|
visitor.visit_unit()
|
||||||
} else {
|
|
||||||
Err(Error::Expected(ExpectedKind::SimpleRecord("tuple", Some(0)), self.input.copy_via_id()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_unit_struct<V>(self, name: &'static str, visitor: V)
|
fn deserialize_unit_struct<V>(self, name: &'static str, visitor: V)
|
||||||
-> Result<V::Value> where V: Visitor<'de>
|
-> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
if self.input.value().is_simple_record(name, Some(0)) {
|
let _fs = self.input.value().to_simple_record(name, Some(0))?;
|
||||||
visitor.visit_unit()
|
visitor.visit_unit()
|
||||||
} else {
|
|
||||||
Err(Error::Expected(ExpectedKind::SimpleRecord(name, Some(0)), self.input.copy_via_id()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V)
|
fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V)
|
||||||
-> Result<V::Value> where V: Visitor<'de>
|
-> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
match super::magic::transmit_input_value(name, || self.input.clone()) {
|
match super::magic::transmit_input_value(name, || Ok(self.input.clone()))? {
|
||||||
Some(v) => visitor.visit_u64(v),
|
Some(v) => visitor.visit_u64(v),
|
||||||
None => {
|
None => {
|
||||||
let fs = self.check(|v| v.as_simple_record(name, Some(1)),
|
let fs = self.input.value().to_simple_record(name, Some(1))?;
|
||||||
ExpectedKind::SimpleRecord(name, Some(1)))?;
|
|
||||||
self.input = &fs[0];
|
self.input = &fs[0];
|
||||||
visitor.visit_newtype_struct(self)
|
visitor.visit_newtype_struct(self)
|
||||||
}
|
}
|
||||||
|
@ -259,27 +191,32 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de>
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de> {
|
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de> {
|
||||||
let vs = self.check(|v| v.as_sequence(), ExpectedKind::Sequence)?;
|
match self.input.value().as_sequence() {
|
||||||
visitor.visit_seq(VecSeq::new(self, vs))
|
Some(vs) => visitor.visit_seq(VecSeq::new(self, vs.iter())),
|
||||||
|
None => {
|
||||||
|
// Hack around serde's model: Deserialize *sets* as
|
||||||
|
// sequences, too, and reconstruct them as Rust Sets
|
||||||
|
// on the visitor side.
|
||||||
|
visitor.visit_seq(VecSeq::new(self, self.input.value().to_set()?.iter()))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
let fs = self.check(|v| v.as_simple_record("tuple", Some(len)),
|
let fs = self.input.value().to_simple_record("tuple", Some(len))?;
|
||||||
ExpectedKind::SimpleRecord("tuple", Some(len)))?;
|
visitor.visit_seq(VecSeq::new(self, fs.iter()))
|
||||||
visitor.visit_seq(VecSeq::new(self, fs))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_tuple_struct<V>(self, name: &'static str, len: usize, visitor: V)
|
fn deserialize_tuple_struct<V>(self, name: &'static str, len: usize, visitor: V)
|
||||||
-> Result<V::Value> where V: Visitor<'de>
|
-> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
let fs = self.check(|v| v.as_simple_record(name, Some(len)),
|
let fs = self.input.value().to_simple_record(name, Some(len))?;
|
||||||
ExpectedKind::SimpleRecord(name, Some(len)))?;
|
visitor.visit_seq(VecSeq::new(self, fs.iter()))
|
||||||
visitor.visit_seq(VecSeq::new(self, fs))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de> {
|
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de> {
|
||||||
let d = self.check(|v| v.as_dictionary(), ExpectedKind::Dictionary)?;
|
let d = self.input.value().to_dictionary()?;
|
||||||
visitor.visit_map(DictMap::new(self, d))
|
visitor.visit_map(DictMap::new(self, d))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -289,9 +226,8 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de>
|
||||||
visitor: V)
|
visitor: V)
|
||||||
-> Result<V::Value> where V: Visitor<'de>
|
-> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
let fs = self.check(|v| v.as_simple_record(name, Some(fields.len())),
|
let fs = self.input.value().to_simple_record(name, Some(fields.len()))?;
|
||||||
ExpectedKind::SimpleRecord(name, Some(fields.len())))?;
|
visitor.visit_seq(VecSeq::new(self, fs.iter()))
|
||||||
visitor.visit_seq(VecSeq::new(self, fs))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_enum<V>(self,
|
fn deserialize_enum<V>(self,
|
||||||
|
@ -305,7 +241,7 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de>
|
||||||
|
|
||||||
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
visitor.visit_borrowed_str(&self.check(|v| v.as_symbol(), ExpectedKind::Symbol)?)
|
visitor.visit_str(&self.input.value().to_symbol()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
@ -314,32 +250,32 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct VecSeq<'a, 'de: 'a> {
|
pub struct VecSeq<'a, 'de: 'a, I: Iterator<Item = &'de IOValue>> {
|
||||||
index: usize,
|
iter: I,
|
||||||
vec: &'de [IOValue],
|
|
||||||
de: &'a mut Deserializer<'de>,
|
de: &'a mut Deserializer<'de>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de, 'a> VecSeq<'a, 'de> {
|
impl<'de, 'a, I: Iterator<Item = &'de IOValue>> VecSeq<'a, 'de, I> {
|
||||||
fn new(de: &'a mut Deserializer<'de>, vec: &'de [IOValue]) -> Self {
|
fn new(de: &'a mut Deserializer<'de>, iter: I) -> Self {
|
||||||
VecSeq { index: 0, vec, de }
|
VecSeq { iter, de }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de, 'a> SeqAccess<'de> for VecSeq<'a, 'de> {
|
impl<'de, 'a, I: Iterator<Item = &'de IOValue>> SeqAccess<'de> for VecSeq<'a, 'de, I> {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn next_element_seed<T>(&mut self, seed: T)
|
fn next_element_seed<T>(&mut self, seed: T) ->
|
||||||
-> Result<Option<T::Value>> where T: DeserializeSeed<'de>
|
Result<Option<T::Value>>
|
||||||
|
where
|
||||||
|
T: DeserializeSeed<'de>
|
||||||
{
|
{
|
||||||
if self.index == self.vec.len() {
|
match self.iter.next() {
|
||||||
return Ok(None)
|
None => Ok(None),
|
||||||
|
Some(v) => {
|
||||||
|
self.de.input = v;
|
||||||
|
Ok(Some(seed.deserialize(&mut *self.de)?))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.de.input = &self.vec[self.index];
|
|
||||||
self.index += 1;
|
|
||||||
let value = seed.deserialize(&mut *self.de)?;
|
|
||||||
Ok(Some(value))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -409,12 +345,12 @@ impl<'a, 'de> VariantAccess<'de> for &'a mut Deserializer<'de> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value> where V: Visitor<'de> {
|
fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value> where V: Visitor<'de> {
|
||||||
visitor.visit_seq(VecSeq::new(self, &self.input.value().as_record(None).unwrap().1))
|
visitor.visit_seq(VecSeq::new(self, self.input.value().as_record(None).unwrap().1.iter()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V)
|
fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V)
|
||||||
-> Result<V::Value> where V: Visitor<'de>
|
-> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
visitor.visit_seq(VecSeq::new(self, &self.input.value().as_record(Some(fields.len())).unwrap().1))
|
visitor.visit_seq(VecSeq::new(self, self.input.value().as_record(Some(fields.len())).unwrap().1.iter()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,40 +1,49 @@
|
||||||
use super::reader::{Reader, is_eof_error};
|
use std::borrow::Cow;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use super::reader::{self, Reader, BinaryReader};
|
||||||
use super::value::IOValue;
|
use super::value::IOValue;
|
||||||
|
|
||||||
pub use super::reader::{Result, DecodePlaceholderMap};
|
pub use super::reader::IOResult as Result;
|
||||||
|
|
||||||
pub struct Decoder<'a, R: Reader> {
|
pub struct Decoder<'de, R: Reader<'de>> {
|
||||||
pub read: R,
|
pub read: R,
|
||||||
placeholders: Option<&'a DecodePlaceholderMap>,
|
|
||||||
read_annotations: bool,
|
read_annotations: bool,
|
||||||
|
phantom: PhantomData<&'de ()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, R: Reader> Decoder<'a, R> {
|
pub fn from_bytes<'de>(bytes: &'de [u8]) ->
|
||||||
pub fn new(read: R, placeholders: Option<&'a DecodePlaceholderMap>) ->
|
Decoder<'de, BinaryReader<'de, reader::BytesBinarySource<'de>>>
|
||||||
Self
|
{
|
||||||
{
|
Decoder::new(reader::from_bytes(bytes))
|
||||||
Decoder {
|
}
|
||||||
read,
|
|
||||||
placeholders,
|
pub fn from_read<'de, 'a, IOR: std::io::Read>(read: &'a mut IOR) ->
|
||||||
read_annotations: true,
|
Decoder<'de, BinaryReader<'de, reader::IOBinarySource<'a, IOR>>>
|
||||||
}
|
{
|
||||||
|
Decoder::new(reader::from_read(read))
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de, R: Reader<'de>> Decoder<'de, R> {
|
||||||
|
pub fn new(read: R) -> Self {
|
||||||
|
Decoder { read, read_annotations: true, phantom: PhantomData }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_read_annotations(&mut self, read_annotations: bool) {
|
pub fn set_read_annotations(&mut self, read_annotations: bool) {
|
||||||
self.read_annotations = read_annotations
|
self.read_annotations = read_annotations
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next_or_err(&mut self) -> Result<IOValue> {
|
pub fn demand_next(&mut self) -> Result<Cow<'de, IOValue>> {
|
||||||
self.read.next(self.placeholders, self.read_annotations)
|
self.read.demand_next(self.read_annotations)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, R: Reader> std::iter::Iterator for Decoder<'a, R> {
|
impl<'de, R: Reader<'de>> std::iter::Iterator for Decoder<'de, R> {
|
||||||
type Item = Result<IOValue>;
|
type Item = Result<IOValue>;
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
match self.next_or_err() {
|
match self.read.next(self.read_annotations) {
|
||||||
Err(e) if is_eof_error(&e) => None,
|
Err(e) => Some(Err(e)),
|
||||||
other => Some(other)
|
Ok(None) => None,
|
||||||
|
Ok(Some(v)) => Some(Ok(v.into_owned())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,21 @@
|
||||||
use super::value::{Value, NestedValue, Domain, IOValue, UnwrappedIOValue, Float, Double, Map};
|
use super::value::{Value, NestedValue, Domain, IOValue, UnwrappedIOValue, Float, Double};
|
||||||
use super::writer::Writer;
|
use super::writer::Writer;
|
||||||
|
|
||||||
pub use super::writer::Result;
|
pub use super::writer::Result;
|
||||||
|
|
||||||
pub type EncodePlaceholderMap = Map<UnwrappedIOValue, usize>;
|
pub struct Encoder<'a, W: Writer> {
|
||||||
|
|
||||||
pub struct Encoder<'a, 'b, W: Writer> {
|
|
||||||
pub write: &'a mut W,
|
pub write: &'a mut W,
|
||||||
pub placeholders: Option<&'b EncodePlaceholderMap>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b, W: Writer> Encoder<'a, 'b, W> {
|
pub fn encode_bytes(v: &IOValue) -> std::io::Result<Vec<u8>> {
|
||||||
pub fn new(write: &'a mut W, placeholders: Option<&'b EncodePlaceholderMap>) -> Self {
|
let mut buf: Vec<u8> = Vec::new();
|
||||||
Encoder{ write, placeholders }
|
Encoder::new(&mut buf).write(v)?;
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, W: Writer> Encoder<'a, W> {
|
||||||
|
pub fn new(write: &'a mut W) -> Self {
|
||||||
|
Encoder { write }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(&mut self, v: &IOValue) -> Result {
|
pub fn write(&mut self, v: &IOValue) -> Result {
|
||||||
|
@ -24,9 +27,7 @@ impl<'a, 'b, W: Writer> Encoder<'a, 'b, W> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_value(&mut self, v: &UnwrappedIOValue) -> Result {
|
pub fn write_value(&mut self, v: &UnwrappedIOValue) -> Result {
|
||||||
match self.placeholders.and_then(|m| m.get(v)) {
|
match v {
|
||||||
Some(&n) => self.write.write_placeholder_ref(n),
|
|
||||||
None => match v {
|
|
||||||
Value::Boolean(b) => self.write.write_bool(*b),
|
Value::Boolean(b) => self.write.write_bool(*b),
|
||||||
Value::Float(Float(f)) => self.write.write_f32(*f),
|
Value::Float(Float(f)) => self.write.write_f32(*f),
|
||||||
Value::Double(Double(d)) => self.write.write_f64(*d),
|
Value::Double(Double(d)) => self.write.write_f64(*d),
|
||||||
|
@ -58,5 +59,4 @@ impl<'a, 'b, W: Writer> Encoder<'a, 'b, W> {
|
||||||
Value::Domain(ref d) => self.write(&d.as_preserves()?)
|
Value::Domain(ref d) => self.write(&d.as_preserves()?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
use super::value::{
|
use super::value::IOValue;
|
||||||
IOValue,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub static MAGIC: &str = "$____Preserves_Serde_Magic";
|
pub static MAGIC: &str = "$____Preserves_Serde_Magic";
|
||||||
|
|
||||||
|
@ -16,20 +14,22 @@ impl<'de> serde::de::Visitor<'de> for IOValueVisitor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn output_value<S: serde::Serializer>(serializer: S, v: IOValue) ->
|
pub fn output_value<S: serde::Serializer>(serializer: S, v: IOValue) ->
|
||||||
Result<S::Ok, S::Error>
|
Result<S::Ok, S::Error>
|
||||||
{
|
{
|
||||||
serializer.serialize_newtype_struct(MAGIC, &(Box::into_raw(Box::new(v)) as u64))
|
serializer.serialize_newtype_struct(MAGIC, &(Box::into_raw(Box::new(v)) as u64))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn input_value<'de, D: serde::Deserializer<'de>>(deserializer: D) ->
|
#[inline]
|
||||||
Result<IOValue, D::Error>
|
pub fn input_value<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<IOValue, D::Error>
|
||||||
{
|
{
|
||||||
deserializer.deserialize_newtype_struct(MAGIC, IOValueVisitor)
|
deserializer.deserialize_newtype_struct(MAGIC, IOValueVisitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn receive_output_value<T: ?Sized>(name: &'static str, magic_value: &T) -> Option<IOValue> {
|
pub fn receive_output_value<T: ?Sized>(name: &'static str, magic_value: &T) -> Option<IOValue> {
|
||||||
if name == MAGIC {
|
if name == MAGIC {
|
||||||
let b = unsafe { Box::from_raw(*((magic_value as *const T) as *const u64) as *mut IOValue) };
|
let b = unsafe { Box::from_raw(*((magic_value as *const T) as *const u64) as *mut IOValue) };
|
||||||
|
@ -40,13 +40,14 @@ pub fn receive_output_value<T: ?Sized>(name: &'static str, magic_value: &T) -> O
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn transmit_input_value<F>(name: &'static str, f: F) -> Option<u64>
|
#[inline]
|
||||||
where F: FnOnce() -> IOValue
|
pub fn transmit_input_value<F>(name: &'static str, f: F) -> Result<Option<u64>, crate::error::Error>
|
||||||
|
where F: FnOnce() -> Result<IOValue, crate::error::Error>
|
||||||
{
|
{
|
||||||
if name == MAGIC {
|
if name == MAGIC {
|
||||||
let b: Box<IOValue> = Box::new(f());
|
let b: Box<IOValue> = Box::new(f()?);
|
||||||
Some(Box::into_raw(b) as u64)
|
Ok(Some(Box::into_raw(b) as u64))
|
||||||
} else {
|
} else {
|
||||||
None
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
pub mod codec;
|
|
||||||
pub mod constants;
|
pub mod constants;
|
||||||
pub mod de;
|
pub mod de;
|
||||||
pub mod decoder;
|
pub mod decoder;
|
||||||
|
@ -10,17 +9,12 @@ pub mod writer;
|
||||||
|
|
||||||
pub mod magic;
|
pub mod magic;
|
||||||
|
|
||||||
pub use codec::Codec;
|
|
||||||
pub use de::Deserializer;
|
pub use de::Deserializer;
|
||||||
pub use de::from_value;
|
pub use de::from_value;
|
||||||
pub use decoder::Decoder;
|
pub use decoder::Decoder;
|
||||||
pub use encoder::EncodePlaceholderMap;
|
|
||||||
pub use encoder::Encoder;
|
pub use encoder::Encoder;
|
||||||
pub use reader::BinaryReader;
|
pub use reader::BinaryReader;
|
||||||
pub use reader::DecodePlaceholderMap;
|
|
||||||
pub use reader::Reader;
|
pub use reader::Reader;
|
||||||
pub use reader::is_eof_error;
|
|
||||||
pub use reader::is_syntax_error;
|
|
||||||
pub use ser::Serializer;
|
pub use ser::Serializer;
|
||||||
pub use ser::to_value;
|
pub use ser::to_value;
|
||||||
pub use value::AnnotatedValue;
|
pub use value::AnnotatedValue;
|
||||||
|
@ -37,6 +31,10 @@ pub use value::UnwrappedIOValue;
|
||||||
pub use value::Value;
|
pub use value::Value;
|
||||||
pub use writer::Writer;
|
pub use writer::Writer;
|
||||||
|
|
||||||
|
pub use value::FALSE;
|
||||||
|
pub use value::TRUE;
|
||||||
|
pub use value::EMPTY_SEQ;
|
||||||
|
|
||||||
pub fn invert_map<A, B>(m: &Map<A, B>) -> Map<B, A>
|
pub fn invert_map<A, B>(m: &Map<A, B>) -> Map<B, A>
|
||||||
where A: Clone, B: Clone + Ord
|
where A: Clone, B: Clone + Ord
|
||||||
{
|
{
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,8 +1,9 @@
|
||||||
use num::bigint::BigInt;
|
use num::bigint::BigInt;
|
||||||
use num::traits::cast::ToPrimitive;
|
use num::traits::cast::ToPrimitive;
|
||||||
use std::cmp::{Ordering};
|
use std::cmp::Ordering;
|
||||||
|
use std::convert::TryFrom;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::hash::{Hash,Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::ops::Index;
|
use std::ops::Index;
|
||||||
use std::ops::IndexMut;
|
use std::ops::IndexMut;
|
||||||
use std::string::String;
|
use std::string::String;
|
||||||
|
@ -11,6 +12,8 @@ use std::vec::Vec;
|
||||||
pub use std::collections::BTreeSet as Set;
|
pub use std::collections::BTreeSet as Set;
|
||||||
pub use std::collections::BTreeMap as Map;
|
pub use std::collections::BTreeMap as Map;
|
||||||
|
|
||||||
|
use crate::error::{Error, ExpectedKind, Received};
|
||||||
|
|
||||||
pub trait Domain: Sized + Debug + Clone + Eq + Hash + Ord {
|
pub trait Domain: Sized + Debug + Clone + Eq + Hash + Ord {
|
||||||
fn as_preserves(&self) -> Result<IOValue, std::io::Error> {
|
fn as_preserves(&self) -> Result<IOValue, std::io::Error> {
|
||||||
Err(std::io::Error::new(std::io::ErrorKind::InvalidData,
|
Err(std::io::Error::new(std::io::ErrorKind::InvalidData,
|
||||||
|
@ -30,6 +33,7 @@ pub trait NestedValue<D: Domain>: Sized + Debug + Clone + Eq + Hash + Ord {
|
||||||
|
|
||||||
fn annotations(&self) -> &[Self];
|
fn annotations(&self) -> &[Self];
|
||||||
fn value(&self) -> &Value<Self, D>;
|
fn value(&self) -> &Value<Self, D>;
|
||||||
|
fn pieces(self) -> (Vec<Self>, Value<Self, D>);
|
||||||
fn value_owned(self) -> Value<Self, D>;
|
fn value_owned(self) -> Value<Self, D>;
|
||||||
|
|
||||||
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
@ -47,10 +51,6 @@ pub trait NestedValue<D: Domain>: Sized + Debug + Clone + Eq + Hash + Ord {
|
||||||
self.value().copy_via(f))
|
self.value().copy_via(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copy_via_id<M: NestedValue<D>>(&self) -> M {
|
|
||||||
self.copy_via(&|d| Value::Domain(d.clone()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_io_value(&self) -> IOValue {
|
fn to_io_value(&self) -> IOValue {
|
||||||
self.copy_via(&|d| d.as_preserves().unwrap().value().clone())
|
self.copy_via(&|d| d.as_preserves().unwrap().value().clone())
|
||||||
}
|
}
|
||||||
|
@ -250,6 +250,10 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
N::wrap(self)
|
N::wrap(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn expected(&self, k: ExpectedKind) -> Error {
|
||||||
|
Error::Expected(k, Received::ReceivedOtherValue(self.clone().wrap().to_io_value()))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_boolean(&self) -> bool {
|
pub fn is_boolean(&self) -> bool {
|
||||||
self.as_boolean().is_some()
|
self.as_boolean().is_some()
|
||||||
}
|
}
|
||||||
|
@ -270,6 +274,10 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_boolean(&self) -> Result<bool, Error> {
|
||||||
|
self.as_boolean().ok_or_else(|| self.expected(ExpectedKind::Boolean))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_float(&self) -> bool {
|
pub fn is_float(&self) -> bool {
|
||||||
self.as_float().is_some()
|
self.as_float().is_some()
|
||||||
}
|
}
|
||||||
|
@ -290,6 +298,10 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_float(&self) -> Result<f32, Error> {
|
||||||
|
self.as_float().ok_or_else(|| self.expected(ExpectedKind::Float))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_double(&self) -> bool {
|
pub fn is_double(&self) -> bool {
|
||||||
self.as_double().is_some()
|
self.as_double().is_some()
|
||||||
}
|
}
|
||||||
|
@ -310,6 +322,10 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_double(&self) -> Result<f64, Error> {
|
||||||
|
self.as_double().ok_or_else(|| self.expected(ExpectedKind::Double))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_signedinteger(&self) -> bool {
|
pub fn is_signedinteger(&self) -> bool {
|
||||||
self.as_signedinteger().is_some()
|
self.as_signedinteger().is_some()
|
||||||
}
|
}
|
||||||
|
@ -330,6 +346,10 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_signedinteger(&self) -> Result<&BigInt, Error> {
|
||||||
|
self.as_signedinteger().ok_or_else(|| self.expected(ExpectedKind::SignedInteger))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn as_u8(&self) -> Option<u8> { self.as_signedinteger().and_then(|i| i.to_u8()) }
|
pub fn as_u8(&self) -> Option<u8> { self.as_signedinteger().and_then(|i| i.to_u8()) }
|
||||||
pub fn as_i8(&self) -> Option<i8> { self.as_signedinteger().and_then(|i| i.to_i8()) }
|
pub fn as_i8(&self) -> Option<i8> { self.as_signedinteger().and_then(|i| i.to_i8()) }
|
||||||
pub fn as_u16(&self) -> Option<u16> { self.as_signedinteger().and_then(|i| i.to_u16()) }
|
pub fn as_u16(&self) -> Option<u16> { self.as_signedinteger().and_then(|i| i.to_u16()) }
|
||||||
|
@ -339,6 +359,52 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
pub fn as_u64(&self) -> Option<u64> { self.as_signedinteger().and_then(|i| i.to_u64()) }
|
pub fn as_u64(&self) -> Option<u64> { self.as_signedinteger().and_then(|i| i.to_u64()) }
|
||||||
pub fn as_i64(&self) -> Option<i64> { self.as_signedinteger().and_then(|i| i.to_i64()) }
|
pub fn as_i64(&self) -> Option<i64> { self.as_signedinteger().and_then(|i| i.to_i64()) }
|
||||||
|
|
||||||
|
pub fn to_i8(&self) -> Result<i8, Error> {
|
||||||
|
let i = self.to_signedinteger()?;
|
||||||
|
i.to_i8().ok_or_else(|| Error::NumberOutOfRange(i.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_u8(&self) -> Result<u8, Error> {
|
||||||
|
let i = self.to_signedinteger()?;
|
||||||
|
i.to_u8().ok_or_else(|| Error::NumberOutOfRange(i.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_u16(&self) -> Result<u16, Error> {
|
||||||
|
let i = self.to_signedinteger()?;
|
||||||
|
i.to_u16().ok_or_else(|| Error::NumberOutOfRange(i.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_i16(&self) -> Result<i16, Error> {
|
||||||
|
let i = self.to_signedinteger()?;
|
||||||
|
i.to_i16().ok_or_else(|| Error::NumberOutOfRange(i.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_u32(&self) -> Result<u32, Error> {
|
||||||
|
let i = self.to_signedinteger()?;
|
||||||
|
i.to_u32().ok_or_else(|| Error::NumberOutOfRange(i.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_i32(&self) -> Result<i32, Error> {
|
||||||
|
let i = self.to_signedinteger()?;
|
||||||
|
i.to_i32().ok_or_else(|| Error::NumberOutOfRange(i.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_u64(&self) -> Result<u64, Error> {
|
||||||
|
let i = self.to_signedinteger()?;
|
||||||
|
i.to_u64().ok_or_else(|| Error::NumberOutOfRange(i.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_i64(&self) -> Result<i64, Error> {
|
||||||
|
let i = self.to_signedinteger()?;
|
||||||
|
i.to_i64().ok_or_else(|| Error::NumberOutOfRange(i.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_char(&self) -> Result<char, Error> {
|
||||||
|
let fs = self.to_simple_record("UnicodeScalar", Some(1))?;
|
||||||
|
let c = fs[0].value().to_u32()?;
|
||||||
|
char::try_from(c).or_else(|_| Err(Error::InvalidUnicodeScalar(c)))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_string(&self) -> bool {
|
pub fn is_string(&self) -> bool {
|
||||||
self.as_string().is_some()
|
self.as_string().is_some()
|
||||||
}
|
}
|
||||||
|
@ -359,6 +425,10 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_string(&self) -> Result<&String, Error> {
|
||||||
|
self.as_string().ok_or_else(|| self.expected(ExpectedKind::String))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_bytestring(&self) -> bool {
|
pub fn is_bytestring(&self) -> bool {
|
||||||
self.as_bytestring().is_some()
|
self.as_bytestring().is_some()
|
||||||
}
|
}
|
||||||
|
@ -379,6 +449,10 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_bytestring(&self) -> Result<&Vec<u8>, Error> {
|
||||||
|
self.as_bytestring().ok_or_else(|| self.expected(ExpectedKind::ByteString))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn symbol(s: &str) -> Value<N, D> {
|
pub fn symbol(s: &str) -> Value<N, D> {
|
||||||
Value::Symbol(s.to_string())
|
Value::Symbol(s.to_string())
|
||||||
}
|
}
|
||||||
|
@ -403,6 +477,10 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_symbol(&self) -> Result<&String, Error> {
|
||||||
|
self.as_symbol().ok_or_else(|| self.expected(ExpectedKind::Symbol))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn record(label: N, fields: Vec<N>) -> Value<N, D> {
|
pub fn record(label: N, fields: Vec<N>) -> Value<N, D> {
|
||||||
Value::Record((label.boxwrap(), fields))
|
Value::Record((label.boxwrap(), fields))
|
||||||
}
|
}
|
||||||
|
@ -435,6 +513,10 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_record(&self, arity: Option<usize>) -> Result<&Record<N, D>, Error> {
|
||||||
|
self.as_record(arity).ok_or_else(|| self.expected(ExpectedKind::Record(arity)))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn simple_record(label: &str, fields: Vec<N>) -> Value<N, D> {
|
pub fn simple_record(label: &str, fields: Vec<N>) -> Value<N, D> {
|
||||||
Value::record(Value::symbol(label).wrap(), fields)
|
Value::record(Value::symbol(label).wrap(), fields)
|
||||||
}
|
}
|
||||||
|
@ -452,6 +534,23 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_simple_record(&self, label: &'static str, arity: Option<usize>) ->
|
||||||
|
Result<&Vec<N>, Error>
|
||||||
|
{
|
||||||
|
self.as_simple_record(label, arity)
|
||||||
|
.ok_or_else(|| self.expected(ExpectedKind::SimpleRecord(label, arity)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_option(&self) -> Result<Option<&N>, Error> {
|
||||||
|
match self.as_simple_record("None", Some(0)) {
|
||||||
|
Some(_fs) => Ok(None),
|
||||||
|
None => match self.as_simple_record("Some", Some(1)) {
|
||||||
|
Some(fs) => Ok(Some(&fs[0])),
|
||||||
|
None => Err(self.expected(ExpectedKind::Option))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_sequence(&self) -> bool {
|
pub fn is_sequence(&self) -> bool {
|
||||||
self.as_sequence().is_some()
|
self.as_sequence().is_some()
|
||||||
}
|
}
|
||||||
|
@ -472,6 +571,10 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_sequence(&self) -> Result<&Vec<N>, Error> {
|
||||||
|
self.as_sequence().ok_or_else(|| self.expected(ExpectedKind::Sequence))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_set(&self) -> bool {
|
pub fn is_set(&self) -> bool {
|
||||||
self.as_set().is_some()
|
self.as_set().is_some()
|
||||||
}
|
}
|
||||||
|
@ -492,6 +595,10 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_set(&self) -> Result<&Set<N>, Error> {
|
||||||
|
self.as_set().ok_or_else(|| self.expected(ExpectedKind::Set))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_dictionary(&self) -> bool {
|
pub fn is_dictionary(&self) -> bool {
|
||||||
self.as_dictionary().is_some()
|
self.as_dictionary().is_some()
|
||||||
}
|
}
|
||||||
|
@ -512,6 +619,10 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_dictionary(&self) -> Result<&Map<N, N>, Error> {
|
||||||
|
self.as_dictionary().ok_or_else(|| self.expected(ExpectedKind::Dictionary))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn copy_via<M: NestedValue<E>, E: Domain, F>(&self, f: &F) -> Value<M, E>
|
pub fn copy_via<M: NestedValue<E>, E: Domain, F>(&self, f: &F) -> Value<M, E>
|
||||||
where
|
where
|
||||||
F: Fn(&D) -> Value<M, E>
|
F: Fn(&D) -> Value<M, E>
|
||||||
|
@ -660,6 +771,11 @@ impl<D: Domain> NestedValue<D> for PlainValue<D> {
|
||||||
&(self.0).1
|
&(self.0).1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pieces(self) -> (Vec<Self>, Value<Self, D>) {
|
||||||
|
let AnnotatedValue(anns, v) = self.0;
|
||||||
|
(anns, v)
|
||||||
|
}
|
||||||
|
|
||||||
fn value_owned(self) -> Value<Self, D> {
|
fn value_owned(self) -> Value<Self, D> {
|
||||||
(self.0).1
|
(self.0).1
|
||||||
}
|
}
|
||||||
|
@ -717,6 +833,13 @@ impl<D: Domain> NestedValue<D> for RcValue<D> {
|
||||||
&(self.0).1
|
&(self.0).1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pieces(self) -> (Vec<Self>, Value<Self, D>) {
|
||||||
|
match Rc::try_unwrap(self.0) {
|
||||||
|
Ok(AnnotatedValue(anns, v)) => (anns, v),
|
||||||
|
Err(r) => (r.0.clone(), r.1.clone()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn value_owned(self) -> Value<Self, D> {
|
fn value_owned(self) -> Value<Self, D> {
|
||||||
Rc::try_unwrap(self.0).unwrap_or_else(|_| panic!("value_owned on RcValue with refcount greater than one")).1
|
Rc::try_unwrap(self.0).unwrap_or_else(|_| panic!("value_owned on RcValue with refcount greater than one")).1
|
||||||
}
|
}
|
||||||
|
@ -774,6 +897,13 @@ impl<D: Domain> NestedValue<D> for ArcValue<D> {
|
||||||
&(self.0).1
|
&(self.0).1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pieces(self) -> (Vec<Self>, Value<Self, D>) {
|
||||||
|
match Arc::try_unwrap(self.0) {
|
||||||
|
Ok(AnnotatedValue(anns, v)) => (anns, v),
|
||||||
|
Err(r) => (r.0.clone(), r.1.clone()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn value_owned(self) -> Value<Self, D> {
|
fn value_owned(self) -> Value<Self, D> {
|
||||||
Arc::try_unwrap(self.0).unwrap_or_else(|_| panic!("value_owned on ArcValue with refcount greater than one")).1
|
Arc::try_unwrap(self.0).unwrap_or_else(|_| panic!("value_owned on ArcValue with refcount greater than one")).1
|
||||||
}
|
}
|
||||||
|
@ -807,6 +937,12 @@ impl Domain for NullDomain {}
|
||||||
pub struct IOValue(Arc<AnnotatedValue<IOValue, NullDomain>>);
|
pub struct IOValue(Arc<AnnotatedValue<IOValue, NullDomain>>);
|
||||||
pub type UnwrappedIOValue = Value<IOValue, NullDomain>;
|
pub type UnwrappedIOValue = Value<IOValue, NullDomain>;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref FALSE: IOValue = IOValue(Arc::new(AnnotatedValue(Vec::new(), Value::Boolean(false))));
|
||||||
|
pub static ref TRUE: IOValue = IOValue(Arc::new(AnnotatedValue(Vec::new(), Value::Boolean(true))));
|
||||||
|
pub static ref EMPTY_SEQ: IOValue = IOValue(Arc::new(AnnotatedValue(Vec::new(), Value::Sequence(Vec::new()))));
|
||||||
|
}
|
||||||
|
|
||||||
impl NestedValue<NullDomain> for IOValue {
|
impl NestedValue<NullDomain> for IOValue {
|
||||||
type BoxType = Self;
|
type BoxType = Self;
|
||||||
|
|
||||||
|
@ -834,6 +970,13 @@ impl NestedValue<NullDomain> for IOValue {
|
||||||
&(self.0).1
|
&(self.0).1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pieces(self) -> (Vec<Self>, Value<Self, NullDomain>) {
|
||||||
|
match Arc::try_unwrap(self.0) {
|
||||||
|
Ok(AnnotatedValue(anns, v)) => (anns, v),
|
||||||
|
Err(r) => (r.0.clone(), r.1.clone()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn value_owned(self) -> Value<Self, NullDomain> {
|
fn value_owned(self) -> Value<Self, NullDomain> {
|
||||||
match Arc::try_unwrap(self.0) {
|
match Arc::try_unwrap(self.0) {
|
||||||
Ok(AnnotatedValue(_anns, v)) => v,
|
Ok(AnnotatedValue(_anns, v)) => v,
|
||||||
|
|
|
@ -8,7 +8,6 @@ pub type Result = std::result::Result<(), Error>;
|
||||||
|
|
||||||
pub trait Writer {
|
pub trait Writer {
|
||||||
fn write_annotation_prefix(&mut self) -> Result;
|
fn write_annotation_prefix(&mut self) -> Result;
|
||||||
fn write_placeholder_ref(&mut self, v: usize) -> Result;
|
|
||||||
fn write_noop(&mut self) -> Result;
|
fn write_noop(&mut self) -> Result;
|
||||||
|
|
||||||
fn write_bool(&mut self, v: bool) -> Result;
|
fn write_bool(&mut self, v: bool) -> Result;
|
||||||
|
@ -86,10 +85,6 @@ impl<W: std::io::Write> Writer for W {
|
||||||
write_header(self, Op::Misc(0), 5)
|
write_header(self, Op::Misc(0), 5)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_placeholder_ref(&mut self, v: usize) -> Result {
|
|
||||||
write_header(self, Op::Misc(1), v)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_noop(&mut self) -> Result {
|
fn write_noop(&mut self) -> Result {
|
||||||
write_op(self, Op::Reserved(3), 15)
|
write_op(self, Op::Reserved(3), 15)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue