diff options
author | Nadrieril | 2020-04-10 12:22:44 +0100 |
---|---|---|
committer | GitHub | 2020-04-10 12:22:44 +0100 |
commit | f78da0fb1f338123a226ec4d576b5329fffb5148 (patch) | |
tree | 627fa7b0b2b951efc4102d02e0a8f5a05e4aa03e /serde_dhall/src/value.rs | |
parent | da80ef06a91e3869cc3c1d4dbd07259c408ff490 (diff) | |
parent | efe4b340bebaa7ef8bce6e69194959b126c5fade (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/src/value.rs')
-rw-r--r-- | serde_dhall/src/value.rs | 58 |
1 files changed, 53 insertions, 5 deletions
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( |