From 0e88eb83efb194427329ccffd3b48671e1d72107 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 1 Jan 2023 21:39:12 +0300 Subject: feat(tvix/serde): add newtype & tuple deserialisation Only missing enums at this point, but they're a bit of a beast. Change-Id: I4ad47c034851f9a8794c81f39a5149a8ac1826e8 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7716 Tested-by: BuildkiteCI Reviewed-by: flokli Autosubmit: tazjin --- tvix/serde/src/de.rs | 22 ++++++++++++---------- tvix/serde/src/de_tests.rs | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/tvix/serde/src/de.rs b/tvix/serde/src/de.rs index 0999e8a60368..2f7b2ba4d66d 100644 --- a/tvix/serde/src/de.rs +++ b/tvix/serde/src/de.rs @@ -225,14 +225,14 @@ impl<'de> de::Deserializer<'de> for NixDeserializer { where V: de::Visitor<'de>, { - todo!("how to represent this?"); + unimplemented!() } fn deserialize_byte_buf(self, visitor: V) -> Result where V: de::Visitor<'de>, { - todo!("how to represent this?"); + unimplemented!() } // Note that this can not distinguish between a serialisation of @@ -261,24 +261,24 @@ impl<'de> de::Deserializer<'de> for NixDeserializer { fn deserialize_unit_struct( self, - name: &'static str, + _name: &'static str, visitor: V, ) -> Result where V: de::Visitor<'de>, { - todo!("how to represent this?"); + self.deserialize_unit(visitor) } fn deserialize_newtype_struct( self, - name: &'static str, + _name: &'static str, visitor: V, ) -> Result where V: de::Visitor<'de>, { - todo!("how to represent this?"); + visitor.visit_newtype_struct(self) } fn deserialize_seq(self, visitor: V) -> Result @@ -296,23 +296,25 @@ impl<'de> de::Deserializer<'de> for NixDeserializer { Err(unexpected("list", &self.value)) } - fn deserialize_tuple(self, len: usize, visitor: V) -> Result + fn deserialize_tuple(self, _len: usize, visitor: V) -> Result where V: de::Visitor<'de>, { - todo!() + // just represent tuples as lists ... + self.deserialize_seq(visitor) } fn deserialize_tuple_struct( self, - name: &'static str, + _name: &'static str, len: usize, visitor: V, ) -> Result where V: de::Visitor<'de>, { - todo!() + // same as above + self.deserialize_seq(visitor) } fn deserialize_map(self, visitor: V) -> Result diff --git a/tvix/serde/src/de_tests.rs b/tvix/serde/src/de_tests.rs index 1b9bc04d2362..1613b874d949 100644 --- a/tvix/serde/src/de_tests.rs +++ b/tvix/serde/src/de_tests.rs @@ -80,3 +80,18 @@ fn deserialize_struct() { } ); } + +#[test] +fn deserialize_newtype() { + #[derive(Debug, Deserialize, PartialEq)] + struct Number(usize); + + let result: Number = from_str("42").expect("should deserialize"); + assert_eq!(result, Number(42)); +} + +#[test] +fn deserialize_tuple() { + let result: (String, usize) = from_str(r#" [ "foo" 42 ] "#).expect("should deserialize"); + assert_eq!(result, ("foo".into(), 42)); +} -- cgit 1.4.1