diff options
author | Nadrieril | 2020-10-28 23:21:19 +0000 |
---|---|---|
committer | Nadrieril | 2020-10-28 23:21:19 +0000 |
commit | 93ed3cf67c49bf7016b8b1780d873cfdffcb84c5 (patch) | |
tree | ef2890f1cd079acbb1e559ab9d4ba887ff39ce0b /serde_dhall | |
parent | 48bb9c03b0ace9efa33315e06c47a868e5c4ed36 (diff) |
Implement SimpleValue serialization
Diffstat (limited to 'serde_dhall')
-rw-r--r-- | serde_dhall/src/serialize.rs | 46 | ||||
-rw-r--r-- | serde_dhall/tests/simple_value.rs | 31 |
2 files changed, 61 insertions, 16 deletions
diff --git a/serde_dhall/src/serialize.rs b/serde_dhall/src/serialize.rs index 632dace..286c04d 100644 --- a/serde_dhall/src/serialize.rs +++ b/serde_dhall/src/serialize.rs @@ -347,6 +347,50 @@ impl serde::ser::Serialize for SimpleValue { where S: serde::ser::Serializer, { - todo!() + use serde::ser::{SerializeMap, SerializeSeq}; + use NumKind::*; + use SimpleValue::*; + + match self { + Num(Bool(x)) => serializer.serialize_bool(*x), + Num(Natural(x)) => serializer.serialize_u64(*x), + Num(Integer(x)) => serializer.serialize_i64(*x), + Num(Double(x)) => serializer.serialize_f64((*x).into()), + Text(x) => serializer.serialize_str(x), + List(xs) => { + let mut seq = serializer.serialize_seq(Some(xs.len()))?; + for x in xs { + seq.serialize_element(x)?; + } + seq.end() + } + Optional(None) => serializer.serialize_none(), + Optional(Some(x)) => serializer.serialize_some(x), + Record(m) => { + let mut map = serializer.serialize_map(Some(m.len()))?; + for (k, v) in m { + map.serialize_entry(k, v)?; + } + map.end() + } + // serde's enum support is yet again really limited. We can't avoid a memleak here :(. + Union(field_name, None) => { + let field_name: Box<str> = field_name.clone().into(); + serializer.serialize_unit_variant( + "SimpleValue", + 0, + Box::leak(field_name), + ) + } + Union(field_name, Some(x)) => { + let field_name: Box<str> = field_name.clone().into(); + serializer.serialize_newtype_variant( + "SimpleValue", + 0, + Box::leak(field_name), + x, + ) + } + } } } diff --git a/serde_dhall/tests/simple_value.rs b/serde_dhall/tests/simple_value.rs index d2792d4..3bd9d64 100644 --- a/serde_dhall/tests/simple_value.rs +++ b/serde_dhall/tests/simple_value.rs @@ -31,27 +31,28 @@ mod simple_value { fn test_serde() { let bool_true = SimpleValue::Num(NumKind::Bool(true)); // https://github.com/Nadrieril/dhall-rust/issues/184 - // assert_serde("[ True ]", vec![bool_true.clone()]); + assert_serde("[True]", vec![bool_true.clone()]); + assert_de("< Foo >.Foo", SimpleValue::Union("Foo".into(), None)); assert_de( "< Foo: Bool >.Foo True", SimpleValue::Union("Foo".into(), Some(Box::new(bool_true.clone()))), ); - // assert_eq!( - // serialize(&SimpleValue::Optional(None)).to_string().map_err(|e| e.to_string()), - // Err("cannot serialize value without a type annotation: Optional(None)".to_string()) - // ); + assert_eq!( + serialize(&SimpleValue::Union("Foo".into(), None)).to_string().map_err(|e| e.to_string()), + Err("cannot serialize value without a type annotation: Union(\"Foo\", None)".to_string()) + ); - // #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] - // struct Foo { - // foo: SimpleValue, - // } - // assert_serde( - // "{ foo = True }", - // Foo { - // foo: bool_true.clone(), - // }, - // ); + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] + struct Foo { + foo: SimpleValue, + } + assert_serde( + "{ foo = True }", + Foo { + foo: bool_true.clone(), + }, + ); } } |