Deserialize tuples with serde

This commit is contained in:
est31 2019-09-02 03:37:01 +02:00
parent 79a779dbea
commit e8bbd330e0
2 changed files with 14 additions and 32 deletions

View File

@ -15,36 +15,6 @@ impl<T> ExtractType for T {
))) )))
} }
} }
impl<T: ExtractType, U: ExtractType> ExtractType for (T, U) {
fn extract(value: &Tagged<Value>) -> Result<(T, U), ShellError> {
let t_name = std::any::type_name::<T>();
let u_name = std::any::type_name::<U>();
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<T: ExtractType> ExtractType for Option<T> { impl<T: ExtractType> ExtractType for Option<T> {
fn extract(value: &Tagged<Value>) -> Result<Option<T>, ShellError> { fn extract(value: &Tagged<Value>) -> Result<Option<T>, ShellError> {
let name = std::any::type_name::<T>(); let name = std::any::type_name::<T>();

View File

@ -218,11 +218,23 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut ConfigDeserializer<'de> {
)), )),
} }
} }
fn deserialize_tuple<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error> fn deserialize_tuple<V>(mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where where
V: Visitor<'de>, V: Visitor<'de>,
{ {
unimplemented!("deserialize_tuple") let value = self.pop();
trace!("<Tuple> 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<V>( fn deserialize_tuple_struct<V>(
self, self,