rhai/src/serde/de.rs

627 lines
20 KiB
Rust
Raw Normal View History

2020-11-20 09:52:28 +01:00
//! Implement deserialization support of [`Dynamic`][crate::Dynamic] for [`serde`].
2020-07-04 09:39:40 +02:00
2021-03-29 12:46:32 +02:00
use super::str::StringSliceDeserializer;
2020-11-16 16:10:14 +01:00
use crate::dynamic::Union;
2020-12-22 08:27:27 +01:00
use crate::{Dynamic, EvalAltResult, ImmutableString, LexError, Position};
use serde::de::{DeserializeSeed, Error, IntoDeserializer, MapAccess, SeqAccess, Visitor};
use serde::{Deserialize, Deserializer};
2021-04-17 09:15:54 +02:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{any::type_name, fmt};
2020-07-03 11:19:55 +02:00
#[cfg(not(feature = "no_index"))]
2020-11-16 09:28:04 +01:00
use crate::Array;
2020-07-26 09:53:22 +02:00
2020-07-03 11:19:55 +02:00
#[cfg(not(feature = "no_object"))]
2020-11-16 09:28:04 +01:00
use crate::Map;
2020-07-26 09:53:22 +02:00
2020-11-20 09:52:28 +01:00
/// Deserializer for [`Dynamic`][crate::Dynamic] which is kept as a reference.
2020-07-04 09:39:40 +02:00
///
/// The reference is necessary because the deserialized type may hold references
2020-11-20 09:52:28 +01:00
/// (especially `&str`) to the source [`Dynamic`][crate::Dynamic].
2021-02-19 08:17:14 +01:00
struct DynamicDeserializer<'a> {
2020-07-03 11:19:55 +02:00
value: &'a Dynamic,
}
2020-07-03 16:42:56 +02:00
impl<'de> DynamicDeserializer<'de> {
2020-11-20 09:52:28 +01:00
/// Create a [`DynamicDeserializer`] from a reference to a [`Dynamic`][crate::Dynamic] value.
2020-07-04 09:39:40 +02:00
///
/// The reference is necessary because the deserialized type may hold references
2020-11-20 09:52:28 +01:00
/// (especially `&str`) to the source [`Dynamic`][crate::Dynamic].
2021-06-12 16:47:43 +02:00
#[must_use]
2020-07-03 16:42:56 +02:00
pub fn from_dynamic(value: &'de Dynamic) -> Self {
2020-07-03 11:19:55 +02:00
Self { value }
}
2020-07-04 09:39:40 +02:00
/// Shortcut for a type conversion error.
fn type_error<T>(&self) -> Result<T, Box<EvalAltResult>> {
2020-07-26 09:53:22 +02:00
self.type_error_str(type_name::<T>())
}
/// Shortcut for a type conversion error.
fn type_error_str<T>(&self, error: &str) -> Result<T, Box<EvalAltResult>> {
Err(EvalAltResult::ErrorMismatchOutputType(
2020-11-20 09:52:28 +01:00
error.into(),
self.value.type_name().into(),
Position::NONE,
)
.into())
2020-07-03 11:19:55 +02:00
}
fn deserialize_int<V: Visitor<'de>>(
&mut self,
v: crate::INT,
visitor: V,
) -> Result<V::Value, Box<EvalAltResult>> {
#[cfg(not(feature = "only_i32"))]
return visitor.visit_i64(v);
#[cfg(feature = "only_i32")]
return visitor.visit_i32(v);
}
2020-07-03 11:19:55 +02:00
}
2020-11-20 09:52:28 +01:00
/// Deserialize a [`Dynamic`][crate::Dynamic] value into a Rust type that implements [`serde::Deserialize`].
2020-07-04 09:39:40 +02:00
///
2020-10-27 04:30:38 +01:00
/// # Example
2020-07-04 09:39:40 +02:00
///
/// ```
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # #[cfg(not(feature = "no_index"))]
/// # #[cfg(not(feature = "no_object"))]
/// # {
/// use rhai::{Dynamic, Array, Map, INT};
2020-10-10 07:43:14 +02:00
/// use rhai::serde::from_dynamic;
2020-07-04 09:39:40 +02:00
/// use serde::Deserialize;
///
/// #[derive(Debug, Deserialize, PartialEq)]
/// struct Hello {
/// a: INT,
/// b: bool,
/// }
///
/// #[derive(Debug, Deserialize, PartialEq)]
/// struct Test {
/// int: u32,
/// seq: Vec<String>,
/// obj: Hello,
/// }
///
/// let mut map = Map::new();
/// map.insert("int".into(), Dynamic::from(42_u32));
///
/// let mut map2 = Map::new();
/// map2.insert("a".into(), (123 as INT).into());
/// map2.insert("b".into(), true.into());
///
/// map.insert("obj".into(), map2.into());
///
/// let arr: Array = vec!["foo".into(), "bar".into(), "baz".into()];
/// map.insert("seq".into(), arr.into());
///
/// let value: Test = from_dynamic(&map.into())?;
///
/// let expected = Test {
/// int: 42,
/// seq: vec!["foo".into(), "bar".into(), "baz".into()],
/// obj: Hello { a: 123, b: true },
/// };
///
/// assert_eq!(value, expected);
/// # }
/// # Ok(())
/// # }
/// ```
2020-07-03 11:19:55 +02:00
pub fn from_dynamic<'de, T: Deserialize<'de>>(
value: &'de Dynamic,
) -> Result<T, Box<EvalAltResult>> {
T::deserialize(&mut DynamicDeserializer::from_dynamic(value))
}
impl Error for Box<EvalAltResult> {
fn custom<T: fmt::Display>(err: T) -> Self {
LexError::ImproperSymbol(Default::default(), err.to_string())
2020-12-22 04:55:51 +01:00
.into_err(Position::NONE)
.into()
2020-07-03 11:19:55 +02:00
}
}
impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
type Error = Box<EvalAltResult>;
fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
2021-06-14 06:02:22 +02:00
match self.value.0 {
2021-05-02 17:57:35 +02:00
Union::Unit(_, _, _) => self.deserialize_unit(visitor),
Union::Bool(_, _, _) => self.deserialize_bool(visitor),
Union::Str(_, _, _) => self.deserialize_str(visitor),
Union::Char(_, _, _) => self.deserialize_char(visitor),
2021-02-13 13:57:56 +01:00
2020-07-03 11:19:55 +02:00
#[cfg(not(feature = "only_i32"))]
2021-05-02 17:57:35 +02:00
Union::Int(_, _, _) => self.deserialize_i64(visitor),
2020-07-03 11:19:55 +02:00
#[cfg(feature = "only_i32")]
2021-05-02 17:57:35 +02:00
Union::Int(_, _, _) => self.deserialize_i32(visitor),
2021-02-13 13:57:56 +01:00
2020-07-03 11:19:55 +02:00
#[cfg(not(feature = "no_float"))]
2021-02-13 13:57:56 +01:00
#[cfg(not(feature = "f32_float"))]
2021-05-02 17:57:35 +02:00
Union::Float(_, _, _) => self.deserialize_f64(visitor),
2021-02-13 13:57:56 +01:00
#[cfg(not(feature = "no_float"))]
#[cfg(feature = "f32_float")]
2021-05-02 17:57:35 +02:00
Union::Float(_, _, _) => self.deserialize_f32(visitor),
2021-02-13 13:57:56 +01:00
#[cfg(feature = "decimal")]
#[cfg(not(feature = "f32_float"))]
2021-05-02 17:57:35 +02:00
Union::Decimal(_, _, _) => self.deserialize_f64(visitor),
2021-02-13 13:57:56 +01:00
#[cfg(feature = "decimal")]
#[cfg(feature = "f32_float")]
2021-05-02 17:57:35 +02:00
Union::Decimal(_, _, _) => self.deserialize_f32(visitor),
2021-02-13 13:57:56 +01:00
2020-07-03 11:19:55 +02:00
#[cfg(not(feature = "no_index"))]
2021-05-02 17:57:35 +02:00
Union::Array(_, _, _) => self.deserialize_seq(visitor),
2020-07-03 11:19:55 +02:00
#[cfg(not(feature = "no_object"))]
2021-05-02 17:57:35 +02:00
Union::Map(_, _, _) => self.deserialize_map(visitor),
Union::FnPtr(_, _, _) => self.type_error(),
2020-07-03 11:19:55 +02:00
#[cfg(not(feature = "no_std"))]
2021-05-02 17:57:35 +02:00
Union::TimeStamp(_, _, _) => self.type_error(),
2020-07-03 11:19:55 +02:00
2021-06-14 06:02:22 +02:00
Union::Variant(ref value, _, _) if value.is::<i8>() => self.deserialize_i8(visitor),
Union::Variant(ref value, _, _) if value.is::<i16>() => self.deserialize_i16(visitor),
Union::Variant(ref value, _, _) if value.is::<i32>() => self.deserialize_i32(visitor),
Union::Variant(ref value, _, _) if value.is::<i64>() => self.deserialize_i64(visitor),
Union::Variant(ref value, _, _) if value.is::<i128>() => self.deserialize_i128(visitor),
Union::Variant(ref value, _, _) if value.is::<u8>() => self.deserialize_u8(visitor),
Union::Variant(ref value, _, _) if value.is::<u16>() => self.deserialize_u16(visitor),
Union::Variant(ref value, _, _) if value.is::<u32>() => self.deserialize_u32(visitor),
Union::Variant(ref value, _, _) if value.is::<u64>() => self.deserialize_u64(visitor),
Union::Variant(ref value, _, _) if value.is::<u128>() => self.deserialize_u128(visitor),
2020-07-03 11:19:55 +02:00
2021-05-02 17:57:35 +02:00
Union::Variant(_, _, _) => self.type_error(),
2020-08-04 03:47:48 +02:00
#[cfg(not(feature = "no_closure"))]
2021-05-02 17:57:35 +02:00
Union::Shared(_, _, _) => self.type_error(),
2020-07-03 11:19:55 +02:00
}
}
fn deserialize_bool<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
2020-07-04 09:39:40 +02:00
visitor.visit_bool(self.value.as_bool().or_else(|_| self.type_error())?)
2020-07-03 11:19:55 +02:00
}
fn deserialize_i8<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<i8>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_i8(x))
}
2020-07-03 11:19:55 +02:00
}
fn deserialize_i16<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<i16>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_i16(x))
}
2020-07-03 11:19:55 +02:00
}
fn deserialize_i32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else if cfg!(feature = "only_i32") {
self.type_error()
} else {
self.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<i32>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_i32(x))
}
2020-07-03 11:19:55 +02:00
}
fn deserialize_i64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else if cfg!(not(feature = "only_i32")) {
self.type_error()
} else {
self.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<i64>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_i64(x))
}
2020-07-03 11:19:55 +02:00
}
fn deserialize_i128<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else if cfg!(not(feature = "only_i32")) {
self.type_error()
} else {
self.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<i128>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_i128(x))
}
}
2020-07-03 11:19:55 +02:00
fn deserialize_u8<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<u8>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_u8(x))
}
2020-07-03 11:19:55 +02:00
}
fn deserialize_u16<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<u16>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_u16(x))
}
2020-07-03 11:19:55 +02:00
}
fn deserialize_u32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<u32>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_u32(x))
}
2020-07-03 11:19:55 +02:00
}
fn deserialize_u64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<u64>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_u64(x))
}
2020-07-03 11:19:55 +02:00
}
fn deserialize_u128<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<u128>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_u128(x))
}
}
2020-07-26 09:53:22 +02:00
fn deserialize_f32<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
2020-07-03 11:19:55 +02:00
#[cfg(not(feature = "no_float"))]
2020-07-04 16:53:00 +02:00
return self
.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<f32>()
2020-07-26 09:53:22 +02:00
.map_or_else(|| self.type_error(), |&x| _visitor.visit_f32(x));
2020-07-04 16:53:00 +02:00
2020-07-03 11:19:55 +02:00
#[cfg(feature = "no_float")]
2021-02-13 13:57:56 +01:00
#[cfg(feature = "decimal")]
{
use rust_decimal::prelude::ToPrimitive;
return self
.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<rust_decimal::Decimal>()
2021-02-13 13:57:56 +01:00
.and_then(|&x| x.to_f32())
.map_or_else(|| self.type_error(), |v| _visitor.visit_f32(v));
}
#[cfg(feature = "no_float")]
#[cfg(not(feature = "decimal"))]
2020-07-04 16:53:00 +02:00
return self.type_error_str("f32");
2020-07-03 11:19:55 +02:00
}
2020-07-26 09:53:22 +02:00
fn deserialize_f64<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
2020-07-03 11:19:55 +02:00
#[cfg(not(feature = "no_float"))]
2020-07-04 16:53:00 +02:00
return self
.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<f64>()
2020-07-26 09:53:22 +02:00
.map_or_else(|| self.type_error(), |&x| _visitor.visit_f64(x));
2020-07-04 16:53:00 +02:00
2020-07-03 11:19:55 +02:00
#[cfg(feature = "no_float")]
2021-02-13 13:57:56 +01:00
#[cfg(feature = "decimal")]
{
use rust_decimal::prelude::ToPrimitive;
return self
.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<rust_decimal::Decimal>()
2021-02-13 13:57:56 +01:00
.and_then(|&x| x.to_f64())
.map_or_else(|| self.type_error(), |v| _visitor.visit_f64(v));
}
#[cfg(feature = "no_float")]
#[cfg(not(feature = "decimal"))]
2020-07-04 16:53:00 +02:00
return self.type_error_str("f64");
2020-07-03 11:19:55 +02:00
}
fn deserialize_char<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
self.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<char>()
2020-07-04 09:39:40 +02:00
.map_or_else(|| self.type_error(), |&x| visitor.visit_char(x))
2020-07-03 11:19:55 +02:00
}
fn deserialize_str<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
2021-03-31 04:16:38 +02:00
self.value.downcast_ref::<ImmutableString>().map_or_else(
2020-07-04 09:39:40 +02:00
|| self.type_error(),
2020-07-03 11:19:55 +02:00
|x| visitor.visit_borrowed_str(x.as_str()),
)
}
fn deserialize_string<V: Visitor<'de>>(
self,
visitor: V,
) -> Result<V::Value, Box<EvalAltResult>> {
self.deserialize_str(visitor)
}
fn deserialize_bytes<V: Visitor<'de>>(self, _: V) -> Result<V::Value, Box<EvalAltResult>> {
2020-07-04 09:39:40 +02:00
self.type_error()
2020-07-03 11:19:55 +02:00
}
fn deserialize_byte_buf<V: Visitor<'de>>(self, _: V) -> Result<V::Value, Box<EvalAltResult>> {
2020-07-04 09:39:40 +02:00
self.type_error()
2020-07-03 11:19:55 +02:00
}
fn deserialize_option<V: Visitor<'de>>(self, _: V) -> Result<V::Value, Box<EvalAltResult>> {
2020-07-04 09:39:40 +02:00
self.type_error()
2020-07-03 11:19:55 +02:00
}
fn deserialize_unit<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
self.value
2021-03-31 04:16:38 +02:00
.downcast_ref::<()>()
2020-07-04 09:39:40 +02:00
.map_or_else(|| self.type_error(), |_| visitor.visit_unit())
2020-07-03 11:19:55 +02:00
}
fn deserialize_unit_struct<V: Visitor<'de>>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Box<EvalAltResult>> {
self.deserialize_unit(visitor)
}
fn deserialize_newtype_struct<V: Visitor<'de>>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Box<EvalAltResult>> {
visitor.visit_newtype_struct(self)
}
2020-07-26 09:53:22 +02:00
fn deserialize_seq<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
2020-07-03 11:19:55 +02:00
#[cfg(not(feature = "no_index"))]
2021-03-31 04:16:38 +02:00
return self.value.downcast_ref::<Array>().map_or_else(
2020-07-04 16:53:00 +02:00
|| self.type_error(),
2020-07-26 09:53:22 +02:00
|arr| _visitor.visit_seq(IterateArray::new(arr.iter())),
2020-07-04 16:53:00 +02:00
);
2020-07-03 11:19:55 +02:00
#[cfg(feature = "no_index")]
2020-07-04 16:53:00 +02:00
return self.type_error();
2020-07-03 11:19:55 +02:00
}
fn deserialize_tuple<V: Visitor<'de>>(
self,
_len: usize,
visitor: V,
) -> Result<V::Value, Box<EvalAltResult>> {
self.deserialize_seq(visitor)
}
fn deserialize_tuple_struct<V: Visitor<'de>>(
self,
_name: &'static str,
_len: usize,
visitor: V,
) -> Result<V::Value, Box<EvalAltResult>> {
self.deserialize_seq(visitor)
}
2020-07-26 09:53:22 +02:00
fn deserialize_map<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
2020-07-03 11:19:55 +02:00
#[cfg(not(feature = "no_object"))]
2021-03-31 04:16:38 +02:00
return self.value.downcast_ref::<Map>().map_or_else(
2020-07-04 16:53:00 +02:00
|| self.type_error(),
2021-03-29 12:46:32 +02:00
|map| {
_visitor.visit_map(IterateMap::new(
map.keys().map(|key| key.as_str()),
map.values(),
))
},
2020-07-04 16:53:00 +02:00
);
2020-07-03 11:19:55 +02:00
#[cfg(feature = "no_object")]
2020-07-04 16:53:00 +02:00
return self.type_error();
2020-07-03 11:19:55 +02:00
}
fn deserialize_struct<V: Visitor<'de>>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Box<EvalAltResult>> {
self.deserialize_map(visitor)
}
fn deserialize_enum<V: Visitor<'de>>(
self,
_name: &'static str,
_variants: &'static [&'static str],
visitor: V,
2020-07-03 11:19:55 +02:00
) -> Result<V::Value, Box<EvalAltResult>> {
2021-03-05 16:41:20 +01:00
if let Some(s) = self.value.read_lock::<ImmutableString>() {
visitor.visit_enum(s.as_str().into_deserializer())
} else {
#[cfg(not(feature = "no_object"))]
2021-03-31 04:16:38 +02:00
if let Some(map) = self.value.downcast_ref::<Map>() {
let mut iter = map.iter();
let first = iter.next();
let second = iter.next();
if let (Some((key, value)), None) = (first, second) {
visitor.visit_enum(EnumDeserializer {
tag: &key,
content: DynamicDeserializer::from_dynamic(value),
})
} else {
self.type_error()
}
} else {
self.type_error()
}
#[cfg(feature = "no_object")]
return self.type_error();
}
2020-07-03 11:19:55 +02:00
}
fn deserialize_identifier<V: Visitor<'de>>(
self,
visitor: V,
) -> Result<V::Value, Box<EvalAltResult>> {
self.deserialize_str(visitor)
}
fn deserialize_ignored_any<V: Visitor<'de>>(
self,
visitor: V,
) -> Result<V::Value, Box<EvalAltResult>> {
self.deserialize_any(visitor)
}
}
2020-07-04 09:39:40 +02:00
/// `SeqAccess` implementation for arrays.
2020-11-03 14:50:14 +01:00
struct IterateArray<'a, ITER: Iterator<Item = &'a Dynamic>> {
2020-11-20 09:52:28 +01:00
/// Iterator for a stream of [`Dynamic`][crate::Dynamic] values.
2020-07-03 11:19:55 +02:00
iter: ITER,
}
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "no_index"))]
2020-11-03 14:50:14 +01:00
impl<'a, ITER: Iterator<Item = &'a Dynamic>> IterateArray<'a, ITER> {
2021-06-12 16:47:43 +02:00
#[must_use]
2020-07-03 11:19:55 +02:00
pub fn new(iter: ITER) -> Self {
Self { iter }
}
}
2020-11-03 14:50:14 +01:00
impl<'a: 'de, 'de, ITER: Iterator<Item = &'a Dynamic>> SeqAccess<'de> for IterateArray<'a, ITER> {
2020-07-03 11:19:55 +02:00
type Error = Box<EvalAltResult>;
fn next_element_seed<T: DeserializeSeed<'de>>(
&mut self,
seed: T,
) -> Result<Option<T::Value>, Box<EvalAltResult>> {
2020-07-04 09:39:40 +02:00
// Deserialize each item coming out of the iterator.
2020-07-03 11:19:55 +02:00
match self.iter.next() {
None => Ok(None),
Some(item) => seed
.deserialize(&mut DynamicDeserializer::from_dynamic(item))
.map(Some),
}
}
}
2020-07-04 09:39:40 +02:00
/// `MapAccess` implementation for maps.
struct IterateMap<'a, KEYS, VALUES>
where
2021-03-29 12:46:32 +02:00
KEYS: Iterator<Item = &'a str>,
2020-07-03 11:19:55 +02:00
VALUES: Iterator<Item = &'a Dynamic>,
2020-07-04 09:39:40 +02:00
{
2020-11-20 09:52:28 +01:00
// Iterator for a stream of [`Dynamic`][crate::Dynamic] keys.
2020-07-03 11:19:55 +02:00
keys: KEYS,
2020-11-20 09:52:28 +01:00
// Iterator for a stream of [`Dynamic`][crate::Dynamic] values.
2020-07-03 11:19:55 +02:00
values: VALUES,
}
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "no_object"))]
2020-07-04 09:39:40 +02:00
impl<'a, KEYS, VALUES> IterateMap<'a, KEYS, VALUES>
where
2021-03-29 12:46:32 +02:00
KEYS: Iterator<Item = &'a str>,
2020-07-04 09:39:40 +02:00
VALUES: Iterator<Item = &'a Dynamic>,
2020-07-03 11:19:55 +02:00
{
2021-06-12 16:47:43 +02:00
#[must_use]
2020-07-03 11:19:55 +02:00
pub fn new(keys: KEYS, values: VALUES) -> Self {
Self { keys, values }
}
}
2020-07-04 09:39:40 +02:00
impl<'a: 'de, 'de, KEYS, VALUES> MapAccess<'de> for IterateMap<'a, KEYS, VALUES>
where
2021-03-29 12:46:32 +02:00
KEYS: Iterator<Item = &'a str>,
2020-07-04 09:39:40 +02:00
VALUES: Iterator<Item = &'a Dynamic>,
2020-07-03 11:19:55 +02:00
{
type Error = Box<EvalAltResult>;
fn next_key_seed<K: DeserializeSeed<'de>>(
&mut self,
seed: K,
) -> Result<Option<K::Value>, Box<EvalAltResult>> {
2021-03-29 12:46:32 +02:00
// Deserialize each `Identifier` key coming out of the keys iterator.
2020-07-03 11:19:55 +02:00
match self.keys.next() {
None => Ok(None),
Some(item) => seed
2021-03-29 12:46:32 +02:00
.deserialize(&mut StringSliceDeserializer::from_str(item))
2020-07-03 11:19:55 +02:00
.map(Some),
}
}
fn next_value_seed<V: DeserializeSeed<'de>>(
&mut self,
seed: V,
) -> Result<V::Value, Box<EvalAltResult>> {
2020-07-04 09:39:40 +02:00
// Deserialize each value item coming out of the iterator.
2020-07-03 11:19:55 +02:00
seed.deserialize(&mut DynamicDeserializer::from_dynamic(
2021-08-26 17:58:41 +02:00
self.values.next().expect("value should exist"),
2020-07-03 11:19:55 +02:00
))
}
}
#[cfg(not(feature = "no_object"))]
struct EnumDeserializer<'t, 'de: 't> {
tag: &'t str,
content: DynamicDeserializer<'de>,
}
#[cfg(not(feature = "no_object"))]
2020-11-16 09:28:04 +01:00
impl<'t, 'de> serde::de::EnumAccess<'de> for EnumDeserializer<'t, 'de> {
type Error = Box<EvalAltResult>;
type Variant = Self;
2020-11-03 14:50:14 +01:00
fn variant_seed<V: DeserializeSeed<'de>>(
self,
seed: V,
) -> Result<(V::Value, Self::Variant), Self::Error> {
seed.deserialize(self.tag.into_deserializer())
.map(|v| (v, self))
}
}
#[cfg(not(feature = "no_object"))]
2020-11-16 09:28:04 +01:00
impl<'t, 'de> serde::de::VariantAccess<'de> for EnumDeserializer<'t, 'de> {
type Error = Box<EvalAltResult>;
fn unit_variant(mut self) -> Result<(), Self::Error> {
Deserialize::deserialize(&mut self.content)
}
2020-11-03 14:50:14 +01:00
fn newtype_variant_seed<T: DeserializeSeed<'de>>(
mut self,
seed: T,
) -> Result<T::Value, Self::Error> {
seed.deserialize(&mut self.content)
}
2020-11-03 14:50:14 +01:00
fn tuple_variant<V: Visitor<'de>>(
mut self,
len: usize,
visitor: V,
) -> Result<V::Value, Self::Error> {
self.content.deserialize_tuple(len, visitor)
}
2020-11-03 14:50:14 +01:00
fn struct_variant<V: Visitor<'de>>(
mut self,
fields: &'static [&'static str],
visitor: V,
2020-11-03 14:50:14 +01:00
) -> Result<V::Value, Self::Error> {
self.content.deserialize_struct("", fields, visitor)
}
}