From a8e696f62f14296b94964adb1946d7e2b5ef5ebd Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 10 May 2019 19:49:38 +0200 Subject: Write a custom map type that allows duplicates --- dhall/src/phase/binary.rs | 67 +++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 29 deletions(-) (limited to 'dhall/src/phase') diff --git a/dhall/src/phase/binary.rs b/dhall/src/phase/binary.rs index 5110241..7f72e80 100644 --- a/dhall/src/phase/binary.rs +++ b/dhall/src/phase/binary.rs @@ -1,5 +1,6 @@ use itertools::Itertools; use serde_cbor::value::value as cbor; +use std::iter::FromIterator; use dhall_syntax::{ rc, ExprF, FilePrefix, Hash, Import, ImportHashed, ImportLocation, @@ -131,11 +132,11 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { Merge(x, y, Some(z)) } [U64(7), Object(map)] => { - let map = cbor_map_to_dhall_map(map.iter())?; + let map = cbor_map_to_dhall_map(map)?; RecordType(map) } [U64(8), Object(map)] => { - let map = cbor_map_to_dhall_map(map.iter())?; + let map = cbor_map_to_dhall_map(map)?; RecordLit(map) } [U64(9), x, String(l)] => { @@ -144,11 +145,11 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { Field(x, l) } [U64(11), Object(map)] => { - let map = cbor_map_to_dhall_opt_map(map.iter())?; + let map = cbor_map_to_dhall_opt_map(map)?; UnionType(map) } [U64(12), String(l), x, Object(map)] => { - let map = cbor_map_to_dhall_opt_map(map.iter())?; + let map = cbor_map_to_dhall_opt_map(map)?; let x = cbor_value_to_dhall(&x)?; let l = Label::from(l.as_str()); UnionLit(l, x, map) @@ -331,31 +332,39 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { })) } -fn cbor_map_to_dhall_map<'a>( - map: impl Iterator, -) -> Result, DecodeError> { - map.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)) - }) - .collect::>() +fn cbor_map_to_dhall_map<'a, T>( + map: impl IntoIterator, +) -> Result +where + T: FromIterator<(Label, ParsedExpr)>, +{ + 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)) + }) + .collect::>() } -fn cbor_map_to_dhall_opt_map<'a>( - map: impl Iterator, -) -> Result)>, DecodeError> { - map.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, - _ => Some(cbor_value_to_dhall(v)?), - }; - Ok((Label::from(k.as_ref()), v)) - }) - .collect::>() +fn cbor_map_to_dhall_opt_map<'a, T>( + 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, + _ => Some(cbor_value_to_dhall(v)?), + }; + Ok((Label::from(k.as_ref()), v)) + }) + .collect::>() } -- cgit v1.2.3