From d80e1c96fa83bf800c494c029d90a2493b811b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Biel?= Date: Wed, 10 Feb 2021 18:11:03 +0100 Subject: [PATCH] Add IntoDeserializer implementation to Value enum --- src/value/mod.rs | 10 +++++++++- tests/test_value.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/value/mod.rs b/src/value/mod.rs index 1365f957..031825e6 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -1,6 +1,6 @@ use crate::ser::SerializerToYaml; use crate::{Error, Mapping}; -use serde::de::{Deserialize, DeserializeOwned}; +use serde::de::{Deserialize, DeserializeOwned, IntoDeserializer}; use serde::Serialize; use std::f64; use std::hash::{Hash, Hasher}; @@ -633,6 +633,14 @@ impl Hash for Value { } } +impl<'de> IntoDeserializer<'de, Error> for Value { + type Deserializer = Self; + + fn into_deserializer(self) -> Self::Deserializer { + self + } +} + mod from; mod index; mod partial_eq; diff --git a/tests/test_value.rs b/tests/test_value.rs index 869c60f5..4e38b65c 100644 --- a/tests/test_value.rs +++ b/tests/test_value.rs @@ -1,5 +1,8 @@ #![allow(clippy::eq_op)] +use serde::de::IntoDeserializer; +use serde::Deserialize; +use serde_derive::Deserialize; use serde_yaml::{Number, Value}; use std::f64; @@ -23,3 +26,30 @@ fn test_digits() { let num_string = serde_yaml::from_str::("01").unwrap(); assert!(num_string.is_string()); } + +#[test] +fn test_into_deserializer() { + let value = serde_yaml::from_str::("xyz").unwrap(); + let s = String::deserialize(value.into_deserializer()).unwrap(); + assert_eq!(s, "xyz"); + + let value = serde_yaml::from_str::("- first\n- second\n- third").unwrap(); + let arr = Vec::::deserialize(value.into_deserializer()).unwrap(); + assert_eq!(arr, &["first", "second", "third"]); + + #[derive(Debug, Deserialize, PartialEq)] + struct Test { + first: String, + second: u32, + } + + let value = serde_yaml::from_str::("first: abc\nsecond: 99").unwrap(); + let test = Test::deserialize(value.into_deserializer()).unwrap(); + assert_eq!( + test, + Test { + first: "abc".to_string(), + second: 99 + } + ) +}