summaryrefslogtreecommitdiff
path: root/serde_dhall
diff options
context:
space:
mode:
authorNadrieril2020-04-10 12:22:44 +0100
committerGitHub2020-04-10 12:22:44 +0100
commitf78da0fb1f338123a226ec4d576b5329fffb5148 (patch)
tree627fa7b0b2b951efc4102d02e0a8f5a05e4aa03e /serde_dhall
parentda80ef06a91e3869cc3c1d4dbd07259c408ff490 (diff)
parentefe4b340bebaa7ef8bce6e69194959b126c5fade (diff)
Merge pull request #160 from Nadrieril/toMap
Deserialize `Prelude.Map` and `toMap` to a map instead of a list
Diffstat (limited to 'serde_dhall')
-rw-r--r--serde_dhall/src/static_type.rs1
-rw-r--r--serde_dhall/src/value.rs58
-rw-r--r--serde_dhall/tests/de.rs4
3 files changed, 58 insertions, 5 deletions
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<String, T>`, structs
/// `< x: T \| y: U >` | enums
+/// `Prelude.Map.Type Text T` | `HashMap<String, T>`, 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::<Option<_>>()?,
- ),
+ 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::<Option<_>>()?,
+ ));
+ }
+ }
+ SimpleValue::List(
+ xs.iter().map(Self::from_nir).collect::<Option<_>>()?,
+ )
+ }
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::<Option<_>>()?,
),
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::<HashMap<String, usize>>("{ x = 1, y = 2 }"),
expected_map
);
+ assert_eq!(
+ parse::<HashMap<String, usize>>("toMap { x = 1, y = 2 }"),
+ expected_map
+ );
let mut expected_map = HashMap::new();
expected_map.insert("if".to_string(), 1);