Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Add IntoDeserializer implementation to Value enum
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-biel committed Feb 10, 2021
1 parent 26e1070 commit d80e1c9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/value/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions tests/test_value.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -23,3 +26,30 @@ fn test_digits() {
let num_string = serde_yaml::from_str::<Value>("01").unwrap();
assert!(num_string.is_string());
}

#[test]
fn test_into_deserializer() {
let value = serde_yaml::from_str::<Value>("xyz").unwrap();
let s = String::deserialize(value.into_deserializer()).unwrap();
assert_eq!(s, "xyz");

let value = serde_yaml::from_str::<Value>("- first\n- second\n- third").unwrap();
let arr = Vec::<String>::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::<Value>("first: abc\nsecond: 99").unwrap();
let test = Test::deserialize(value.into_deserializer()).unwrap();
assert_eq!(
test,
Test {
first: "abc".to_string(),
second: 99
}
)
}

0 comments on commit d80e1c9

Please sign in to comment.