From e9c92d6ad3bd9ba9c98fbb92a19e8afcbfc1c8f5 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 30 Oct 2020 19:54:39 +0000 Subject: Don't use serde_cbor::Value in binary encoding --- dhall/src/syntax/binary/encode.rs | 80 +++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 40 deletions(-) (limited to 'dhall/src/syntax/binary') diff --git a/dhall/src/syntax/binary/encode.rs b/dhall/src/syntax/binary/encode.rs index 33c7fa5..cf2e112 100644 --- a/dhall/src/syntax/binary/encode.rs +++ b/dhall/src/syntax/binary/encode.rs @@ -1,4 +1,3 @@ -use serde_cbor::value::value as cbor; use std::collections::BTreeMap; use std::vec; @@ -17,8 +16,13 @@ pub fn encode(expr: &Expr) -> Result, EncodeError> { } enum Serialize<'a> { + Null, + Tag(u64), + Label(&'a Label), + Text(String), + Bytes(Vec), + Expr(&'a Expr), - CBOR(cbor::Value), RecordMap(&'a BTreeMap), UnionMap(&'a BTreeMap>), } @@ -44,7 +48,6 @@ fn serialize_subexpr(ser: S, e: &Expr) -> Result where S: serde::ser::Serializer, { - use cbor::Value::{String, I64, U64}; use std::iter::once; use syntax::ExprKind::*; use syntax::NumKind::*; @@ -54,18 +57,18 @@ where fn expr(x: &Expr) -> self::Serialize<'_> { self::Serialize::Expr(x) } - let cbor = - |v: cbor::Value| -> self::Serialize<'_> { self::Serialize::CBOR(v) }; - let tag = |x: u64| cbor(U64(x)); - let null = || cbor(cbor::Value::Null); - let label = |l: &Label| cbor(cbor::Value::String(l.into())); + fn label(x: &Label) -> self::Serialize<'_> { + self::Serialize::Label(x) + } + let tag = |x: u64| Serialize::Tag(x); + let null = || Serialize::Null; match e.as_ref() { Const(c) => ser.serialize_str(&c.to_string()), Builtin(b) => ser.serialize_str(&b.to_string()), Num(Bool(b)) => ser.serialize_bool(*b), - Num(Natural(n)) => ser_seq!(ser; tag(15), U64(*n as u64)), - Num(Integer(n)) => ser_seq!(ser; tag(16), I64(*n as i64)), + Num(Natural(n)) => ser_seq!(ser; tag(15), n), + Num(Integer(n)) => ser_seq!(ser; tag(16), n), Num(Double(n)) => { let n: f64 = (*n).into(); ser.serialize_f64(n) @@ -73,13 +76,13 @@ where Op(BoolIf(x, y, z)) => { ser_seq!(ser; tag(14), expr(x), expr(y), expr(z)) } - Var(V(l, n)) if l == &"_".into() => ser.serialize_u64(*n as u64), - Var(V(l, n)) => ser_seq!(ser; label(l), U64(*n as u64)), - Lam(l, x, y) if l == &"_".into() => { + Var(V(l, n)) if l.as_ref() == "_" => ser.serialize_u64(*n as u64), + Var(V(l, n)) => ser_seq!(ser; label(l), (*n as u64)), + Lam(l, x, y) if l.as_ref() == "_" => { ser_seq!(ser; tag(1), expr(x), expr(y)) } Lam(l, x, y) => ser_seq!(ser; tag(1), label(l), expr(x), expr(y)), - Pi(l, x, y) if l == &"_".into() => { + Pi(l, x, y) if l.as_ref() == "_" => { ser_seq!(ser; tag(2), expr(x), expr(y)) } Pi(l, x, y) => ser_seq!(ser; tag(2), label(l), expr(x), expr(y)), @@ -128,7 +131,7 @@ where use syntax::InterpolatedTextContents::{Expr, Text}; ser.collect_seq(once(tag(18)).chain(xs.iter().map(|x| match x { Expr(x) => expr(x), - Text(x) => cbor(String(x)), + Text(x) => Serialize::Text(x), }))) } RecordType(map) => ser_seq!(ser; tag(7), RecordMap(map)), @@ -152,7 +155,7 @@ where ImportAlt => 11, Equivalence => 12, }; - ser_seq!(ser; tag(3), U64(op), expr(x), expr(y)) + ser_seq!(ser; tag(3), op, expr(x), expr(y)) } Op(Merge(x, y, None)) => ser_seq!(ser; tag(6), expr(x), expr(y)), Op(Merge(x, y, Some(z))) => { @@ -183,8 +186,8 @@ fn serialize_import(ser: S, import: &Import) -> Result where S: serde::ser::Serializer, { - use cbor::Value::{Bytes, Null, U64}; use serde::ser::SerializeSeq; + use Serialize::Null; let count = 4 + match &import.location { ImportTarget::Remote(url) => 3 + url.path.file_path.len(), @@ -194,24 +197,23 @@ where }; let mut ser_seq = ser.serialize_seq(Some(count))?; - ser_seq.serialize_element(&U64(24))?; + ser_seq.serialize_element(&24)?; - let hash = match &import.hash { - None => Null, + match &import.hash { + None => ser_seq.serialize_element(&Null)?, Some(Hash::SHA256(h)) => { let mut bytes = vec![18, 32]; bytes.extend_from_slice(h); - Bytes(bytes) + ser_seq.serialize_element(&Serialize::Bytes(bytes))?; } - }; - ser_seq.serialize_element(&hash)?; + } let mode = match import.mode { ImportMode::Code => 0, ImportMode::RawText => 1, ImportMode::Location => 2, }; - ser_seq.serialize_element(&U64(mode))?; + ser_seq.serialize_element(&mode)?; let scheme = match &import.location { ImportTarget::Remote(url) => match url.scheme { @@ -227,7 +229,7 @@ where ImportTarget::Env(_) => 6, ImportTarget::Missing => 7, }; - ser_seq.serialize_element(&U64(scheme))?; + ser_seq.serialize_element(&scheme)?; match &import.location { ImportTarget::Remote(url) => { @@ -265,23 +267,21 @@ impl<'a> serde::ser::Serialize for Serialize<'a> { where S: serde::ser::Serializer, { + use Serialize::*; match self { - Serialize::Expr(e) => serialize_subexpr(ser, e), - Serialize::CBOR(v) => v.serialize(ser), - Serialize::RecordMap(map) => { - ser.collect_map(map.iter().map(|(k, v)| { - (cbor::Value::String(k.into()), Serialize::Expr(v)) - })) - } - Serialize::UnionMap(map) => { - ser.collect_map(map.iter().map(|(k, v)| { - let v = match v { - Some(x) => Serialize::Expr(x), - None => Serialize::CBOR(cbor::Value::Null), - }; - (cbor::Value::String(k.into()), v) - })) + Null => ser.serialize_unit(), + Tag(v) => ser.serialize_u64(*v), + Label(v) => ser.serialize_str(v.as_ref()), + Text(v) => ser.serialize_str(v), + Bytes(v) => ser.serialize_bytes(v), + + Expr(e) => serialize_subexpr(ser, e), + RecordMap(map) => { + ser.collect_map(map.iter().map(|(k, v)| (Label(k), Expr(v)))) } + UnionMap(map) => ser.collect_map( + map.iter().map(|(k, v)| (Label(k), v.as_ref().map(Expr))), + ), } } } -- cgit v1.2.3 From c3e7b9004d136fd968468abc7559f003a9f7d235 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 30 Oct 2020 20:10:33 +0000 Subject: Roll our own Value for binary decoding --- dhall/src/syntax/binary/decode.rs | 174 +++++++++++++++++++++++++++++++++++--- 1 file changed, 162 insertions(+), 12 deletions(-) (limited to 'dhall/src/syntax/binary') diff --git a/dhall/src/syntax/binary/decode.rs b/dhall/src/syntax/binary/decode.rs index d360949..f4747d3 100644 --- a/dhall/src/syntax/binary/decode.rs +++ b/dhall/src/syntax/binary/decode.rs @@ -1,5 +1,7 @@ use itertools::Itertools; -use serde_cbor::value::value as cbor; +use serde::de; +use std::collections::BTreeMap; +use std::fmt; use std::iter::FromIterator; use crate::error::DecodeError; @@ -19,18 +21,32 @@ pub fn decode(data: &[u8]) -> Result { } } +/// An enum that can encode most CBOR values. +#[derive(Clone, Debug, PartialEq)] +pub enum Value { + Null, + Bool(bool), + U64(u64), + I64(i64), + F64(f64), + String(String), + Array(Vec), + Object(BTreeMap), + Bytes(Vec), +} + // Should probably rename this fn rc(x: UnspannedExpr) -> Expr { Expr::new(x, Span::Decoded) } -fn cbor_value_to_dhall(data: &cbor::Value) -> Result { +fn cbor_value_to_dhall(data: &Value) -> Result { use crate::builtins::Builtin; use crate::operations::BinOp; - use cbor::Value::*; use syntax::Const; use ExprKind::*; use OpKind::*; + use Value::*; Ok(rc(match data { String(s) => match Builtin::parse(s) { Some(b) => ExprKind::Builtin(b), @@ -467,16 +483,13 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { } fn cbor_map_to_dhall_map<'a, T>( - map: impl IntoIterator, + map: impl IntoIterator, ) -> Result where T: FromIterator<(Label, DecodedExpr)>, { map.into_iter() .map(|(k, v)| -> Result<(_, _), _> { - let k = k.as_string().ok_or_else(|| { - DecodeError::WrongFormatError("map/key".to_owned()) - })?; let v = cbor_value_to_dhall(v)?; Ok((Label::from(k.as_ref()), v)) }) @@ -484,21 +497,158 @@ where } fn cbor_map_to_dhall_opt_map<'a, T>( - map: impl IntoIterator, + map: impl IntoIterator, ) -> Result where T: FromIterator<(Label, Option)>, { map.into_iter() .map(|(k, v)| -> Result<(_, _), _> { - let k = k.as_string().ok_or_else(|| { - DecodeError::WrongFormatError("map/key".to_owned()) - })?; let v = match v { - cbor::Value::Null => None, + Value::Null => None, _ => Some(cbor_value_to_dhall(v)?), }; Ok((Label::from(k.as_ref()), v)) }) .collect::>() } + +impl Value { + pub fn as_string(&self) -> Option<&String> { + if let Value::String(v) = self { + Some(v) + } else { + None + } + } +} + +impl<'de> de::Deserialize<'de> for Value { + #[inline] + fn deserialize(deserializer: D) -> Result + where + D: de::Deserializer<'de>, + { + struct ValueVisitor; + + impl<'de> de::Visitor<'de> for ValueVisitor { + type Value = Value; + + fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.write_str("any valid CBOR value") + } + + #[inline] + fn visit_str(self, value: &str) -> Result + where + E: de::Error, + { + self.visit_string(String::from(value)) + } + + #[inline] + fn visit_string(self, value: String) -> Result + where + E: de::Error, + { + Ok(Value::String(value)) + } + #[inline] + fn visit_bytes(self, v: &[u8]) -> Result + where + E: de::Error, + { + self.visit_byte_buf(v.to_owned()) + } + + #[inline] + fn visit_byte_buf(self, v: Vec) -> Result + where + E: de::Error, + { + Ok(Value::Bytes(v)) + } + + #[inline] + fn visit_u64(self, v: u64) -> Result + where + E: de::Error, + { + Ok(Value::U64(v)) + } + + #[inline] + fn visit_i64(self, v: i64) -> Result + where + E: de::Error, + { + Ok(Value::I64(v)) + } + + #[inline] + fn visit_bool(self, v: bool) -> Result + where + E: de::Error, + { + Ok(Value::Bool(v)) + } + + #[inline] + fn visit_none(self) -> Result + where + E: de::Error, + { + self.visit_unit() + } + + #[inline] + fn visit_unit(self) -> Result + where + E: de::Error, + { + Ok(Value::Null) + } + + #[inline] + fn visit_seq( + self, + mut visitor: V, + ) -> Result + where + V: de::SeqAccess<'de>, + { + let mut vec = Vec::new(); + + while let Some(elem) = visitor.next_element()? { + vec.push(elem); + } + + Ok(Value::Array(vec)) + } + + #[inline] + fn visit_map(self, mut visitor: V) -> Result + where + V: de::MapAccess<'de>, + { + let mut values = BTreeMap::new(); + + while let Some((key, value)) = visitor.next_entry()? { + values.insert(key, value); + } + + Ok(Value::Object(values)) + } + + #[inline] + fn visit_f64(self, v: f64) -> Result + where + E: de::Error, + { + Ok(Value::F64(v)) + } + } + + deserializer.deserialize_any(ValueVisitor) + } +} -- cgit v1.2.3