about summary refs log tree commit diff
path: root/tvix/serde/src
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-01-01T18·39+0300
committertazjin <tazjin@tvl.su>2023-01-04T17·21+0000
commit0e88eb83efb194427329ccffd3b48671e1d72107 (patch)
tree86f5c4fd48ac9eca9956db40f8c1355ac0e06389 /tvix/serde/src
parent0c17718dd10f8b53b523eb8c54a525c7af166712 (diff)
feat(tvix/serde): add newtype & tuple deserialisation r/5584
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 <flokli@flokli.de>
Autosubmit: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/serde/src')
-rw-r--r--tvix/serde/src/de.rs22
-rw-r--r--tvix/serde/src/de_tests.rs15
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<V>(self, visitor: V) -> Result<V::Value, Self::Error>
     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<V>(
         self,
-        name: &'static str,
+        _name: &'static str,
         visitor: V,
     ) -> Result<V::Value, Self::Error>
     where
         V: de::Visitor<'de>,
     {
-        todo!("how to represent this?");
+        self.deserialize_unit(visitor)
     }
 
     fn deserialize_newtype_struct<V>(
         self,
-        name: &'static str,
+        _name: &'static str,
         visitor: V,
     ) -> Result<V::Value, Self::Error>
     where
         V: de::Visitor<'de>,
     {
-        todo!("how to represent this?");
+        visitor.visit_newtype_struct(self)
     }
 
     fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
@@ -296,23 +296,25 @@ impl<'de> de::Deserializer<'de> for NixDeserializer {
         Err(unexpected("list", &self.value))
     }
 
-    fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
+    fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
     where
         V: de::Visitor<'de>,
     {
-        todo!()
+        // just represent tuples as lists ...
+        self.deserialize_seq(visitor)
     }
 
     fn deserialize_tuple_struct<V>(
         self,
-        name: &'static str,
+        _name: &'static str,
         len: usize,
         visitor: V,
     ) -> Result<V::Value, Self::Error>
     where
         V: de::Visitor<'de>,
     {
-        todo!()
+        // same as above
+        self.deserialize_seq(visitor)
     }
 
     fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
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));
+}