diff options
Diffstat (limited to 'dhall/src/syntax/binary')
-rw-r--r-- | dhall/src/syntax/binary/encode.rs | 80 |
1 files changed, 40 insertions, 40 deletions
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<Vec<u8>, EncodeError> { } enum Serialize<'a> { + Null, + Tag(u64), + Label(&'a Label), + Text(String), + Bytes(Vec<u8>), + Expr(&'a Expr), - CBOR(cbor::Value), RecordMap(&'a BTreeMap<Label, Expr>), UnionMap(&'a BTreeMap<Label, Option<Expr>>), } @@ -44,7 +48,6 @@ fn serialize_subexpr<S>(ser: S, e: &Expr) -> Result<S::Ok, S::Error> 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<S>(ser: S, import: &Import<Expr>) -> Result<S::Ok, S::Error> 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))), + ), } } } |