diff options
author | Vincent Ambo <mail@tazj.in> | 2023-01-02T10·39+0300 |
---|---|---|
committer | tazjin <tazjin@tvl.su> | 2023-01-04T17·21+0000 |
commit | 34be6466d4a5da7dd3ad55ce80c951f21e45520c (patch) | |
tree | 28456ec15c4a2c6cae8eab74ebb5434be599b8e0 /tvix/serde/src/de_tests.rs | |
parent | 0e88eb83efb194427329ccffd3b48671e1d72107 (diff) |
feat(tvix/serde): implement enum deserialisation r/5585
Implements externally tagged enum deserialisation. Other serialisation methods are handled by serde internally using the existing methods. See the tests for examples. Change-Id: Ic4a9da3b5a32ddbb5918b1512e70c3ac5ce64f04 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7721 Tested-by: BuildkiteCI Autosubmit: tazjin <tazjin@tvl.su> Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/serde/src/de_tests.rs')
-rw-r--r-- | tvix/serde/src/de_tests.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/tvix/serde/src/de_tests.rs b/tvix/serde/src/de_tests.rs index 1613b874d949..8fe15a17e378 100644 --- a/tvix/serde/src/de_tests.rs +++ b/tvix/serde/src/de_tests.rs @@ -95,3 +95,106 @@ fn deserialize_tuple() { let result: (String, usize) = from_str(r#" [ "foo" 42 ] "#).expect("should deserialize"); assert_eq!(result, ("foo".into(), 42)); } + +#[test] +fn deserialize_unit_enum() { + #[derive(Debug, Deserialize, PartialEq)] + enum Foo { + Bar, + Baz, + } + + let result: Foo = from_str("\"Baz\"").expect("should deserialize"); + assert_eq!(result, Foo::Baz); +} + +#[test] +fn deserialize_tuple_enum() { + #[derive(Debug, Deserialize, PartialEq)] + enum Foo { + Bar, + Baz(String, usize), + } + + let result: Foo = from_str( + r#" + { + Baz = [ "Slartibartfast" 42 ]; + } + "#, + ) + .expect("should deserialize"); + + assert_eq!(result, Foo::Baz("Slartibartfast".into(), 42)); +} + +#[test] +fn deserialize_struct_enum() { + #[derive(Debug, Deserialize, PartialEq)] + enum Foo { + Bar, + Baz { name: String, age: usize }, + } + + let result: Foo = from_str( + r#" + { + Baz.name = "Slartibartfast"; + Baz.age = 42; + } + "#, + ) + .expect("should deserialize"); + + assert_eq!( + result, + Foo::Baz { + name: "Slartibartfast".into(), + age: 42 + } + ); +} + +#[test] +fn deserialize_enum_all() { + #[derive(Debug, Deserialize, PartialEq)] + #[serde(rename_all = "snake_case")] + enum TestEnum { + UnitVariant, + TupleVariant(String, String), + StructVariant { name: String, age: usize }, + } + + let result: Vec<TestEnum> = from_str( + r#" + let + mkTuple = country: drink: { tuple_variant = [ country drink ]; }; + in + [ + (mkTuple "UK" "cask ale") + + "unit_variant" + + { + struct_variant.name = "Slartibartfast"; + struct_variant.age = 42; + } + + (mkTuple "Russia" "квас") + ] + "#, + ) + .expect("should deserialize"); + + let expected = vec![ + TestEnum::TupleVariant("UK".into(), "cask ale".into()), + TestEnum::UnitVariant, + TestEnum::StructVariant { + name: "Slartibartfast".into(), + age: 42, + }, + TestEnum::TupleVariant("Russia".into(), "квас".into()), + ]; + + assert_eq!(result, expected); +} |