Skip to content

Commit c005e40

Browse files
authored
Merge pull request #246 from ChristopherRabotin/feat/enum-variant-simple-type-issue-242-impl1
2 parents 7c3457c + a2519e2 commit c005e40

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#### [0.13.0] - unreleased
66

7+
- Support enum struct variants in `SimpleType`
78
- BREAKING CHANGE: Change minimum supported version to 1.76.0 because of the `wasm_bindgen` dependency
89

910
#### [0.12.1] - 2023-02-01

serde_dhall/src/serialize.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl ser::Serializer for Serializer {
6767
type SerializeTupleVariant = ser::Impossible<Self::Ok, Self::Error>;
6868
type SerializeMap = MapSerializer;
6969
type SerializeStruct = StructSerializer;
70-
type SerializeStructVariant = ser::Impossible<Self::Ok, Self::Error>;
70+
type SerializeStructVariant = StructVariantSerializer;
7171

7272
fn serialize_bool(self, v: bool) -> Result<Self::Ok> {
7373
Ok(Num(NumKind::Bool(v)))
@@ -195,13 +195,13 @@ impl ser::Serializer for Serializer {
195195
self,
196196
_name: &'static str,
197197
_variant_index: u32,
198-
_variant: &'static str,
198+
variant: &'static str,
199199
_len: usize,
200200
) -> Result<Self::SerializeStructVariant> {
201-
Err(ErrorKind::Serialize(
202-
"Unsupported data for serialization: struct variant".to_owned(),
203-
)
204-
.into())
201+
Ok(StructVariantSerializer {
202+
variant,
203+
value: BTreeMap::new(),
204+
})
205205
}
206206

207207
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
@@ -339,6 +339,32 @@ impl ser::SerializeStruct for StructSerializer {
339339
}
340340
}
341341

342+
struct StructVariantSerializer {
343+
variant: &'static str,
344+
value: BTreeMap<String, SimpleValue>,
345+
}
346+
347+
impl ser::SerializeStructVariant for StructVariantSerializer {
348+
type Ok = SimpleValue;
349+
type Error = Error;
350+
351+
fn serialize_field<T>(&mut self, key: &'static str, val: &T) -> Result<()>
352+
where
353+
T: ?Sized + ser::Serialize,
354+
{
355+
let val: SimpleValue = val.serialize(Serializer)?;
356+
self.value.insert(key.into(), val);
357+
Ok(())
358+
}
359+
360+
fn end(self) -> Result<Self::Ok> {
361+
Ok(SimpleValue::Union(
362+
self.variant.to_string(),
363+
Some(Box::new(Record(self.value))),
364+
))
365+
}
366+
}
367+
342368
impl serde::ser::Serialize for SimpleValue {
343369
fn serialize<S>(
344370
&self,

serde_dhall/tests/enum_tests.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
mod test_enum {
2+
use serde::{Deserialize, Serialize};
3+
use serde_dhall::StaticType;
4+
5+
#[derive(
6+
Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, StaticType,
7+
)]
8+
pub struct ParentStruct {
9+
pub id0: i32,
10+
pub id1: i32,
11+
}
12+
13+
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, StaticType)]
14+
pub enum EnumVariants {
15+
SimpleStruct {
16+
x: f64,
17+
y: f64,
18+
z: f64,
19+
},
20+
InheritStruct {
21+
field_a: ParentStruct,
22+
field_b: ParentStruct,
23+
},
24+
25+
Unitary,
26+
}
27+
28+
#[test]
29+
fn test_enum_simple_struct() {
30+
let v = EnumVariants::SimpleStruct {
31+
x: 1.0,
32+
y: 2.0,
33+
z: 3.0,
34+
};
35+
let v_str = serde_dhall::serialize(&v)
36+
.static_type_annotation()
37+
.to_string()
38+
.unwrap();
39+
println!("{v_str:?}");
40+
let v_deser: EnumVariants =
41+
serde_dhall::from_str(&v_str).parse().unwrap();
42+
assert_eq!(v_deser, v);
43+
}
44+
45+
#[test]
46+
fn test_enum_inherit_struct() {
47+
let v = EnumVariants::InheritStruct {
48+
field_a: ParentStruct { id0: 399, id1: 0 },
49+
field_b: ParentStruct { id0: 301, id1: 0 },
50+
};
51+
52+
let v_str = serde_dhall::serialize(&v)
53+
.static_type_annotation()
54+
.to_string()
55+
.unwrap();
56+
println!("{v_str:?}");
57+
let v_deser: EnumVariants =
58+
serde_dhall::from_str(&v_str).parse().unwrap();
59+
assert_eq!(v_deser, v);
60+
}
61+
62+
#[test]
63+
fn test_enum_unitary() {
64+
let v = EnumVariants::Unitary;
65+
66+
let v_str = serde_dhall::serialize(&v)
67+
.static_type_annotation()
68+
.to_string()
69+
.unwrap();
70+
println!("{v_str:?}");
71+
let v_deser: EnumVariants =
72+
serde_dhall::from_str(&v_str).parse().unwrap();
73+
assert_eq!(v_deser, v);
74+
}
75+
}

0 commit comments

Comments
 (0)