diff --git a/implementations/rust/preserves-schema/src/compiler/mod.rs b/implementations/rust/preserves-schema/src/compiler/mod.rs index c174e2f..f44680c 100644 --- a/implementations/rust/preserves-schema/src/compiler/mod.rs +++ b/implementations/rust/preserves-schema/src/compiler/mod.rs @@ -127,7 +127,7 @@ pub fn compile(config: &CompilerConfig) -> Result<(), std::io::Error> { lines.push("#![allow(unused_imports)]".to_owned()); lines.push("".to_owned()); lines.push("use std::convert::TryFrom;".to_owned()); - lines.push("use preserves::value::Domain;".to_owned()); + lines.push("use preserves::value::Embeddable;".to_owned()); lines.push("use preserves::value::NestedValue;".to_owned()); lines.push(format!("use {}::support as _support;", &config.support_crate)); lines.push("".to_owned()); diff --git a/implementations/rust/preserves-schema/src/compiler/unparsers.rs b/implementations/rust/preserves-schema/src/compiler/unparsers.rs index 0558b76..83c3db6 100644 --- a/implementations/rust/preserves-schema/src/compiler/unparsers.rs +++ b/implementations/rust/preserves-schema/src/compiler/unparsers.rs @@ -141,9 +141,9 @@ fn simple_pattern_unparser( SimplePattern::Embedded { .. } => { match ctxt.m.mode { ModuleContextMode::TargetIOValue => - item(seq!["preserves::value::Value::Domain(", src.as_ref().unwrap().to_owned(), ".as_preserves()).wrap()"]), + item(seq!["preserves::value::Value::Embedded(", src.as_ref().unwrap().to_owned(), ".as_preserves()).wrap()"]), ModuleContextMode::TargetAny => - item(seq!["preserves::value::Value::Domain(", src.as_ref().unwrap().to_owned(), ").wrap()"]), + item(seq!["preserves::value::Value::Embedded(", src.as_ref().unwrap().to_owned(), ").wrap()"]), } } SimplePattern::Lit { value } => diff --git a/implementations/rust/preserves-schema/src/gen/schema.rs b/implementations/rust/preserves-schema/src/gen/schema.rs index cd33538..db2267c 100644 --- a/implementations/rust/preserves-schema/src/gen/schema.rs +++ b/implementations/rust/preserves-schema/src/gen/schema.rs @@ -2,7 +2,7 @@ #![allow(unused_imports)] use std::convert::TryFrom; -use preserves::value::Domain; +use preserves::value::Embeddable; use preserves::value::NestedValue; use crate::support as _support; diff --git a/implementations/rust/preserves/src/lib.rs b/implementations/rust/preserves/src/lib.rs index 54c8ca1..6c1e3b3 100644 --- a/implementations/rust/preserves/src/lib.rs +++ b/implementations/rust/preserves/src/lib.rs @@ -11,7 +11,7 @@ pub mod value; #[cfg(test)] mod dom { use super::value::{ - Value, IOValue, IOResult, NestedValue, PlainValue, PreDomain, Domain, + Value, IOValue, IOResult, NestedValue, PlainValue, Domain, PackedWriter, }; @@ -21,8 +21,7 @@ mod dom { Two, } - impl Domain for Dom {} - impl PreDomain for Dom { + impl Domain for Dom { fn from_preserves(v: IOValue) -> IOResult { panic!("Cannot decode IOValue to Dom: {:?}", v); } @@ -37,7 +36,7 @@ mod dom { #[test] fn test_one() { let v: PlainValue<_> = Value::from(vec![Value::from(1).wrap(), - Value::Domain(Dom::One).wrap(), + Value::Embedded(Dom::One).wrap(), Value::from(2).wrap()]) .wrap(); assert_eq!(PackedWriter::encode(&v.to_io_value()).unwrap(), @@ -46,7 +45,7 @@ mod dom { #[test] fn test_two() { let v: PlainValue<_> = Value::from(vec![Value::from(1).wrap(), - Value::Domain(Dom::Two).wrap(), + Value::Embedded(Dom::Two).wrap(), Value::from(2).wrap()]) .wrap(); assert_eq!(PackedWriter::encode(&v.to_io_value()).unwrap(), diff --git a/implementations/rust/preserves/src/value/mod.rs b/implementations/rust/preserves/src/value/mod.rs index 6c20177..f37f54c 100644 --- a/implementations/rust/preserves/src/value/mod.rs +++ b/implementations/rust/preserves/src/value/mod.rs @@ -17,12 +17,12 @@ pub use repr::AnnotatedValue; pub use repr::ArcValue; pub use repr::Domain; pub use repr::Double; +pub use repr::Embeddable; pub use repr::Float; pub use repr::IOValue; pub use repr::Map; pub use repr::NestedValue; pub use repr::PlainValue; -pub use repr::PreDomain; pub use repr::RcValue; pub use repr::Record; pub use repr::Set; diff --git a/implementations/rust/preserves/src/value/packed/reader.rs b/implementations/rust/preserves/src/value/packed/reader.rs index ef0c75f..af7f417 100644 --- a/implementations/rust/preserves/src/value/packed/reader.rs +++ b/implementations/rust/preserves/src/value/packed/reader.rs @@ -275,7 +275,7 @@ impl<'de, S: BinarySource<'de>> Reader<'de> for PackedReader<'de, S> { } Tag::Embedded => { let v = self.demand_next(read_annotations)?; - Value::Domain(v).wrap() + Value::Embedded(v).wrap() } Tag::SmallInteger(v) => { // TODO: prebuild these in value.rs diff --git a/implementations/rust/preserves/src/value/repr.rs b/implementations/rust/preserves/src/value/repr.rs index a470090..0382ef0 100644 --- a/implementations/rust/preserves/src/value/repr.rs +++ b/implementations/rust/preserves/src/value/repr.rs @@ -21,15 +21,15 @@ use super::IOResult; use super::signed_integer::SignedInteger; use crate::error::{Error, ExpectedKind, Received}; -pub trait PreDomain: Sized + Debug + Eq + Hash + Ord { +pub trait Domain: Sized + Debug + Eq + Hash + Ord { fn from_preserves(v: IOValue) -> IOResult; fn as_preserves(&self) -> IOValue; } -pub trait Domain: PreDomain + Clone {} +pub trait Embeddable: Domain + Clone {} +impl Embeddable for T where T: Domain + Clone {} -impl Domain for Arc {} -impl PreDomain for Arc { +impl Domain for Arc { fn from_preserves(v: IOValue) -> IOResult { Ok(Arc::new(D::from_preserves(v)?)) } @@ -39,7 +39,7 @@ impl PreDomain for Arc { } } -pub trait NestedValue: Sized + Debug + Clone + Eq + Hash + Ord { +pub trait NestedValue: Sized + Debug + Clone + Eq + Hash + Ord { fn wrap(anns: Annotations, v: Value) -> Self; fn annotations(&self) -> &Annotations; @@ -54,7 +54,7 @@ pub trait NestedValue: Sized + Debug + Clone + Eq + Hash + Ord { self.value().fmt(f) } - fn copy_via, E: Domain, F>(&self, f: &F) -> IOResult + fn copy_via, E: Embeddable, F>(&self, f: &F) -> IOResult where F: Fn(&D) -> IOResult> { @@ -66,13 +66,13 @@ pub trait NestedValue: Sized + Debug + Clone + Eq + Hash + Ord { } fn from_io_value(v: &IOValue) -> IOResult { - v.copy_via(&|d| Ok(Value::Domain(D::from_preserves(d.clone())?))) + v.copy_via(&|d| Ok(Value::Embedded(D::from_preserves(d.clone())?))) } } /// The `Value`s from the specification. #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub enum Value where N: NestedValue, D: Domain { +pub enum Value where N: NestedValue, D: Embeddable { Boolean(bool), Float(Float), Double(Double), @@ -84,7 +84,7 @@ pub enum Value where N: NestedValue, D: Domain { Sequence(Vec), Set(Set), Dictionary(Map), - Domain(D), + Embedded(D), } /// Single-precision IEEE 754 Value @@ -128,7 +128,7 @@ impl Record { &mut self.0 } - pub fn finish(self) -> Value where N: NestedValue { + pub fn finish(self) -> Value where N: NestedValue { Value::Record(self) } } @@ -217,38 +217,38 @@ impl PartialOrd for Double { impl Eq for Double {} -impl, D: Domain> From for Value { fn from(v: bool) -> Self { Value::Boolean(v) } } +impl, D: Embeddable> From for Value { fn from(v: bool) -> Self { Value::Boolean(v) } } -impl, D: Domain> From for Value { fn from(v: f32) -> Self { Value::Float(Float::from(v)) } } -impl, D: Domain> From<&Float> for Value { fn from(v: &Float) -> Self { Value::Float(v.clone()) } } -impl, D: Domain> From for Value { fn from(v: f64) -> Self { Value::Double(Double::from(v)) } } -impl, D: Domain> From<&Double> for Value { fn from(v: &Double) -> Self { Value::Double(v.clone()) } } +impl, D: Embeddable> From for Value { fn from(v: f32) -> Self { Value::Float(Float::from(v)) } } +impl, D: Embeddable> From<&Float> for Value { fn from(v: &Float) -> Self { Value::Float(v.clone()) } } +impl, D: Embeddable> From for Value { fn from(v: f64) -> Self { Value::Double(Double::from(v)) } } +impl, D: Embeddable> From<&Double> for Value { fn from(v: &Double) -> Self { Value::Double(v.clone()) } } -impl, D: Domain> From for Value { fn from(v: u8) -> Self { Value::from(i128::from(v)) } } -impl, D: Domain> From for Value { fn from(v: i8) -> Self { Value::from(i128::from(v)) } } -impl, D: Domain> From for Value { fn from(v: u16) -> Self { Value::from(i128::from(v)) } } -impl, D: Domain> From for Value { fn from(v: i16) -> Self { Value::from(i128::from(v)) } } -impl, D: Domain> From for Value { fn from(v: u32) -> Self { Value::from(i128::from(v)) } } -impl, D: Domain> From for Value { fn from(v: i32) -> Self { Value::from(i128::from(v)) } } -impl, D: Domain> From for Value { fn from(v: u64) -> Self { Value::from(i128::from(v)) } } -impl, D: Domain> From for Value { fn from(v: i64) -> Self { Value::from(i128::from(v)) } } -impl, D: Domain> From for Value { fn from(v: u128) -> Self { Value::SignedInteger(SignedInteger::from(v)) } } -impl, D: Domain> From for Value { fn from(v: i128) -> Self { Value::SignedInteger(SignedInteger::from(v)) } } -impl, D: Domain> From<&BigInt> for Value { fn from(v: &BigInt) -> Self { Value::SignedInteger(SignedInteger::from(Cow::Borrowed(v))) } } -impl, D: Domain> From<&SignedInteger> for Value { fn from(v: &SignedInteger) -> Self { Value::SignedInteger(v.clone()) } } +impl, D: Embeddable> From for Value { fn from(v: u8) -> Self { Value::from(i128::from(v)) } } +impl, D: Embeddable> From for Value { fn from(v: i8) -> Self { Value::from(i128::from(v)) } } +impl, D: Embeddable> From for Value { fn from(v: u16) -> Self { Value::from(i128::from(v)) } } +impl, D: Embeddable> From for Value { fn from(v: i16) -> Self { Value::from(i128::from(v)) } } +impl, D: Embeddable> From for Value { fn from(v: u32) -> Self { Value::from(i128::from(v)) } } +impl, D: Embeddable> From for Value { fn from(v: i32) -> Self { Value::from(i128::from(v)) } } +impl, D: Embeddable> From for Value { fn from(v: u64) -> Self { Value::from(i128::from(v)) } } +impl, D: Embeddable> From for Value { fn from(v: i64) -> Self { Value::from(i128::from(v)) } } +impl, D: Embeddable> From for Value { fn from(v: u128) -> Self { Value::SignedInteger(SignedInteger::from(v)) } } +impl, D: Embeddable> From for Value { fn from(v: i128) -> Self { Value::SignedInteger(SignedInteger::from(v)) } } +impl, D: Embeddable> From<&BigInt> for Value { fn from(v: &BigInt) -> Self { Value::SignedInteger(SignedInteger::from(Cow::Borrowed(v))) } } +impl, D: Embeddable> From<&SignedInteger> for Value { fn from(v: &SignedInteger) -> Self { Value::SignedInteger(v.clone()) } } -impl, D: Domain> From<&str> for Value { fn from(v: &str) -> Self { Value::String(String::from(v)) } } -impl, D: Domain> From for Value { fn from(v: String) -> Self { Value::String(v) } } -impl, D: Domain> From<&String> for Value { fn from(v: &String) -> Self { Value::String(v.to_owned()) } } +impl, D: Embeddable> From<&str> for Value { fn from(v: &str) -> Self { Value::String(String::from(v)) } } +impl, D: Embeddable> From for Value { fn from(v: String) -> Self { Value::String(v) } } +impl, D: Embeddable> From<&String> for Value { fn from(v: &String) -> Self { Value::String(v.to_owned()) } } -impl, D: Domain> From<&[u8]> for Value { fn from(v: &[u8]) -> Self { Value::ByteString(Vec::from(v)) } } -// impl, D: Domain> From> for Value { fn from(v: Vec) -> Self { Value::ByteString(v) } } +impl, D: Embeddable> From<&[u8]> for Value { fn from(v: &[u8]) -> Self { Value::ByteString(Vec::from(v)) } } +// impl, D: Embeddable> From> for Value { fn from(v: Vec) -> Self { Value::ByteString(v) } } -impl, D: Domain> From> for Value { fn from(v: Vec) -> Self { Value::Sequence(v) } } -impl, D: Domain> From> for Value { fn from(v: Set) -> Self { Value::Set(v) } } -impl, D: Domain> From> for Value { fn from(v: Map) -> Self { Value::Dictionary(v) } } +impl, D: Embeddable> From> for Value { fn from(v: Vec) -> Self { Value::Sequence(v) } } +impl, D: Embeddable> From> for Value { fn from(v: Set) -> Self { Value::Set(v) } } +impl, D: Embeddable> From> for Value { fn from(v: Map) -> Self { Value::Dictionary(v) } } -impl, D: Domain> Debug for Value { +impl, D: Embeddable> Debug for Value { // 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 { @@ -287,14 +287,14 @@ impl, D: Domain> Debug for Value { f.debug_set().entries(v.iter()).finish() } Value::Dictionary(v) => f.debug_map().entries(v.iter()).finish(), - Value::Domain(d) => write!(f, "#!{:?}", d), + Value::Embedded(d) => write!(f, "#!{:?}", d), } } } //--------------------------------------------------------------------------- -impl, D: Domain> Value { +impl, D: Embeddable> Value { pub fn wrap(self) -> N { N::wrap(Annotations::empty(), self) } @@ -777,7 +777,7 @@ impl, D: Domain> Value { } pub fn as_embedded(&self) -> Option<&D> { - if let Value::Domain(d) = self { + if let Value::Embedded(d) = self { Some(d) } else { None @@ -788,7 +788,7 @@ impl, D: Domain> Value { self.as_embedded().ok_or_else(|| self.expected(ExpectedKind::Embedded)) } - pub fn copy_via, E: Domain, F>(&self, f: &F) -> IOResult> + pub fn copy_via, E: Embeddable, F>(&self, f: &F) -> IOResult> where F: Fn(&D) -> IOResult> { @@ -810,12 +810,12 @@ impl, D: Domain> Value { Value::Dictionary(v) => Value::Dictionary(v.iter().map(|(a,b)| Ok((a.copy_via(f)?, b.copy_via(f)?))) .collect::>>()?), - Value::Domain(d) => f(d)?, + Value::Embedded(d) => f(d)?, }) } } -impl, D: Domain> Index for Value { +impl, D: Embeddable> Index for Value { type Output = N; fn index(&self, i: usize) -> &Self::Output { @@ -823,13 +823,13 @@ impl, D: Domain> Index for Value { } } -impl, D: Domain> IndexMut for Value { +impl, D: Embeddable> IndexMut for Value { fn index_mut(&mut self, i: usize) -> &mut Self::Output { &mut self.as_sequence_mut().unwrap()[i] } } -impl, D: Domain> Index<&N> for Value { +impl, D: Embeddable> Index<&N> for Value { type Output = N; fn index(&self, i: &N) -> &Self::Output { @@ -852,13 +852,13 @@ impl<'de> serde::Deserialize<'de> for UnwrappedIOValue { } } -pub fn serialize_nested_value(v: &N, serializer: S) -> +pub fn serialize_nested_value(v: &N, serializer: S) -> Result where S: serde::Serializer, N: NestedValue + serde::Serialize { super::magic::output_value(serializer, v.to_io_value()) } -pub fn deserialize_nested_value<'de, D, N, Dom: Domain>(deserializer: D) -> +pub fn deserialize_nested_value<'de, D, N, Dom: Embeddable>(deserializer: D) -> Result where D: serde::Deserializer<'de>, N: NestedValue + serde::Deserialize<'de> { N::from_io_value(&super::magic::input_value(deserializer)?) @@ -868,9 +868,9 @@ pub fn deserialize_nested_value<'de, D, N, Dom: Domain>(deserializer: D) -> //--------------------------------------------------------------------------- #[derive(Clone)] -pub struct Annotations, D: Domain>(Option>, PhantomData); +pub struct Annotations, D: Embeddable>(Option>, PhantomData); -impl, D: Domain> Annotations { +impl, D: Embeddable> Annotations { pub fn empty() -> Self { Annotations(None, PhantomData) @@ -901,7 +901,7 @@ impl, D: Domain> Annotations { self } - pub fn copy_via, E: Domain, F>(&self, f: &F) -> IOResult> + pub fn copy_via, E: Embeddable, F>(&self, f: &F) -> IOResult> where F: Fn(&D) -> IOResult> { @@ -918,35 +918,35 @@ impl, D: Domain> Annotations { /// A possibly-annotated Value, with annotations (themselves /// possibly-annotated) in order of appearance. #[derive(Clone)] -pub struct AnnotatedValue, D: Domain>(pub Annotations, pub Value); +pub struct AnnotatedValue, D: Embeddable>(pub Annotations, pub Value); -impl, D: Domain> AnnotatedValue { +impl, D: Embeddable> AnnotatedValue { fn new(anns: Annotations, value: Value) -> Self { AnnotatedValue(anns, value) } } -impl, D: Domain> PartialEq for AnnotatedValue { +impl, D: Embeddable> PartialEq for AnnotatedValue { fn eq(&self, other: &Self) -> bool { self.1.eq(&other.1) } } -impl, D: Domain> Eq for AnnotatedValue {} +impl, D: Embeddable> Eq for AnnotatedValue {} -impl, D: Domain> Hash for AnnotatedValue { +impl, D: Embeddable> Hash for AnnotatedValue { fn hash(&self, state: &mut H) { self.1.hash(state); } } -impl, D: Domain> PartialOrd for AnnotatedValue { +impl, D: Embeddable> PartialOrd for AnnotatedValue { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl, D: Domain> Ord for AnnotatedValue { +impl, D: Embeddable> Ord for AnnotatedValue { fn cmp(&self, other: &Self) -> Ordering { self.1.cmp(&other.1) } @@ -955,9 +955,9 @@ impl, D: Domain> Ord for AnnotatedValue { //--------------------------------------------------------------------------- #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub struct PlainValue(AnnotatedValue, D>); +pub struct PlainValue(AnnotatedValue, D>); -impl PlainValue { +impl PlainValue { pub fn annotations_mut(&mut self) -> &mut Annotations { &mut (self.0).0 } @@ -967,7 +967,7 @@ impl PlainValue { } } -impl NestedValue for PlainValue { +impl NestedValue for PlainValue { fn wrap(anns: Annotations, v: Value) -> Self { PlainValue(AnnotatedValue::new(anns, v)) } @@ -990,19 +990,19 @@ impl NestedValue for PlainValue { } } -impl Debug for PlainValue { +impl Debug for PlainValue { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.debug_fmt(f) } } -impl serde::Serialize for PlainValue { +impl serde::Serialize for PlainValue { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer { serialize_nested_value(self, serializer) } } -impl<'de, Dom: Domain> serde::Deserialize<'de> for PlainValue { +impl<'de, Dom: Embeddable> serde::Deserialize<'de> for PlainValue { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de> { deserialize_nested_value(deserializer) } @@ -1013,9 +1013,9 @@ impl<'de, Dom: Domain> serde::Deserialize<'de> for PlainValue { use std::rc::Rc; #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub struct RcValue(Rc, D>>); +pub struct RcValue(Rc, D>>); -impl NestedValue for RcValue { +impl NestedValue for RcValue { fn wrap(anns: Annotations, v: Value) -> Self { RcValue(Rc::new(AnnotatedValue::new(anns, v))) } @@ -1040,19 +1040,19 @@ impl NestedValue for RcValue { } } -impl Debug for RcValue { +impl Debug for RcValue { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.debug_fmt(f) } } -impl serde::Serialize for RcValue { +impl serde::Serialize for RcValue { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer { serialize_nested_value(self, serializer) } } -impl<'de, Dom: Domain> serde::Deserialize<'de> for RcValue { +impl<'de, Dom: Embeddable> serde::Deserialize<'de> for RcValue { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de> { deserialize_nested_value(deserializer) } @@ -1061,9 +1061,9 @@ impl<'de, Dom: Domain> serde::Deserialize<'de> for RcValue { //--------------------------------------------------------------------------- #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub struct ArcValue(Arc, D>>); +pub struct ArcValue(Arc, D>>); -impl NestedValue for ArcValue { +impl NestedValue for ArcValue { fn wrap(anns: Annotations, v: Value) -> Self { ArcValue(Arc::new(AnnotatedValue::new(anns, v))) } @@ -1088,19 +1088,19 @@ impl NestedValue for ArcValue { } } -impl Debug for ArcValue { +impl Debug for ArcValue { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.debug_fmt(f) } } -impl serde::Serialize for ArcValue { +impl serde::Serialize for ArcValue { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer { serialize_nested_value(self, serializer) } } -impl<'de, Dom: Domain> serde::Deserialize<'de> for ArcValue { +impl<'de, Dom: Embeddable> serde::Deserialize<'de> for ArcValue { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de> { deserialize_nested_value(deserializer) } @@ -1112,8 +1112,7 @@ impl<'de, Dom: Domain> serde::Deserialize<'de> for ArcValue { pub struct IOValue(Arc>); pub type UnwrappedIOValue = Value; -impl Domain for IOValue {} -impl PreDomain for IOValue { +impl Domain for IOValue { fn from_preserves(v: IOValue) -> IOResult { Ok(v) } diff --git a/implementations/rust/preserves/src/value/writer.rs b/implementations/rust/preserves/src/value/writer.rs index b6272a8..41421f5 100644 --- a/implementations/rust/preserves/src/value/writer.rs +++ b/implementations/rust/preserves/src/value/writer.rs @@ -132,7 +132,7 @@ pub trait Writer: Sized { } self.end_set(c) } - Value::Domain(d) => { + Value::Embedded(d) => { let mut c = self.start_embedded()?; c.write(&d)?; self.end_embedded(c)