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 <flokli@flokli.de>
Autosubmit: tazjin <tazjin@tvl.su>
This commit is contained in:
Vincent Ambo 2023-01-01 21:39:12 +03:00 committed by tazjin
parent 0c17718dd1
commit 0e88eb83ef
2 changed files with 27 additions and 10 deletions

View file

@ -225,14 +225,14 @@ impl<'de> de::Deserializer<'de> for NixDeserializer {
where where
V: de::Visitor<'de>, V: de::Visitor<'de>,
{ {
todo!("how to represent this?"); unimplemented!()
} }
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where where
V: de::Visitor<'de>, V: de::Visitor<'de>,
{ {
todo!("how to represent this?"); unimplemented!()
} }
// Note that this can not distinguish between a serialisation of // 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>( fn deserialize_unit_struct<V>(
self, self,
name: &'static str, _name: &'static str,
visitor: V, visitor: V,
) -> Result<V::Value, Self::Error> ) -> Result<V::Value, Self::Error>
where where
V: de::Visitor<'de>, V: de::Visitor<'de>,
{ {
todo!("how to represent this?"); self.deserialize_unit(visitor)
} }
fn deserialize_newtype_struct<V>( fn deserialize_newtype_struct<V>(
self, self,
name: &'static str, _name: &'static str,
visitor: V, visitor: V,
) -> Result<V::Value, Self::Error> ) -> Result<V::Value, Self::Error>
where where
V: de::Visitor<'de>, 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> 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)) 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 where
V: de::Visitor<'de>, V: de::Visitor<'de>,
{ {
todo!() // just represent tuples as lists ...
self.deserialize_seq(visitor)
} }
fn deserialize_tuple_struct<V>( fn deserialize_tuple_struct<V>(
self, self,
name: &'static str, _name: &'static str,
len: usize, len: usize,
visitor: V, visitor: V,
) -> Result<V::Value, Self::Error> ) -> Result<V::Value, Self::Error>
where where
V: de::Visitor<'de>, V: de::Visitor<'de>,
{ {
todo!() // same as above
self.deserialize_seq(visitor)
} }
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>

View file

@ -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));
}