From e8bbd330e08c4d2fd34195db52f984c64ab967d7 Mon Sep 17 00:00:00 2001 From: est31 Date: Mon, 2 Sep 2019 03:37:01 +0200 Subject: [PATCH] Deserialize tuples with serde --- src/object/types.rs | 30 ------------------------------ src/parser/deserializer.rs | 16 ++++++++++++++-- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/src/object/types.rs b/src/object/types.rs index e766cbaf4..ea1a5dbee 100644 --- a/src/object/types.rs +++ b/src/object/types.rs @@ -15,36 +15,6 @@ impl ExtractType for T { ))) } } - -impl ExtractType for (T, U) { - fn extract(value: &Tagged) -> Result<(T, U), ShellError> { - let t_name = std::any::type_name::(); - let u_name = std::any::type_name::(); - - trace!("Extracting {:?} for ({}, {})", value, t_name, u_name); - - match value.item() { - Value::List(items) => { - if items.len() == 2 { - let first = &items[0]; - let second = &items[1]; - - Ok((T::extract(first)?, U::extract(second)?)) - } else { - Err(ShellError::type_error( - "two-element-tuple", - "not-two".tagged(value.tag()), - )) - } - } - other => Err(ShellError::type_error( - "two-element-tuple", - other.type_name().tagged(value.tag()), - )), - } - } -} - impl ExtractType for Option { fn extract(value: &Tagged) -> Result, ShellError> { let name = std::any::type_name::(); diff --git a/src/parser/deserializer.rs b/src/parser/deserializer.rs index 66f83c656..48b4239f4 100644 --- a/src/parser/deserializer.rs +++ b/src/parser/deserializer.rs @@ -218,11 +218,23 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut ConfigDeserializer<'de> { )), } } - fn deserialize_tuple(self, _len: usize, _visitor: V) -> Result + fn deserialize_tuple(mut self, len: usize, visitor: V) -> Result where V: Visitor<'de>, { - unimplemented!("deserialize_tuple") + let value = self.pop(); + trace!(" Extracting {:?} for tuple with {} elements", value.val, len); + + match value.val.into_parts() { + (Value::List(items), _) => { + let de = SeqDeserializer::new(&mut self, items.into_iter()); + visitor.visit_seq(de) + } + (other, tag) => Err(ShellError::type_error( + "Tuple", + other.type_name().tagged(tag), + )), + } } fn deserialize_tuple_struct( self,