Remove mostly-useless Domain methods
This commit is contained in:
parent
0fb1ef4efd
commit
83d15a838e
|
@ -1,6 +1,5 @@
|
||||||
use num::bigint::BigInt;
|
use num::bigint::BigInt;
|
||||||
use std::convert::From;
|
use std::convert::From;
|
||||||
use crate::value::IOValue;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
@ -19,7 +18,7 @@ pub enum Error {
|
||||||
pub enum Received {
|
pub enum Received {
|
||||||
ReceivedSomethingElse,
|
ReceivedSomethingElse,
|
||||||
ReceivedRecordWithLabel(String),
|
ReceivedRecordWithLabel(String),
|
||||||
ReceivedOtherValue(IOValue),
|
ReceivedOtherValue(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
|
|
|
@ -10,10 +10,7 @@ pub mod value;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod dom {
|
mod dom {
|
||||||
use super::value::{
|
use super::value::*;
|
||||||
Value, IOValue, IOResult, NestedValue, PlainValue, Domain,
|
|
||||||
PackedWriter,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Hash, Clone, Ord, PartialEq, Eq, PartialOrd)]
|
#[derive(Debug, Hash, Clone, Ord, PartialEq, Eq, PartialOrd)]
|
||||||
pub enum Dom {
|
pub enum Dom {
|
||||||
|
@ -21,17 +18,13 @@ mod dom {
|
||||||
Two,
|
Two,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Domain for Dom {
|
impl Domain for Dom {}
|
||||||
fn from_preserves(v: IOValue) -> IOResult<Self> {
|
|
||||||
panic!("Cannot decode IOValue to Dom: {:?}", v);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_preserves(&self) -> IOValue {
|
fn dom_as_preserves(v: &Dom) -> IOResult<UnwrappedIOValue> {
|
||||||
match self {
|
Ok(match v {
|
||||||
Dom::One => Value::ByteString(vec![255, 255, 255, 255]).wrap(),
|
Dom::One => Value::ByteString(vec![255, 255, 255, 255]),
|
||||||
Dom::Two => Value::symbol(&format!("Dom::{:?}", self)).wrap(),
|
Dom::Two => Value::symbol(&format!("Dom::{:?}", v)),
|
||||||
}
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn test_one() {
|
#[test] fn test_one() {
|
||||||
|
@ -39,7 +32,7 @@ mod dom {
|
||||||
Value::Embedded(Dom::One).wrap(),
|
Value::Embedded(Dom::One).wrap(),
|
||||||
Value::from(2).wrap()])
|
Value::from(2).wrap()])
|
||||||
.wrap();
|
.wrap();
|
||||||
assert_eq!(PackedWriter::encode(&v.to_io_value()).unwrap(),
|
assert_eq!(PackedWriter::encode(&v.copy_via(&mut dom_as_preserves).unwrap()).unwrap(),
|
||||||
[0xb5, 0x91, 0xb2, 0x04, 255, 255, 255, 255, 0x92, 0x84]);
|
[0xb5, 0x91, 0xb2, 0x04, 255, 255, 255, 255, 0x92, 0x84]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +41,7 @@ mod dom {
|
||||||
Value::Embedded(Dom::Two).wrap(),
|
Value::Embedded(Dom::Two).wrap(),
|
||||||
Value::from(2).wrap()])
|
Value::from(2).wrap()])
|
||||||
.wrap();
|
.wrap();
|
||||||
assert_eq!(PackedWriter::encode(&v.to_io_value()).unwrap(),
|
assert_eq!(PackedWriter::encode(&v.copy_via(&mut dom_as_preserves).unwrap()).unwrap(),
|
||||||
[0xb5, 0x91, 0xb3, 0x08, 68, 111, 109, 58, 58, 84, 119, 111, 0x92, 0x84]);
|
[0xb5, 0x91, 0xb3, 0x08, 68, 111, 109, 58, 58, 84, 119, 111, 0x92, 0x84]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ impl<'de> Deserializer<'de> {
|
||||||
where F: FnOnce(&'de UnwrappedIOValue) -> Option<T>
|
where F: FnOnce(&'de UnwrappedIOValue) -> Option<T>
|
||||||
{
|
{
|
||||||
f(self.input.value()).ok_or_else(
|
f(self.input.value()).ok_or_else(
|
||||||
|| Error::Expected(k, Received::ReceivedOtherValue(self.input.clone())))
|
|| Error::Expected(k, Received::ReceivedOtherValue(format!("{:?}", self.input))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -80,7 +80,7 @@ impl<'de, S: BinarySource<'de>> PackedReader<'de, S> {
|
||||||
|
|
||||||
fn expected(&mut self, k: ExpectedKind) -> error::Error {
|
fn expected(&mut self, k: ExpectedKind) -> error::Error {
|
||||||
match self.demand_next(true) {
|
match self.demand_next(true) {
|
||||||
Ok(v) => error::Error::Expected(k, Received::ReceivedOtherValue(v)),
|
Ok(v) => error::Error::Expected(k, Received::ReceivedOtherValue(format!("{:?}", v))),
|
||||||
Err(e) => e.into()
|
Err(e) => e.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,27 +17,14 @@ 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 super::IOResult;
|
|
||||||
use super::signed_integer::SignedInteger;
|
use super::signed_integer::SignedInteger;
|
||||||
use crate::error::{Error, ExpectedKind, Received};
|
use crate::error::{Error, ExpectedKind, Received};
|
||||||
|
|
||||||
pub trait Domain: Sized + Debug + Eq + Hash + Ord {
|
pub trait Domain: Sized + Debug + Eq + Hash + Ord {}
|
||||||
fn from_preserves(v: IOValue) -> IOResult<Self>;
|
|
||||||
fn as_preserves(&self) -> IOValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Embeddable: Domain + Clone {}
|
pub trait Embeddable: Domain + Clone {}
|
||||||
impl<T> Embeddable for T where T: Domain + Clone {}
|
impl<T> Embeddable for T where T: Domain + Clone {}
|
||||||
|
|
||||||
impl<D: Domain> Domain for Arc<D> {
|
impl<D: Domain> Domain for Arc<D> {}
|
||||||
fn from_preserves(v: IOValue) -> IOResult<Self> {
|
|
||||||
Ok(Arc::new(D::from_preserves(v)?))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_preserves(&self) -> IOValue {
|
|
||||||
D::as_preserves(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait NestedValue<D: Embeddable>: Sized + Debug + Clone + Eq + Hash + Ord {
|
pub trait NestedValue<D: Embeddable>: Sized + Debug + Clone + Eq + Hash + Ord {
|
||||||
fn new<V>(v: V) -> Self where Value<Self, D>: From<V> {
|
fn new<V>(v: V) -> Self where Value<Self, D>: From<V> {
|
||||||
|
@ -58,20 +45,12 @@ pub trait NestedValue<D: Embeddable>: Sized + Debug + Clone + Eq + Hash + Ord {
|
||||||
self.value().fmt(f)
|
self.value().fmt(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copy_via<M: NestedValue<E>, E: Embeddable, F>(&self, f: &F) -> IOResult<M>
|
fn copy_via<M: NestedValue<E>, E: Embeddable, F, Err>(&self, f: &mut F) -> Result<M, Err>
|
||||||
where
|
where
|
||||||
F: Fn(&D) -> IOResult<Value<M, E>>
|
F: FnMut(&D) -> Result<Value<M, E>, Err>
|
||||||
{
|
{
|
||||||
Ok(M::wrap(self.annotations().copy_via(f)?, self.value().copy_via(f)?))
|
Ok(M::wrap(self.annotations().copy_via(f)?, self.value().copy_via(f)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_io_value(&self) -> IOValue {
|
|
||||||
self.copy_via(&|d| Ok(d.as_preserves().value().clone())).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_io_value(v: &IOValue) -> IOResult<Self> {
|
|
||||||
v.copy_via(&|d| Ok(Value::Embedded(D::from_preserves(d.clone())?)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The `Value`s from the specification.
|
/// The `Value`s from the specification.
|
||||||
|
@ -304,7 +283,7 @@ impl<N: NestedValue<D>, D: Embeddable> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expected(&self, k: ExpectedKind) -> Error {
|
fn expected(&self, k: ExpectedKind) -> Error {
|
||||||
Error::Expected(k, Received::ReceivedOtherValue(self.clone().wrap().to_io_value()))
|
Error::Expected(k, Received::ReceivedOtherValue(format!("{:?}", self.clone().wrap())))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_boolean(&self) -> bool {
|
pub fn is_boolean(&self) -> bool {
|
||||||
|
@ -792,9 +771,9 @@ impl<N: NestedValue<D>, D: Embeddable> Value<N, D> {
|
||||||
self.as_embedded().ok_or_else(|| self.expected(ExpectedKind::Embedded))
|
self.as_embedded().ok_or_else(|| self.expected(ExpectedKind::Embedded))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn copy_via<M: NestedValue<E>, E: Embeddable, F>(&self, f: &F) -> IOResult<Value<M, E>>
|
pub fn copy_via<M: NestedValue<E>, E: Embeddable, F, Err>(&self, f: &mut F) -> Result<Value<M, E>, Err>
|
||||||
where
|
where
|
||||||
F: Fn(&D) -> IOResult<Value<M, E>>
|
F: FnMut(&D) -> Result<Value<M, E>, Err>
|
||||||
{
|
{
|
||||||
Ok(match self {
|
Ok(match self {
|
||||||
Value::Boolean(b) => Value::Boolean(*b),
|
Value::Boolean(b) => Value::Boolean(*b),
|
||||||
|
@ -808,12 +787,12 @@ impl<N: NestedValue<D>, D: Embeddable> Value<N, D> {
|
||||||
Value::Record(Record(r.fields_vec().iter().map(|a| a.copy_via(f))
|
Value::Record(Record(r.fields_vec().iter().map(|a| a.copy_via(f))
|
||||||
.collect::<Result<Vec<_>, _>>()?)),
|
.collect::<Result<Vec<_>, _>>()?)),
|
||||||
Value::Sequence(v) => Value::Sequence(v.iter().map(|a| a.copy_via(f))
|
Value::Sequence(v) => Value::Sequence(v.iter().map(|a| a.copy_via(f))
|
||||||
.collect::<IOResult<Vec<_>>>()?),
|
.collect::<Result<Vec<_>, _>>()?),
|
||||||
Value::Set(v) => Value::Set(v.iter().map(|a| a.copy_via(f))
|
Value::Set(v) => Value::Set(v.iter().map(|a| a.copy_via(f))
|
||||||
.collect::<IOResult<Set<_>>>()?),
|
.collect::<Result<Set<_>, _>>()?),
|
||||||
Value::Dictionary(v) =>
|
Value::Dictionary(v) =>
|
||||||
Value::Dictionary(v.iter().map(|(a,b)| Ok((a.copy_via(f)?, b.copy_via(f)?)))
|
Value::Dictionary(v.iter().map(|(a,b)| Ok((a.copy_via(f)?, b.copy_via(f)?)))
|
||||||
.collect::<IOResult<Map<_, _>>>()?),
|
.collect::<Result<Map<_, _>, _>>()?),
|
||||||
Value::Embedded(d) => f(d)?,
|
Value::Embedded(d) => f(d)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -856,19 +835,6 @@ impl<'de> serde::Deserialize<'de> for UnwrappedIOValue {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialize_nested_value<S, N, D: Embeddable>(v: &N, serializer: S) ->
|
|
||||||
Result<S::Ok, S::Error> where S: serde::Serializer, N: NestedValue<D> + serde::Serialize
|
|
||||||
{
|
|
||||||
super::magic::output_value(serializer, v.to_io_value())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deserialize_nested_value<'de, D, N, Dom: Embeddable>(deserializer: D) ->
|
|
||||||
Result<N, D::Error> where D: serde::Deserializer<'de>, N: NestedValue<Dom> + serde::Deserialize<'de>
|
|
||||||
{
|
|
||||||
N::from_io_value(&super::magic::input_value(deserializer)?)
|
|
||||||
.map_err(|e| serde::de::Error::custom(format!("{:?}", e)))
|
|
||||||
}
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -905,9 +871,9 @@ impl<N: NestedValue<D>, D: Embeddable> Annotations<N, D> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn copy_via<M: NestedValue<E>, E: Embeddable, F>(&self, f: &F) -> IOResult<Annotations<M, E>>
|
pub fn copy_via<M: NestedValue<E>, E: Embeddable, F, Err>(&self, f: &mut F) -> Result<Annotations<M, E>, Err>
|
||||||
where
|
where
|
||||||
F: Fn(&D) -> IOResult<Value<M, E>>
|
F: FnMut(&D) -> Result<Value<M, E>, Err>
|
||||||
{
|
{
|
||||||
Ok(match &self.0 {
|
Ok(match &self.0 {
|
||||||
None =>
|
None =>
|
||||||
|
@ -1000,18 +966,6 @@ impl<D: Embeddable> Debug for PlainValue<D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D: Embeddable> serde::Serialize for PlainValue<D> {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
|
||||||
serialize_nested_value(self, serializer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de, Dom: Embeddable> serde::Deserialize<'de> for PlainValue<Dom> {
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
|
||||||
deserialize_nested_value(deserializer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
@ -1050,18 +1004,6 @@ impl<D: Embeddable> Debug for RcValue<D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D: Embeddable> serde::Serialize for RcValue<D> {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
|
||||||
serialize_nested_value(self, serializer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de, Dom: Embeddable> serde::Deserialize<'de> for RcValue<Dom> {
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
|
||||||
deserialize_nested_value(deserializer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
|
@ -1098,40 +1040,20 @@ impl<D: Embeddable> Debug for ArcValue<D> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D: Embeddable> serde::Serialize for ArcValue<D> {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
|
||||||
serialize_nested_value(self, serializer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de, Dom: Embeddable> serde::Deserialize<'de> for ArcValue<Dom> {
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
|
||||||
deserialize_nested_value(deserializer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
pub struct IOValue(Arc<AnnotatedValue<IOValue, IOValue>>);
|
pub struct IOValue(Arc<AnnotatedValue<IOValue, IOValue>>);
|
||||||
pub type UnwrappedIOValue = Value<IOValue, IOValue>;
|
pub type UnwrappedIOValue = Value<IOValue, IOValue>;
|
||||||
|
|
||||||
impl Domain for IOValue {
|
|
||||||
fn from_preserves(v: IOValue) -> IOResult<Self> {
|
|
||||||
Ok(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_preserves(&self) -> IOValue {
|
|
||||||
self.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref FALSE: IOValue = IOValue(Arc::new(AnnotatedValue(Annotations::empty(), Value::Boolean(false))));
|
pub static ref FALSE: IOValue = IOValue(Arc::new(AnnotatedValue(Annotations::empty(), Value::Boolean(false))));
|
||||||
pub static ref TRUE: IOValue = IOValue(Arc::new(AnnotatedValue(Annotations::empty(), Value::Boolean(true))));
|
pub static ref TRUE: IOValue = IOValue(Arc::new(AnnotatedValue(Annotations::empty(), Value::Boolean(true))));
|
||||||
pub static ref EMPTY_SEQ: IOValue = IOValue(Arc::new(AnnotatedValue(Annotations::empty(), Value::Sequence(Vec::new()))));
|
pub static ref EMPTY_SEQ: IOValue = IOValue(Arc::new(AnnotatedValue(Annotations::empty(), Value::Sequence(Vec::new()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Domain for IOValue {}
|
||||||
|
|
||||||
impl NestedValue<IOValue> for IOValue {
|
impl NestedValue<IOValue> for IOValue {
|
||||||
fn wrap(anns: Annotations<Self, IOValue>, v: Value<Self, IOValue>) -> Self {
|
fn wrap(anns: Annotations<Self, IOValue>, v: Value<Self, IOValue>) -> Self {
|
||||||
IOValue(Arc::new(AnnotatedValue::new(anns, v)))
|
IOValue(Arc::new(AnnotatedValue::new(anns, v)))
|
||||||
|
@ -1158,14 +1080,6 @@ impl NestedValue<IOValue> for IOValue {
|
||||||
Err(r) => r.1.clone(),
|
Err(r) => r.1.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_io_value(&self) -> IOValue {
|
|
||||||
self.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_io_value(v: &IOValue) -> IOResult<Self> {
|
|
||||||
Ok(v.clone())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for IOValue {
|
impl Debug for IOValue {
|
||||||
|
@ -1176,12 +1090,12 @@ impl Debug for IOValue {
|
||||||
|
|
||||||
impl serde::Serialize for IOValue {
|
impl serde::Serialize for IOValue {
|
||||||
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 {
|
||||||
serialize_nested_value(self, serializer)
|
super::magic::output_value(serializer, self.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de> serde::Deserialize<'de> for IOValue {
|
impl<'de> serde::Deserialize<'de> for IOValue {
|
||||||
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> {
|
||||||
deserialize_nested_value(deserializer)
|
super::magic::input_value(deserializer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue