First tentative step to allowing reuse of Value with direct containment, Rc and Arc

This commit is contained in:
Tony Garnock-Jones 2019-10-12 15:40:35 +01:00
parent cea42db3dc
commit f1b37ac9af
9 changed files with 268 additions and 198 deletions

View File

@ -5,10 +5,9 @@ pub mod symbol;
mod ieee754_section_5_10_total_order_tests {
use std::cmp::Ordering::{Less, Equal, Greater};
use crate::value::Value;
// fn v<T>(val: T) -> Value where Value: std::convert::From<T> { Value::from(val) }
fn f(val: f32) -> Value { Value::from(val) }
fn d(val: f64) -> Value { Value::from(val) }
use crate::value::{Value, AValue};
fn f(val: f32) -> Value<AValue> { Value::from(val) }
fn d(val: f64) -> Value<AValue> { Value::from(val) }
// TODO: Test cases with a few different signalling and non-signalling NaNs
@ -98,78 +97,80 @@ mod ieee754_section_5_10_total_order_tests {
#[cfg(test)]
mod value_tests {
use crate::value::Value;
use crate::value::{Value, AValue};
use num::bigint::BigInt;
type VV = Value<AValue>;
#[test] fn boolean_mut() {
let mut b = Value::Boolean(true);
let mut b = VV::Boolean(true);
assert!(b.is_boolean());
*(b.as_boolean_mut().unwrap()) = false;
assert_eq!(b, Value::Boolean(false));
assert_eq!(b, VV::Boolean(false));
}
#[test] fn float_mut() {
let mut f = Value::from(1.0f32);
let mut f = VV::from(1.0f32);
assert!(f.is_float());
*(f.as_float_mut().unwrap()) = 123.45;
assert_eq!(f, Value::from(123.45f32));
assert_eq!(f, VV::from(123.45f32));
assert_eq!(f.as_float().unwrap(), 123.45f32);
}
#[test] fn double_mut() {
let mut f = Value::from(1.0);
let mut f = VV::from(1.0);
assert!(f.is_double());
*(f.as_double_mut().unwrap()) = 123.45;
assert_eq!(f, Value::from(123.45));
assert_eq!(f, VV::from(123.45));
assert_eq!(f.as_double().unwrap(), 123.45);
}
#[test] fn signedinteger_mut() {
let mut i = Value::from(123);
let mut i = VV::from(123);
assert!(i.is_signedinteger());
*(i.as_signedinteger_mut().unwrap()) = BigInt::from(234);
assert_eq!(i, Value::from(234));
assert_eq!(i, VV::from(234));
use num::traits::cast::ToPrimitive;
assert_eq!(i.as_signedinteger().unwrap().to_i64().unwrap(), 234);
}
#[test] fn string_mut() {
let mut s = Value::from("hello, world!");
let mut s = VV::from("hello, world!");
assert!(s.is_string());
s.as_string_mut().unwrap().replace_range(7..12, "there");
assert_eq!(s, Value::from("hello, there!"));
assert_eq!(s, VV::from("hello, there!"));
}
#[test] fn bytes_mut() {
let mut b = Value::from(&b"hello, world!"[..]);
let mut b = VV::from(&b"hello, world!"[..]);
assert!(b.is_bytestring());
b.as_bytestring_mut().unwrap().splice(7..12, Vec::from(&b"there"[..]));
assert_eq!(b, Value::from(&b"hello, there!"[..]));
assert_eq!(b, VV::from(&b"hello, there!"[..]));
}
#[test] fn symbol_mut() {
let mut s = Value::symbol("abcd");
let mut s = VV::symbol("abcd");
assert!(s.is_symbol());
s.as_symbol_mut().unwrap().replace_range(..2, "AB");
assert_eq!(s, Value::symbol("ABcd"));
assert_eq!(s, VV::symbol("ABcd"));
}
#[test] fn record_mut() {
let says = Value::symbol("says").wrap();
let mut r = Value::record(says.clone(), vec![Value::from("Tony").wrap(), Value::from("Hello!").wrap()]);
r.as_record_mut().unwrap().1[0] = Value::from("Alice").wrap();
assert_eq!(r, Value::record(says, vec![Value::from("Alice").wrap(), Value::from("Hello!").wrap()]));
let says = VV::symbol("says").wrap();
let mut r = VV::record(says.clone(), vec![VV::from("Tony").wrap(), VV::from("Hello!").wrap()]);
r.as_record_mut().unwrap().1[0] = VV::from("Alice").wrap();
assert_eq!(r, VV::record(says, vec![VV::from("Alice").wrap(), VV::from("Hello!").wrap()]));
}
#[test] fn sequence_mut() {
let mut s = Value::Sequence(vec![Value::from(1).wrap(),
Value::from(2).wrap(),
Value::from(3).wrap()]);
let r = Value::Sequence(vec![Value::from(1).wrap(),
Value::from(99).wrap(),
Value::from(3).wrap()]);
s.as_sequence_mut().unwrap()[1] = Value::from(99).wrap();
let mut s = VV::Sequence(vec![VV::from(1).wrap(),
VV::from(2).wrap(),
VV::from(3).wrap()]);
let r = VV::Sequence(vec![VV::from(1).wrap(),
VV::from(99).wrap(),
VV::from(3).wrap()]);
s.as_sequence_mut().unwrap()[1] = VV::from(99).wrap();
assert_eq!(r, s);
}
}
@ -177,11 +178,11 @@ mod value_tests {
#[cfg(test)]
mod decoder_tests {
use crate::value::Decoder;
use crate::value::Value;
use crate::value::{Value, AValue, NestedValue};
#[test] fn read_123() {
let mut buf = &b"abc"[..];
let mut d = Decoder::new(&mut buf, None);
let mut d = Decoder::<_, AValue>::new(&mut buf, None);
assert_eq!(d.read().ok(), Some(97));
assert_eq!(d.read().ok(), Some(98));
assert_eq!(d.read().ok(), Some(99));
@ -190,7 +191,7 @@ mod decoder_tests {
#[test] fn skip_annotations_noskip() {
let mut buf = &b"\x0521"[..];
let mut d = Decoder::new(&mut buf, None);
let mut d = Decoder::<_, AValue>::new(&mut buf, None);
let v = d.next().unwrap();
assert_eq!(v.annotations().len(), 1);
assert_eq!(v.annotations()[0], Value::from(2).wrap());
@ -199,7 +200,7 @@ mod decoder_tests {
#[test] fn skip_annotations_skip() {
let mut buf = &b"\x0521"[..];
let mut d = Decoder::new(&mut buf, None);
let mut d = Decoder::<_, AValue>::new(&mut buf, None);
d.set_read_annotations(false);
let v = d.next().unwrap();
assert_eq!(v.annotations().len(), 0);
@ -218,7 +219,7 @@ mod samples_tests {
use std::collections::BTreeMap;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct ExpectedPlaceholderMapping(DecodePlaceholderMap);
struct ExpectedPlaceholderMapping(DecodePlaceholderMap<AValue>);
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct TestCases {
@ -239,7 +240,7 @@ mod samples_tests {
#[test] fn run() -> codec::Result<()> {
let mut fh = std::fs::File::open("../../tests/samples.bin").unwrap();
let mut d = Decoder::new(&mut fh, None);
let mut d = Decoder::<_, AValue>::new(&mut fh, None);
let tests: TestCases = from_value(&d.next().unwrap()).unwrap();
// println!("{:#?}", tests);

View File

@ -1,17 +1,17 @@
use crate::value::Value;
use crate::value::{Value, AValue};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Symbol(pub String);
impl serde::Serialize for Symbol {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
Value::symbol(&self.0).serialize(serializer)
Value::<AValue>::symbol(&self.0).serialize(serializer)
}
}
impl<'de> serde::Deserialize<'de> for Symbol {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
let v = Value::deserialize(deserializer)?;
let v = Value::<AValue>::deserialize(deserializer)?;
let s = v.as_symbol().ok_or(serde::de::Error::custom("Expected symbol"))?;
Ok(Symbol(s.clone()))
}

View File

@ -1,4 +1,4 @@
use crate::value::{decoder, encoder, invert_map, AValue};
use crate::value::{decoder, encoder, invert_map, NestedValue};
use decoder::{Decoder, DecodePlaceholderMap};
use encoder::{Encoder, EncodePlaceholderMap};
use std::io::{Read, Write};
@ -6,13 +6,13 @@ use std::io::{Read, Write};
pub type Error = decoder::Error;
pub type Result<T> = decoder::Result<T>;
pub struct Codec {
pub decode_placeholders: Option<DecodePlaceholderMap>,
pub encode_placeholders: Option<EncodePlaceholderMap>,
pub struct Codec<N: NestedValue> {
pub decode_placeholders: Option<DecodePlaceholderMap<N>>,
pub encode_placeholders: Option<EncodePlaceholderMap<N>>,
}
impl Codec {
pub fn new(decode_placeholders: DecodePlaceholderMap) -> Self {
impl<N: NestedValue> Codec<N> {
pub fn new(decode_placeholders: DecodePlaceholderMap<N>) -> Self {
let encode_placeholders = Some(invert_map(&decode_placeholders));
Codec { decode_placeholders: Some(decode_placeholders), encode_placeholders }
}
@ -21,19 +21,19 @@ impl Codec {
Codec { decode_placeholders: None, encode_placeholders: None }
}
pub fn decoder<'a, 'r, R: Read>(&'a self, read: &'r mut R) -> Decoder<'r, 'a, R> {
pub fn decoder<'a, 'r, R: Read>(&'a self, read: &'r mut R) -> Decoder<'r, 'a, R, N> {
Decoder::new(read, self.decode_placeholders.as_ref())
}
pub fn encoder<'a, 'w, W: Write>(&'a self, write: &'w mut W) -> Encoder<'w, 'a, W> {
pub fn encoder<'a, 'w, W: Write>(&'a self, write: &'w mut W) -> Encoder<'w, 'a, W, N> {
Encoder::new(write, self.encode_placeholders.as_ref())
}
pub fn decode<'r, R: Read>(&self, read: &'r mut R) -> Result<AValue> {
pub fn decode<'r, R: Read>(&self, read: &'r mut R) -> Result<N> {
self.decoder(read).next()
}
pub fn encode_bytes(&self, v: &AValue) -> Result<Vec<u8>> {
pub fn encode_bytes(&self, v: &N) -> Result<Vec<u8>> {
let mut buf: Vec<u8> = Vec::new();
self.encoder(&mut buf).write(v)?;
Ok(buf)

View File

@ -1,4 +1,4 @@
use crate::value::{Value, AValue, Dictionary};
use crate::value::{Value, NestedValue};
use crate::value::value::{Float, Double};
use crate::value::error::{Error, Result, ExpectedKind};
use num::traits::cast::ToPrimitive;
@ -6,30 +6,31 @@ use serde::Deserialize;
use serde::de::{Visitor, SeqAccess, MapAccess, EnumAccess, VariantAccess, DeserializeSeed};
use std::convert::TryFrom;
use std::iter::Iterator;
use std::collections::BTreeMap as Map;
pub struct Deserializer<'de> {
input: &'de AValue,
pub struct Deserializer<'de, N: NestedValue> {
input: &'de N,
}
pub fn from_value<'a, T>(v: &'a AValue) -> Result<T> where T: Deserialize<'a> {
pub fn from_value<'a, T, N: NestedValue>(v: &'a N) -> Result<T> where T: Deserialize<'a> {
let mut de = Deserializer::from_value(v);
let t = T::deserialize(&mut de)?;
Ok(t)
}
impl<'de> Deserializer<'de> {
pub fn from_value(v: &'de AValue) -> Self {
impl<'de, N: NestedValue> Deserializer<'de, N> {
pub fn from_value(v: &'de N) -> Self {
Deserializer{ input: v }
}
fn check<'a, T, F>(&'a mut self, f: F, k: ExpectedKind) -> Result<T>
where F: FnOnce(&'de Value) -> Option<T>
where F: FnOnce(&'de Value<N>) -> 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, self.input.copy_via()))
}
}
impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> {
impl<'de, 'a, N: NestedValue> serde::de::Deserializer<'de> for &'a mut Deserializer<'de, N> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
@ -138,7 +139,7 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> {
let fs = self.check(|v| v.as_simple_record("UnicodeScalar", Some(1)),
ExpectedKind::SimpleRecord("UnicodeScalar", Some(1)))?;
let c = fs[0].value().as_u32()
.ok_or_else(|| Error::Expected(ExpectedKind::SignedInteger, self.input.clone()))?;
.ok_or_else(|| Error::Expected(ExpectedKind::SignedInteger, self.input.copy_via()))?;
visitor.visit_char(char::try_from(c).or(Err(Error::InvalidUnicodeScalar(c)))?)
}
@ -171,7 +172,7 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> {
self.input = &fs[0];
visitor.visit_some(self)
}
None => Err(Error::Expected(ExpectedKind::Option, self.input.clone()))
None => Err(Error::Expected(ExpectedKind::Option, self.input.copy_via()))
}
}
}
@ -181,7 +182,7 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> {
if self.input.value().is_simple_record("tuple", Some(0)) {
visitor.visit_unit()
} else {
Err(Error::Expected(ExpectedKind::SimpleRecord("tuple", Some(0)), self.input.clone()))
Err(Error::Expected(ExpectedKind::SimpleRecord("tuple", Some(0)), self.input.copy_via()))
}
}
@ -191,7 +192,7 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> {
if self.input.value().is_simple_record(name, Some(0)) {
visitor.visit_unit()
} else {
Err(Error::Expected(ExpectedKind::SimpleRecord(name, Some(0)), self.input.clone()))
Err(Error::Expected(ExpectedKind::SimpleRecord(name, Some(0)), self.input.copy_via()))
}
}
@ -267,19 +268,19 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> {
}
}
pub struct VecSeq<'a, 'de: 'a> {
pub struct VecSeq<'a, 'de: 'a, N: NestedValue> {
index: usize,
vec: &'de Vec<AValue>,
de: &'a mut Deserializer<'de>,
vec: &'de Vec<N>,
de: &'a mut Deserializer<'de, N>,
}
impl<'de, 'a> VecSeq<'a, 'de> {
fn new(de: &'a mut Deserializer<'de>, vec: &'de Vec<AValue>) -> Self {
impl<'de, 'a, N: NestedValue> VecSeq<'a, 'de, N> {
fn new(de: &'a mut Deserializer<'de, N>, vec: &'de Vec<N>) -> Self {
VecSeq{ index: 0, vec, de }
}
}
impl<'de, 'a> SeqAccess<'de> for VecSeq<'a, 'de> {
impl<'de, 'a, N: NestedValue> SeqAccess<'de> for VecSeq<'a, 'de, N> {
type Error = Error;
fn next_element_seed<T>(&mut self, seed: T)
@ -296,19 +297,19 @@ impl<'de, 'a> SeqAccess<'de> for VecSeq<'a, 'de> {
}
}
pub struct DictMap<'a, 'de: 'a> {
pending: Option<&'de AValue>,
iter: Box<dyn Iterator<Item = (&'de AValue, &'de AValue)> + 'a>,
de: &'a mut Deserializer<'de>,
pub struct DictMap<'a, 'de: 'a, N: NestedValue> {
pending: Option<&'de N>,
iter: Box<dyn Iterator<Item = (&'de N, &'de N)> + 'a>,
de: &'a mut Deserializer<'de, N>,
}
impl<'de, 'a> DictMap<'a, 'de> {
fn new(de: &'a mut Deserializer<'de>, d: &'de Dictionary) -> Self {
impl<'de, 'a, N: NestedValue> DictMap<'a, 'de, N> {
fn new(de: &'a mut Deserializer<'de, N>, d: &'de Map<N, N>) -> Self {
DictMap{ pending: None, iter: Box::new(d.into_iter()), de }
}
}
impl<'de, 'a> MapAccess<'de> for DictMap<'a, 'de> {
impl<'de, 'a, N: NestedValue> MapAccess<'de> for DictMap<'a, 'de, N> {
type Error = Error;
fn next_key_seed<K>(&mut self, seed: K)
@ -332,7 +333,7 @@ impl<'de, 'a> MapAccess<'de> for DictMap<'a, 'de> {
}
}
impl<'a, 'de> EnumAccess<'de> for &'a mut Deserializer<'de> {
impl<'a, 'de, N: NestedValue> EnumAccess<'de> for &'a mut Deserializer<'de, N> {
type Error = Error;
type Variant = Self;
@ -348,7 +349,7 @@ impl<'a, 'de> EnumAccess<'de> for &'a mut Deserializer<'de> {
}
}
impl<'a, 'de> VariantAccess<'de> for &'a mut Deserializer<'de> {
impl<'a, 'de, N: NestedValue> VariantAccess<'de> for &'a mut Deserializer<'de, N> {
type Error = Error;
fn unit_variant(self) -> Result<()> {

View File

@ -1,7 +1,9 @@
use std::io::{Read, ErrorKind};
use std::collections::BTreeSet as Set;
use std::collections::BTreeMap as Map;
use std::convert::TryInto;
use std::convert::TryFrom;
use crate::value::value::{Value, AValue, Set, Dictionary};
use crate::value::{Value, NestedValue};
use num::bigint::BigInt;
use crate::value::constants::{Op, InvalidOp, AtomMinor, CompoundMinor};
@ -48,13 +50,13 @@ impl Error {
pub fn is_eof(&self) -> bool { if let Error::Eof = *self { true } else { false } }
}
pub type DecodePlaceholderMap = std::collections::BTreeMap<usize, Value>;
pub type DecodePlaceholderMap<N> = std::collections::BTreeMap<usize, Value<N>>;
pub struct Decoder<'a, 'b, R: Read> {
pub struct Decoder<'a, 'b, R: Read, N: NestedValue> {
read: &'a mut R,
index: usize,
buf: Box<Option<Option<u8>>>,
placeholders: Option<&'b DecodePlaceholderMap>,
placeholders: Option<&'b DecodePlaceholderMap<N>>,
read_annotations: bool,
}
@ -62,8 +64,8 @@ fn decodeint(bs: &[u8]) -> BigInt {
BigInt::from_signed_bytes_be(bs)
}
impl<'a, 'b, R: Read> Decoder<'a, 'b, R> {
pub fn new(read: &'a mut R, placeholders: Option<&'b DecodePlaceholderMap>) -> Self {
impl<'a, 'b, R: Read, N: NestedValue> Decoder<'a, 'b, R, N> {
pub fn new(read: &'a mut R, placeholders: Option<&'b DecodePlaceholderMap<N>>) -> Self {
Decoder{
read,
index: 0,
@ -132,8 +134,8 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> {
}
}
pub fn readvalues(&mut self, mut count: usize) -> Result<Vec<AValue>> {
let mut pieces: Vec<AValue> = Vec::with_capacity(count);
pub fn readvalues(&mut self, mut count: usize) -> Result<Vec<N>> {
let mut pieces: Vec<N> = Vec::with_capacity(count);
while count > 0 {
pieces.push(self.next()?);
count = count - 1;
@ -175,7 +177,7 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> {
}
}
pub fn decodebinary(minor: AtomMinor, bs: Vec<u8>) -> Result<AValue> {
pub fn decodebinary(minor: AtomMinor, bs: Vec<u8>) -> Result<N> {
Ok(match minor {
AtomMinor::SignedInteger => Value::from(decodeint(&bs)).wrap(),
AtomMinor::String => Value::from(std::str::from_utf8(&bs)?).wrap(),
@ -184,7 +186,7 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> {
})
}
pub fn decodecompound(minor: CompoundMinor, mut pieces: Vec<AValue>) -> Result<AValue> {
pub fn decodecompound(minor: CompoundMinor, mut pieces: Vec<N>) -> Result<N> {
match minor {
CompoundMinor::Record =>
if pieces.len() == 0 {
@ -193,7 +195,7 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> {
let label = pieces.remove(0);
Ok(Value::record(label, pieces).wrap())
},
CompoundMinor::Sequence => Ok(Value::from(pieces).wrap()),
CompoundMinor::Sequence => Ok(Value::Sequence(pieces).wrap()),
CompoundMinor::Set => {
let mut s = Set::new();
while let Some(v) = pieces.pop() {
@ -205,7 +207,7 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> {
if pieces.len() % 2 != 0 {
Err(Error::Syntax("Missing dictionary value"))
} else {
let mut d = Dictionary::new();
let mut d = Map::new();
while let Some(v) = pieces.pop() {
let k = pieces.pop().unwrap();
d.insert(k, v);
@ -215,10 +217,10 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> {
}
}
pub fn binarystream(&mut self, minor: AtomMinor) -> Result<AValue> {
pub fn binarystream(&mut self, minor: AtomMinor) -> Result<N> {
let mut bs: Vec<u8> = Vec::new();
while !self.peekend()? {
match self.next()?.1.as_bytestring() {
match self.next()?.value().as_bytestring() {
Some(chunk) => bs.extend_from_slice(chunk),
None => return Err(Error::Syntax("Unexpected non-binary chunk")),
}
@ -226,15 +228,15 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> {
Self::decodebinary(minor, bs)
}
pub fn valuestream(&mut self, minor: CompoundMinor) -> Result<AValue> {
let mut pieces: Vec<AValue> = Vec::new();
pub fn valuestream(&mut self, minor: CompoundMinor) -> Result<N> {
let mut pieces: Vec<N> = Vec::new();
while !self.peekend()? {
pieces.push(self.next()?);
}
Self::decodecompound(minor, pieces)
}
pub fn next(&mut self) -> Result<AValue> {
pub fn next(&mut self) -> Result<N> {
match self.nextop()? {
(Op::Misc(0), 0) => Ok(Value::from(false).wrap()),
(Op::Misc(0), 1) => Ok(Value::from(true).wrap()),

View File

@ -1,5 +1,5 @@
use std::io::Write;
use crate::value::value::{Value, AValue, Float, Double};
use crate::value::value::{Value, NestedValue, Float, Double};
use num::bigint::BigInt;
use num::cast::ToPrimitive;
use crate::value::constants::{Op, AtomMinor, CompoundMinor};
@ -7,15 +7,15 @@ use crate::value::constants::{Op, AtomMinor, CompoundMinor};
pub type Error = std::io::Error;
pub type Result = std::result::Result<(), Error>;
pub type EncodePlaceholderMap = std::collections::BTreeMap<Value, usize>;
pub type EncodePlaceholderMap<N> = std::collections::BTreeMap<Value<N>, usize>;
pub struct Encoder<'a, 'b, W: Write> {
pub struct Encoder<'a, 'b, W: Write, N: NestedValue> {
write: &'a mut W,
placeholders: Option<&'b EncodePlaceholderMap>,
placeholders: Option<&'b EncodePlaceholderMap<N>>,
}
impl<'a, 'b, W: Write> Encoder<'a, 'b, W> {
pub fn new(write: &'a mut W, placeholders: Option<&'b EncodePlaceholderMap>) -> Self {
impl<'a, 'b, W: Write, N: NestedValue> Encoder<'a, 'b, W, N> {
pub fn new(write: &'a mut W, placeholders: Option<&'b EncodePlaceholderMap<N>>) -> Self {
Encoder{ write, placeholders }
}
@ -70,7 +70,7 @@ impl<'a, 'b, W: Write> Encoder<'a, 'b, W> {
self.write_all(bs)
}
pub fn write(&mut self, v: &AValue) -> Result {
pub fn write(&mut self, v: &N) -> Result {
for ann in v.annotations() {
self.write_header(Op::Misc(0), 5)?;
self.write(ann)?;
@ -78,7 +78,7 @@ impl<'a, 'b, W: Write> Encoder<'a, 'b, W> {
self.write_value(v.value())
}
pub fn write_value(&mut self, v: &Value) -> Result {
pub fn write_value(&mut self, v: &Value<N>) -> Result {
match self.placeholders.and_then(|m| m.get(v)) {
Some(&n) => self.write_header(Op::Misc(1), n),
None => match v {

View File

@ -16,10 +16,12 @@ pub use encoder::EncodePlaceholderMap;
pub use encoder::Encoder;
pub use ser::Serializer;
pub use ser::to_value;
pub use value::AValue;
pub use value::Dictionary;
pub use value::Set;
pub use value::NestedValue;
pub use value::Value;
pub use value::AValue;
pub type Set = std::collections::BTreeSet<AValue>;
pub type Dictionary = std::collections::BTreeMap<AValue, AValue>;
pub fn invert_map<A, B>(m: &std::collections::BTreeMap<A, B>) -> std::collections::BTreeMap<B, A>
where A: Clone, B: Clone + Ord

View File

@ -1,4 +1,4 @@
use crate::value::{Value, AValue, Dictionary};
use crate::value::{Value, AValue, NestedValue, Dictionary};
use serde::Serialize;
use crate::value::error::{Error, Result};

View File

@ -1,17 +1,48 @@
use std::hash::{Hash,Hasher};
use std::cmp::{Ordering};
use std::fmt::Debug;
use num::bigint::BigInt;
use std::vec::Vec;
use std::string::String;
use std::collections::BTreeSet;
use std::collections::BTreeMap;
use std::collections::BTreeSet as Set;
use std::collections::BTreeMap as Map;
use std::ops::Deref;
use std::ops::Index;
use std::ops::IndexMut;
use num::traits::cast::ToPrimitive;
pub trait NestedValue: Sized + Debug + Clone + Eq + Hash + Ord {
type BoxType: Sized + Debug + Clone + Eq + Hash + Ord + Deref<Target=Self>;
fn wrap(v: Value<Self>) -> Self;
fn wrap_ann(anns: Vec<Self>, v: Value<Self>) -> Self;
fn boxwrap(self) -> Self::BoxType;
fn boxunwrap(b: &Self::BoxType) -> &Self;
fn annotations(&self) -> &Vec<Self>;
fn annotations_mut(&mut self) -> &mut Vec<Self>;
fn value(&self) -> &Value<Self>;
fn value_mut(&mut self) -> &mut Value<Self>;
fn value_owned(self) -> Value<Self>;
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for ann in self.annotations() {
write!(f, "@{:?} ", ann)?;
}
self.value().fmt(f)
}
fn copy_via<M: NestedValue>(&self) -> M {
M::wrap_ann(self.annotations().iter().map(|a| a.copy_via()).collect(),
self.value().copy_via())
}
}
/// The `Value`s from the specification.
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Value {
pub enum Value<N> where N: NestedValue {
Boolean(bool),
Float(Float),
Double(Double),
@ -19,16 +50,56 @@ pub enum Value {
String(String),
ByteString(Vec<u8>),
Symbol(String),
Record(Record),
Sequence(Vec<AValue>),
Set(Set),
Dictionary(Dictionary),
Record(Record<N>),
Sequence(Vec<N>),
Set(Set<N>),
Dictionary(Map<N, N>),
}
/// An possibly-annotated Value, with annotations (themselves
/// possibly-annotated) in order of appearance.
#[derive(Clone)]
pub struct AValue(pub Vec<AValue>, pub Value);
pub struct AValue(pub Vec<AValue>, pub Value<AValue>);
impl NestedValue for AValue {
type BoxType = Box<Self>;
fn wrap(v: Value<Self>) -> Self {
Self::wrap_ann(Vec::new(), v)
}
fn wrap_ann(anns: Vec<Self>, v: Value<Self>) -> Self {
AValue(anns, v)
}
fn boxwrap(self) -> Self::BoxType {
Box::new(self)
}
fn boxunwrap(b: &Self::BoxType) -> &Self {
&**b
}
fn annotations(&self) -> &Vec<Self> {
&self.0
}
fn annotations_mut(&mut self) -> &mut Vec<Self> {
&mut self.0
}
fn value(&self) -> &Value<Self> {
&self.1
}
fn value_mut(&mut self) -> &mut Value<Self> {
&mut self.1
}
fn value_owned(self) -> Value<Self> {
self.1
}
}
/// Single-precision IEEE 754 Value
#[derive(Clone, Debug)]
@ -39,11 +110,7 @@ pub struct Float(pub f32);
pub struct Double(pub f64);
/// A Record `Value`
pub type Record = (Box<AValue>, Vec<AValue>);
pub type Set = BTreeSet<AValue>;
pub type Dictionary = BTreeMap<AValue, AValue>;
pub type Record<N> = (<N as NestedValue>::BoxType, Vec<N>);
impl From<f32> for Float {
fn from(v: f32) -> Self {
@ -131,7 +198,7 @@ impl Eq for Double {}
impl PartialEq for AValue {
fn eq(&self, other: &Self) -> bool {
self.1.eq(&other.1)
self.value().eq(other.value())
}
}
@ -151,38 +218,44 @@ impl PartialOrd for AValue {
impl Ord for AValue {
fn cmp(&self, other: &Self) -> Ordering {
self.1.cmp(&other.1)
self.value().cmp(&other.value())
}
}
impl From<bool> for Value { fn from(v: bool) -> Self { Value::Boolean(v) } }
impl Debug for AValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.debug_fmt(f)
}
}
impl From<f32> for Value { fn from(v: f32) -> Self { Value::Float(Float::from(v)) } }
impl From<f64> for Value { fn from(v: f64) -> Self { Value::Double(Double::from(v)) } }
impl<N: NestedValue> From<bool> for Value<N> { fn from(v: bool) -> Self { Value::Boolean(v) } }
impl From<u8> for Value { fn from(v: u8) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl From<i8> for Value { fn from(v: i8) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl From<u16> for Value { fn from(v: u16) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl From<i16> for Value { fn from(v: i16) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl From<u32> for Value { fn from(v: u32) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl From<i32> for Value { fn from(v: i32) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl From<u64> for Value { fn from(v: u64) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl From<i64> for Value { fn from(v: i64) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl From<u128> for Value { fn from(v: u128) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl From<i128> for Value { fn from(v: i128) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl From<BigInt> for Value { fn from(v: BigInt) -> Self { Value::SignedInteger(v) } }
impl<N: NestedValue> From<f32> for Value<N> { fn from(v: f32) -> Self { Value::Float(Float::from(v)) } }
impl<N: NestedValue> From<f64> for Value<N> { fn from(v: f64) -> Self { Value::Double(Double::from(v)) } }
impl From<&str> for Value { fn from(v: &str) -> Self { Value::String(String::from(v)) } }
impl From<String> for Value { fn from(v: String) -> Self { Value::String(v) } }
impl<N: NestedValue> From<u8> for Value<N> { fn from(v: u8) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl<N: NestedValue> From<i8> for Value<N> { fn from(v: i8) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl<N: NestedValue> From<u16> for Value<N> { fn from(v: u16) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl<N: NestedValue> From<i16> for Value<N> { fn from(v: i16) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl<N: NestedValue> From<u32> for Value<N> { fn from(v: u32) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl<N: NestedValue> From<i32> for Value<N> { fn from(v: i32) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl<N: NestedValue> From<u64> for Value<N> { fn from(v: u64) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl<N: NestedValue> From<i64> for Value<N> { fn from(v: i64) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl<N: NestedValue> From<u128> for Value<N> { fn from(v: u128) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl<N: NestedValue> From<i128> for Value<N> { fn from(v: i128) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl<N: NestedValue> From<BigInt> for Value<N> { fn from(v: BigInt) -> Self { Value::SignedInteger(v) } }
impl From<&[u8]> for Value { fn from(v: &[u8]) -> Self { Value::ByteString(Vec::from(v)) } }
impl From<Vec<u8>> for Value { fn from(v: Vec<u8>) -> Self { Value::ByteString(v) } }
impl<N: NestedValue> From<&str> for Value<N> { fn from(v: &str) -> Self { Value::String(String::from(v)) } }
impl<N: NestedValue> From<String> for Value<N> { fn from(v: String) -> Self { Value::String(v) } }
impl From<Vec<AValue>> for Value { fn from(v: Vec<AValue>) -> Self { Value::Sequence(v) } }
impl From<Set> for Value { fn from(v: Set) -> Self { Value::Set(v) } }
impl From<Dictionary> for Value { fn from(v: Dictionary) -> Self { Value::Dictionary(v) } }
impl<N: NestedValue> From<&[u8]> for Value<N> { fn from(v: &[u8]) -> Self { Value::ByteString(Vec::from(v)) } }
impl<N: NestedValue> From<Vec<u8>> for Value<N> { fn from(v: Vec<u8>) -> Self { Value::ByteString(v) } }
impl std::fmt::Debug for Value {
impl<N: NestedValue> From<Vec<N>> for Value<N> { fn from(v: Vec<N>) -> Self { Value::Sequence(v) } }
impl<N: NestedValue> From<Set<N>> for Value<N> { fn from(v: Set<N>) -> Self { Value::Set(v) } }
impl<N: NestedValue> From<Map<N, N>> for Value<N> { fn from(v: Map<N, N>) -> Self { Value::Dictionary(v) } }
impl<N: NestedValue> Debug for Value<N> {
// Not *quite* a formatter for the Preserves text syntax, since it
// doesn't escape strings/symbols properly.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -220,43 +293,16 @@ impl std::fmt::Debug for Value {
f.write_str("#set")?;
f.debug_set().entries(v.iter()).finish()
}
Value::Dictionary (ref v) => f.debug_map().entries(v.iter()).finish()
Value::Dictionary(ref v) => f.debug_map().entries(v.iter()).finish()
}
}
}
impl std::fmt::Debug for AValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for ann in &self.0 {
write!(f, "@{:?} ", ann)?;
}
self.1.fmt(f)
}
}
//---------------------------------------------------------------------------
impl AValue {
pub fn annotations(&self) -> &Vec<AValue> {
&self.0
}
pub fn annotations_mut(&mut self) -> &mut Vec<AValue> {
&mut self.0
}
pub fn value(&self) -> &Value {
&self.1
}
pub fn value_mut(&mut self) -> &mut Value {
&mut self.1
}
}
impl Value {
pub fn wrap(self) -> AValue {
AValue(Vec::new(), self)
impl<N: NestedValue> Value<N> {
pub fn wrap(self) -> N {
N::wrap(self)
}
pub fn is_boolean(&self) -> bool {
@ -388,7 +434,7 @@ impl Value {
}
}
pub fn symbol(s: &str) -> Value {
pub fn symbol(s: &str) -> Value<N> {
Value::Symbol(s.to_string())
}
@ -412,15 +458,15 @@ impl Value {
}
}
pub fn record(label: AValue, fields: Vec<AValue>) -> Value {
Value::Record((Box::new(label), fields))
pub fn record(label: N, fields: Vec<N>) -> Value<N> {
Value::Record((label.boxwrap(), fields))
}
pub fn is_record(&self) -> bool {
self.as_record().is_some()
}
pub fn as_record(&self) -> Option<&Record> {
pub fn as_record(&self) -> Option<&Record<N>> {
if let Value::Record(ref r) = *self {
Some(r)
} else {
@ -428,7 +474,7 @@ impl Value {
}
}
pub fn as_record_mut(&mut self) -> Option<&mut Record> {
pub fn as_record_mut(&mut self) -> Option<&mut Record<N>> {
if let Value::Record(ref mut r) = *self {
Some(r)
} else {
@ -436,7 +482,7 @@ impl Value {
}
}
pub fn simple_record(label: &str, fields: Vec<AValue>) -> Value {
pub fn simple_record(label: &str, fields: Vec<N>) -> Value<N> {
Value::record(Value::symbol(label).wrap(), fields)
}
@ -444,7 +490,7 @@ impl Value {
self.as_simple_record(label, arity).is_some()
}
pub fn as_simple_record(&self, label: &str, arity: Option<usize>) -> Option<&Vec<AValue>> {
pub fn as_simple_record(&self, label: &str, arity: Option<usize>) -> Option<&Vec<N>> {
self.as_record().and_then(|(lp,fs)| {
match *lp.value() {
Value::Symbol(ref s) if s == label =>
@ -462,7 +508,7 @@ impl Value {
self.as_sequence().is_some()
}
pub fn as_sequence(&self) -> Option<&Vec<AValue>> {
pub fn as_sequence(&self) -> Option<&Vec<N>> {
if let Value::Sequence(ref s) = *self {
Some(s)
} else {
@ -470,7 +516,7 @@ impl Value {
}
}
pub fn as_sequence_mut(&mut self) -> Option<&mut Vec<AValue>> {
pub fn as_sequence_mut(&mut self) -> Option<&mut Vec<N>> {
if let Value::Sequence(ref mut s) = *self {
Some(s)
} else {
@ -482,7 +528,7 @@ impl Value {
self.as_set().is_some()
}
pub fn as_set(&self) -> Option<&Set> {
pub fn as_set(&self) -> Option<&Set<N>> {
if let Value::Set(ref s) = *self {
Some(s)
} else {
@ -490,7 +536,7 @@ impl Value {
}
}
pub fn as_set_mut(&mut self) -> Option<&mut Set> {
pub fn as_set_mut(&mut self) -> Option<&mut Set<N>> {
if let Value::Set(ref mut s) = *self {
Some(s)
} else {
@ -502,7 +548,7 @@ impl Value {
self.as_dictionary().is_some()
}
pub fn as_dictionary(&self) -> Option<&Dictionary> {
pub fn as_dictionary(&self) -> Option<&Map<N, N>> {
if let Value::Dictionary(ref s) = *self {
Some(s)
} else {
@ -510,34 +556,52 @@ impl Value {
}
}
pub fn as_dictionary_mut(&mut self) -> Option<&mut Dictionary> {
pub fn as_dictionary_mut(&mut self) -> Option<&mut Map<N, N>> {
if let Value::Dictionary(ref mut s) = *self {
Some(s)
} else {
None
}
}
pub fn copy_via<M: NestedValue>(&self) -> Value<M> {
match self {
Value::Boolean(b) => Value::Boolean(*b),
Value::Float(f) => Value::Float(f.clone()),
Value::Double(d) => Value::Double(d.clone()),
Value::SignedInteger(i) => Value::SignedInteger(i.clone()),
Value::String(ref s) => Value::String(s.clone()),
Value::ByteString(ref v) => Value::ByteString(v.clone()),
Value::Symbol(ref v) => Value::String(v.clone()),
Value::Record((ref l, ref fs)) =>
Value::Record((N::boxunwrap(l).copy_via::<M>().boxwrap(), fs.iter().map(|a| a.copy_via()).collect())),
Value::Sequence(ref v) => Value::Sequence(v.iter().map(|a| a.copy_via()).collect()),
Value::Set(ref v) => Value::Set(v.iter().map(|a| a.copy_via()).collect()),
Value::Dictionary(ref v) =>
Value::Dictionary(v.iter().map(|(a,b)| (a.copy_via(), b.copy_via())).collect()),
}
}
}
impl Index<usize> for Value {
type Output = AValue;
impl<N: NestedValue> Index<usize> for Value<N> {
type Output = N;
fn index(&self, i: usize) -> &Self::Output {
&self.as_sequence().unwrap()[i]
}
}
impl IndexMut<usize> for Value {
impl<N: NestedValue> IndexMut<usize> for Value<N> {
fn index_mut(&mut self, i: usize) -> &mut Self::Output {
&mut self.as_sequence_mut().unwrap()[i]
}
}
impl Index<&AValue> for Value {
type Output = AValue;
impl<N: NestedValue> Index<&N> for Value<N> {
type Output = N;
fn index(&self, i: &AValue) -> &Self::Output {
&self.as_dictionary().unwrap()[i]
fn index(&self, i: &N) -> &Self::Output {
&(*self.as_dictionary().unwrap())[i]
}
}
@ -570,7 +634,7 @@ impl<'de> serde::Deserialize<'de> for ValueWrapper {
}
}
impl serde::Serialize for Value {
impl<N: NestedValue> serde::Serialize for Value<N> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
let mut buf: Vec<u8> = Vec::new();
crate::value::Encoder::new(&mut buf, None).write_value(self)
@ -588,12 +652,12 @@ impl serde::Serialize for AValue {
}
}
impl<'de> serde::Deserialize<'de> for Value {
impl<'de, N: NestedValue> serde::Deserialize<'de> for Value<N> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
let ValueWrapper(buf) = ValueWrapper::deserialize(deserializer)?;
let v = crate::value::Decoder::new(&mut &buf[..], None).next()
let v: N = crate::value::Decoder::new(&mut &buf[..], None).next()
.or(Err(serde::de::Error::custom("Internal error")))?;
Ok(v.value().clone())
Ok(v.value_owned())
}
}