From efe4b340bebaa7ef8bce6e69194959b126c5fade Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 10 Apr 2020 12:10:53 +0100 Subject: Deserialize `Prelude.Map` and `toMap` to a map instead of a list --- serde_dhall/src/static_type.rs | 1 + serde_dhall/src/value.rs | 58 ++++++++++++++++++++++++++++++++++++++---- serde_dhall/tests/de.rs | 4 +++ 3 files changed, 58 insertions(+), 5 deletions(-) (limited to 'serde_dhall') diff --git a/serde_dhall/src/static_type.rs b/serde_dhall/src/static_type.rs index 26c70cd..3c5da18 100644 --- a/serde_dhall/src/static_type.rs +++ b/serde_dhall/src/static_type.rs @@ -47,6 +47,7 @@ use crate::SimpleType; /// `{ _1: T, _2: U }` | `(T, U)`, structs /// `{ x: T, y: T }` | `HashMap`, structs /// `< x: T \| y: U >` | enums +/// `Prelude.Map.Type Text T` | `HashMap`, structs /// `T -> U` | unsupported /// `Prelude.JSON.Type` | unsupported /// `Prelude.Map.Type T U` | unsupported diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index 03cfdba..0f0b256 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -144,13 +144,61 @@ impl SimpleValue { NirKind::NEOptionalLit(x) => { SimpleValue::Optional(Some(Box::new(Self::from_nir(x)?))) } - NirKind::EmptyListLit(_) => SimpleValue::List(vec![]), - NirKind::NEListLit(xs) => SimpleValue::List( - xs.iter().map(Self::from_nir).collect::>()?, - ), + NirKind::EmptyListLit(t) => { + // Detect and handle the special records that make assoc maps + if let NirKind::RecordType(kts) = t.kind() { + if kts.len() == 2 + && kts.contains_key("mapKey") + && kts.contains_key("mapValue") + { + return Some(SimpleValue::Record(Default::default())); + } + } + SimpleValue::List(vec![]) + } + NirKind::NEListLit(xs) => { + // Detect and handle the special records that make assoc maps + if let NirKind::RecordLit(kvs) = xs[0].kind() { + if kvs.len() == 2 + && kvs.contains_key("mapKey") + && kvs.contains_key("mapValue") + { + let convert_entry = |x: &Nir| match x.kind() { + NirKind::RecordLit(kvs) => { + let k = match kvs.get("mapKey").unwrap().kind() + { + NirKind::TextLit(t) + if t.as_text().is_some() => + { + t.as_text().unwrap() + } + // TODO + _ => panic!( + "Expected `mapKey` to be a text \ + literal" + ), + }; + let v = Self::from_nir( + kvs.get("mapValue").unwrap(), + )?; + Some((k, v)) + } + _ => unreachable!("Internal type error"), + }; + return Some(SimpleValue::Record( + xs.iter() + .map(convert_entry) + .collect::>()?, + )); + } + } + SimpleValue::List( + xs.iter().map(Self::from_nir).collect::>()?, + ) + } NirKind::RecordLit(kvs) => SimpleValue::Record( kvs.iter() - .map(|(k, v)| Some((k.into(), Self::from_nir(v)?))) + .map(|(k, v)| Some((k.to_string(), Self::from_nir(v)?))) .collect::>()?, ), NirKind::UnionLit(field, x, _) => SimpleValue::Union( diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs index 41b4080..1932e26 100644 --- a/serde_dhall/tests/de.rs +++ b/serde_dhall/tests/de.rs @@ -80,6 +80,10 @@ fn test_de_untyped() { parse::>("{ x = 1, y = 2 }"), expected_map ); + assert_eq!( + parse::>("toMap { x = 1, y = 2 }"), + expected_map + ); let mut expected_map = HashMap::new(); expected_map.insert("if".to_string(), 1); -- cgit v1.2.3