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-11-13 15:36:23 +01:00
|
|
|
use crate::types::dynamic::Union;
|
2022-08-05 17:45:40 +02:00
|
|
|
use crate::{Dynamic, ImmutableString, LexError, Position, RhaiError, RhaiResultOf, ERR};
|
2021-12-25 16:49:14 +01:00
|
|
|
use serde::de::{Error, IntoDeserializer, Visitor};
|
2021-01-18 03:30:58 +01:00
|
|
|
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
|
|
|
|
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]
|
2022-08-29 08:27:05 +02:00
|
|
|
pub const 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.
|
2021-12-25 16:49:14 +01:00
|
|
|
fn type_error<T>(&self) -> RhaiResultOf<T> {
|
2020-07-26 09:53:22 +02:00
|
|
|
self.type_error_str(type_name::<T>())
|
|
|
|
}
|
|
|
|
/// Shortcut for a type conversion error.
|
2021-12-25 16:49:14 +01:00
|
|
|
fn type_error_str<T>(&self, error: &str) -> RhaiResultOf<T> {
|
2021-12-27 05:27:31 +01:00
|
|
|
Err(ERR::ErrorMismatchOutputType(
|
2020-11-20 09:52:28 +01:00
|
|
|
error.into(),
|
|
|
|
self.value.type_name().into(),
|
|
|
|
Position::NONE,
|
|
|
|
)
|
2021-10-19 17:52:58 +02:00
|
|
|
.into())
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2020-07-05 05:35:50 +02:00
|
|
|
fn deserialize_int<V: Visitor<'de>>(
|
|
|
|
&mut self,
|
|
|
|
v: crate::INT,
|
|
|
|
visitor: V,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<V::Value> {
|
2020-07-05 05:35:50 +02:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
2021-01-18 03:30:58 +01:00
|
|
|
return visitor.visit_i64(v);
|
2020-07-05 05:35:50 +02:00
|
|
|
#[cfg(feature = "only_i32")]
|
2021-01-18 03:30:58 +01:00
|
|
|
return visitor.visit_i32(v);
|
2020-07-05 05:35:50 +02:00
|
|
|
}
|
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(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2021-12-25 16:49:14 +01:00
|
|
|
pub fn from_dynamic<'de, T: Deserialize<'de>>(value: &'de Dynamic) -> RhaiResultOf<T> {
|
2020-07-03 11:19:55 +02:00
|
|
|
T::deserialize(&mut DynamicDeserializer::from_dynamic(value))
|
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
impl Error for RhaiError {
|
2020-07-03 11:19:55 +02:00
|
|
|
fn custom<T: fmt::Display>(err: T) -> Self {
|
2021-11-07 11:12:37 +01:00
|
|
|
LexError::ImproperSymbol(String::new(), 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> {
|
2021-12-25 16:49:14 +01:00
|
|
|
type Error = RhaiError;
|
2020-07-03 11:19:55 +02:00
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2021-06-14 06:02:22 +02:00
|
|
|
match self.value.0 {
|
2022-02-08 02:02:15 +01: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"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Int(..) => self.deserialize_i64(visitor),
|
2020-07-03 11:19:55 +02:00
|
|
|
#[cfg(feature = "only_i32")]
|
2022-02-08 02:02:15 +01: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"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Float(..) => self.deserialize_f64(visitor),
|
2021-02-13 13:57:56 +01:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
#[cfg(feature = "f32_float")]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Float(..) => self.deserialize_f32(visitor),
|
2021-02-13 13:57:56 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
#[cfg(not(feature = "f32_float"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Decimal(..) => self.deserialize_f64(visitor),
|
2021-02-13 13:57:56 +01:00
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
#[cfg(feature = "f32_float")]
|
2022-02-08 02:02:15 +01: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"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Array(..) => self.deserialize_seq(visitor),
|
2021-11-23 07:58:54 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Blob(..) => self.deserialize_bytes(visitor),
|
2020-07-03 11:19:55 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-02-08 02:02:15 +01: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"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::TimeStamp(..) => self.type_error(),
|
2020-07-03 11:19:55 +02:00
|
|
|
|
2022-02-08 02:02:15 +01: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
|
|
|
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Variant(..) => self.type_error(),
|
2020-08-04 03:47:48 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Union::Shared(..) => self.type_error(),
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_bool<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
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
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_i8<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-05 05:35:50 +02:00
|
|
|
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>()
|
2020-07-05 05:35:50 +02:00
|
|
|
.map_or_else(|| self.type_error(), |&x| visitor.visit_i8(x))
|
|
|
|
}
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_i16<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-05 05:35:50 +02:00
|
|
|
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>()
|
2020-07-05 05:35:50 +02:00
|
|
|
.map_or_else(|| self.type_error(), |&x| visitor.visit_i16(x))
|
|
|
|
}
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_i32<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-05 05:35:50 +02:00
|
|
|
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>()
|
2020-07-05 05:35:50 +02:00
|
|
|
.map_or_else(|| self.type_error(), |&x| visitor.visit_i32(x))
|
|
|
|
}
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_i64<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-05 05:35:50 +02:00
|
|
|
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>()
|
2020-07-05 05:35:50 +02:00
|
|
|
.map_or_else(|| self.type_error(), |&x| visitor.visit_i64(x))
|
|
|
|
}
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_i128<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-24 19:08:59 +02:00
|
|
|
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>()
|
2020-07-24 19:08:59 +02:00
|
|
|
.map_or_else(|| self.type_error(), |&x| visitor.visit_i128(x))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_u8<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-05 05:35:50 +02:00
|
|
|
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>()
|
2020-07-05 05:35:50 +02:00
|
|
|
.map_or_else(|| self.type_error(), |&x| visitor.visit_u8(x))
|
|
|
|
}
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_u16<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-05 05:35:50 +02:00
|
|
|
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>()
|
2020-07-05 05:35:50 +02:00
|
|
|
.map_or_else(|| self.type_error(), |&x| visitor.visit_u16(x))
|
|
|
|
}
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_u32<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-05 05:35:50 +02:00
|
|
|
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>()
|
2020-07-05 05:35:50 +02:00
|
|
|
.map_or_else(|| self.type_error(), |&x| visitor.visit_u32(x))
|
|
|
|
}
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_u64<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-05 05:35:50 +02:00
|
|
|
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>()
|
2020-07-05 05:35:50 +02:00
|
|
|
.map_or_else(|| self.type_error(), |&x| visitor.visit_u64(x))
|
|
|
|
}
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_u128<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-24 19:08:59 +02:00
|
|
|
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>()
|
2020-07-24 19:08:59 +02:00
|
|
|
.map_or_else(|| self.type_error(), |&x| visitor.visit_u128(x))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_f32<V: Visitor<'de>>(self, _visitor: V) -> RhaiResultOf<V::Value> {
|
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
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_f64<V: Visitor<'de>>(self, _visitor: V) -> RhaiResultOf<V::Value> {
|
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
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_char<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_str<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
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()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_string<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_str(visitor)
|
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_bytes<V: Visitor<'de>>(self, _visitor: V) -> RhaiResultOf<V::Value> {
|
2021-12-02 09:01:37 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
return self
|
|
|
|
.value
|
2021-12-06 13:52:47 +01:00
|
|
|
.downcast_ref::<crate::Blob>()
|
2021-12-02 10:46:39 +01:00
|
|
|
.map_or_else(|| self.type_error(), |x| _visitor.visit_bytes(x));
|
|
|
|
|
2021-12-02 09:01:37 +01:00
|
|
|
#[cfg(feature = "no_index")]
|
|
|
|
return self.type_error();
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_byte_buf<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2021-12-02 07:10:53 +01:00
|
|
|
self.deserialize_bytes(visitor)
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_option<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2021-12-02 05:50:11 +01:00
|
|
|
if self.value.is::<()>() {
|
|
|
|
visitor.visit_none()
|
|
|
|
} else {
|
|
|
|
visitor.visit_some(self)
|
|
|
|
}
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_unit<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
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,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_unit(visitor)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_newtype_struct<V: Visitor<'de>>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
visitor: V,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
visitor.visit_newtype_struct(self)
|
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_seq<V: Visitor<'de>>(self, _visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2021-12-06 13:52:47 +01:00
|
|
|
return self.value.downcast_ref::<crate::Array>().map_or_else(
|
2020-07-04 16:53:00 +02:00
|
|
|
|| self.type_error(),
|
2021-12-02 07:10:53 +01:00
|
|
|
|arr| _visitor.visit_seq(IterateDynamicArray::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
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_tuple<V: Visitor<'de>>(self, _len: usize, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_seq(visitor)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_tuple_struct<V: Visitor<'de>>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
visitor: V,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_seq(visitor)
|
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_map<V: Visitor<'de>>(self, _visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2021-12-06 13:52:47 +01:00
|
|
|
return self.value.downcast_ref::<crate::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(
|
2022-08-05 17:45:40 +02:00
|
|
|
map.keys().map(crate::SmartString::as_str),
|
2021-03-29 12:46:32 +02:00
|
|
|
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,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_map(visitor)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_enum<V: Visitor<'de>>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variants: &'static [&'static str],
|
2020-07-05 05:43:48 +02:00
|
|
|
visitor: V,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<V::Value> {
|
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())
|
2020-07-05 05:43:48 +02:00
|
|
|
} else {
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-08-29 08:27:05 +02:00
|
|
|
return self.value.downcast_ref::<crate::Map>().map_or_else(
|
|
|
|
|| self.type_error(),
|
|
|
|
|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()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2020-07-05 05:43:48 +02:00
|
|
|
#[cfg(feature = "no_object")]
|
|
|
|
return self.type_error();
|
|
|
|
}
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_identifier<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_str(visitor)
|
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_ignored_any<V: Visitor<'de>>(self, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_any(visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 09:39:40 +02:00
|
|
|
/// `SeqAccess` implementation for arrays.
|
2021-12-02 10:46:39 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2021-12-02 07:10:53 +01:00
|
|
|
struct IterateDynamicArray<'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,
|
|
|
|
}
|
|
|
|
|
2021-12-02 10:46:39 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2021-12-02 07:10:53 +01:00
|
|
|
impl<'a, ITER: Iterator<Item = &'a Dynamic>> IterateDynamicArray<'a, ITER> {
|
2021-06-12 16:47:43 +02:00
|
|
|
#[must_use]
|
2022-08-29 08:27:05 +02:00
|
|
|
pub const fn new(iter: ITER) -> Self {
|
2020-07-03 11:19:55 +02:00
|
|
|
Self { iter }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 10:46:39 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
impl<'a: 'de, 'de, ITER: Iterator<Item = &'a Dynamic>> serde::de::SeqAccess<'de>
|
2021-12-02 07:10:53 +01:00
|
|
|
for IterateDynamicArray<'a, ITER>
|
|
|
|
{
|
2021-12-25 16:49:14 +01:00
|
|
|
type Error = RhaiError;
|
2020-07-03 11:19:55 +02:00
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn next_element_seed<T: serde::de::DeserializeSeed<'de>>(
|
2020-07-03 11:19:55 +02:00
|
|
|
&mut self,
|
|
|
|
seed: T,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<Option<T::Value>> {
|
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.
|
2021-12-02 10:46:39 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-08-24 12:27:58 +02:00
|
|
|
struct IterateMap<'a, K: Iterator<Item = &'a str>, V: Iterator<Item = &'a Dynamic>> {
|
2020-11-20 09:52:28 +01:00
|
|
|
// Iterator for a stream of [`Dynamic`][crate::Dynamic] keys.
|
2022-08-24 12:27:58 +02:00
|
|
|
keys: K,
|
2020-11-20 09:52:28 +01:00
|
|
|
// Iterator for a stream of [`Dynamic`][crate::Dynamic] values.
|
2022-08-24 12:27:58 +02:00
|
|
|
values: V,
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2020-07-26 09:53:22 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-08-24 12:27:58 +02:00
|
|
|
impl<'a, K: Iterator<Item = &'a str>, V: Iterator<Item = &'a Dynamic>> IterateMap<'a, K, V> {
|
2021-06-12 16:47:43 +02:00
|
|
|
#[must_use]
|
2022-08-29 08:27:05 +02:00
|
|
|
pub const fn new(keys: K, values: V) -> Self {
|
2020-07-03 11:19:55 +02:00
|
|
|
Self { keys, values }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 10:46:39 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-08-24 12:27:58 +02:00
|
|
|
impl<'a: 'de, 'de, K: Iterator<Item = &'a str>, V: Iterator<Item = &'a Dynamic>>
|
|
|
|
serde::de::MapAccess<'de> for IterateMap<'a, K, V>
|
2020-07-03 11:19:55 +02:00
|
|
|
{
|
2021-12-25 16:49:14 +01:00
|
|
|
type Error = RhaiError;
|
2020-07-03 11:19:55 +02:00
|
|
|
|
2022-08-24 12:27:58 +02:00
|
|
|
fn next_key_seed<S: serde::de::DeserializeSeed<'de>>(
|
2020-07-03 11:19:55 +02:00
|
|
|
&mut self,
|
2022-08-24 12:27:58 +02:00
|
|
|
seed: S,
|
|
|
|
) -> RhaiResultOf<Option<S::Value>> {
|
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-12-02 10:46:39 +01:00
|
|
|
.deserialize(&mut super::str::StringSliceDeserializer::from_str(item))
|
2020-07-03 11:19:55 +02:00
|
|
|
.map(Some),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 12:27:58 +02:00
|
|
|
fn next_value_seed<S: serde::de::DeserializeSeed<'de>>(
|
2020-07-03 11:19:55 +02:00
|
|
|
&mut self,
|
2022-08-24 12:27:58 +02:00
|
|
|
seed: S,
|
|
|
|
) -> RhaiResultOf<S::Value> {
|
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(
|
2022-01-06 04:07:52 +01:00
|
|
|
self.values.next().unwrap(),
|
2020-07-03 11:19:55 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2020-07-05 05:43:48 +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> {
|
2021-12-25 16:49:14 +01:00
|
|
|
type Error = RhaiError;
|
2020-07-05 05:43:48 +02:00
|
|
|
type Variant = Self;
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn variant_seed<V: serde::de::DeserializeSeed<'de>>(
|
2020-11-03 14:50:14 +01:00
|
|
|
self,
|
|
|
|
seed: V,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<(V::Value, Self::Variant)> {
|
2020-07-05 05:43:48 +02:00
|
|
|
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> {
|
2021-12-25 16:49:14 +01:00
|
|
|
type Error = RhaiError;
|
2020-07-05 05:43:48 +02:00
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn unit_variant(mut self) -> RhaiResultOf<()> {
|
2020-07-05 05:43:48 +02:00
|
|
|
Deserialize::deserialize(&mut self.content)
|
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn newtype_variant_seed<T: serde::de::DeserializeSeed<'de>>(
|
2020-11-03 14:50:14 +01:00
|
|
|
mut self,
|
|
|
|
seed: T,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<T::Value> {
|
2020-07-05 05:43:48 +02:00
|
|
|
seed.deserialize(&mut self.content)
|
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
fn tuple_variant<V: Visitor<'de>>(mut self, len: usize, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-05 05:43:48 +02:00
|
|
|
self.content.deserialize_tuple(len, visitor)
|
|
|
|
}
|
|
|
|
|
2020-11-03 14:50:14 +01:00
|
|
|
fn struct_variant<V: Visitor<'de>>(
|
2020-07-05 05:43:48 +02:00
|
|
|
mut self,
|
|
|
|
fields: &'static [&'static str],
|
|
|
|
visitor: V,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<V::Value> {
|
2020-07-05 05:43:48 +02:00
|
|
|
self.content.deserialize_struct("", fields, visitor)
|
|
|
|
}
|
|
|
|
}
|