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
|
|
|
|
2022-09-25 10:20:36 +02:00
|
|
|
/// Deserializer for [`Dynamic`][crate::Dynamic].
|
2022-09-26 12:14:45 +02:00
|
|
|
pub struct DynamicDeserializer<'de>(&'de Dynamic);
|
2022-09-25 10:20:36 +02:00
|
|
|
|
|
|
|
impl<'de> IntoDeserializer<'de, RhaiError> for &'de Dynamic {
|
|
|
|
type Deserializer = DynamicDeserializer<'de>;
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
fn into_deserializer(self) -> Self::Deserializer {
|
2022-09-26 12:14:45 +02:00
|
|
|
DynamicDeserializer(self)
|
2022-09-25 10:20:36 +02:00
|
|
|
}
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
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].
|
2022-09-25 10:20:36 +02:00
|
|
|
#[inline(always)]
|
2021-06-12 16:47:43 +02:00
|
|
|
#[must_use]
|
2022-09-25 10:20:36 +02:00
|
|
|
pub const fn new(value: &'de Dynamic) -> Self {
|
2022-09-26 12:14:45 +02:00
|
|
|
Self(value)
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2020-07-04 09:39:40 +02:00
|
|
|
/// Shortcut for a type conversion error.
|
2022-09-25 10:20:36 +02:00
|
|
|
#[cold]
|
|
|
|
#[inline(always)]
|
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.
|
2022-09-25 10:20:36 +02:00
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
2021-12-25 16:49:14 +01:00
|
|
|
fn type_error_str<T>(&self, error: &str) -> RhaiResultOf<T> {
|
2022-09-26 12:14:45 +02:00
|
|
|
Err(
|
|
|
|
ERR::ErrorMismatchOutputType(error.into(), self.0.type_name().into(), Position::NONE)
|
|
|
|
.into(),
|
2020-11-20 09:52:28 +01:00
|
|
|
)
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2022-09-25 10:20:36 +02:00
|
|
|
#[inline(always)]
|
|
|
|
fn deserialize_int<V: Visitor<'de>>(self, v: crate::INT, visitor: V) -> 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> {
|
2022-09-25 10:20:36 +02:00
|
|
|
T::deserialize(DynamicDeserializer::new(value))
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
impl Error for RhaiError {
|
2022-09-25 10:20:36 +02:00
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 10:20:36 +02:00
|
|
|
impl<'de> Deserializer<'de> for 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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
match self.0 .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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
visitor.visit_bool(self.0.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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
if let Ok(v) = self.0.as_int() {
|
2020-07-05 05:35:50 +02:00
|
|
|
self.deserialize_int(v, visitor)
|
|
|
|
} else {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
if let Ok(v) = self.0.as_int() {
|
2020-07-05 05:35:50 +02:00
|
|
|
self.deserialize_int(v, visitor)
|
|
|
|
} else {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
if let Ok(v) = self.0.as_int() {
|
2020-07-05 05:35:50 +02:00
|
|
|
self.deserialize_int(v, visitor)
|
|
|
|
} else if cfg!(feature = "only_i32") {
|
|
|
|
self.type_error()
|
|
|
|
} else {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
if let Ok(v) = self.0.as_int() {
|
2020-07-05 05:35:50 +02:00
|
|
|
self.deserialize_int(v, visitor)
|
|
|
|
} else if cfg!(not(feature = "only_i32")) {
|
|
|
|
self.type_error()
|
|
|
|
} else {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
if let Ok(v) = self.0.as_int() {
|
2020-07-24 19:08:59 +02:00
|
|
|
self.deserialize_int(v, visitor)
|
|
|
|
} else if cfg!(not(feature = "only_i32")) {
|
|
|
|
self.type_error()
|
|
|
|
} else {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
if let Ok(v) = self.0.as_int() {
|
2020-07-05 05:35:50 +02:00
|
|
|
self.deserialize_int(v, visitor)
|
|
|
|
} else {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
if let Ok(v) = self.0.as_int() {
|
2020-07-05 05:35:50 +02:00
|
|
|
self.deserialize_int(v, visitor)
|
|
|
|
} else {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
if let Ok(v) = self.0.as_int() {
|
2020-07-05 05:35:50 +02:00
|
|
|
self.deserialize_int(v, visitor)
|
|
|
|
} else {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
if let Ok(v) = self.0.as_int() {
|
2020-07-05 05:35:50 +02:00
|
|
|
self.deserialize_int(v, visitor)
|
|
|
|
} else {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
if let Ok(v) = self.0.as_int() {
|
2020-07-24 19:08:59 +02:00
|
|
|
self.deserialize_int(v, visitor)
|
|
|
|
} else {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0
|
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
|
2022-09-26 12:14:45 +02:00
|
|
|
.0
|
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
|
2022-09-26 12:14:45 +02:00
|
|
|
.0
|
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
|
2022-09-26 12:14:45 +02:00
|
|
|
.0
|
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
|
2022-09-26 12:14:45 +02:00
|
|
|
.0
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0.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
|
2022-09-26 12:14:45 +02:00
|
|
|
.0
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
if self.0.is::<()>() {
|
2021-12-02 05:50:11 +01:00
|
|
|
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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
self.0
|
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"))]
|
2022-09-26 12:14:45 +02:00
|
|
|
return self.0.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"))]
|
2022-09-26 12:14:45 +02:00
|
|
|
return self.0.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> {
|
2022-09-26 12:14:45 +02:00
|
|
|
if let Some(s) = self.0.read_lock::<ImmutableString>() {
|
2021-03-05 16:41:20 +01:00
|
|
|
visitor.visit_enum(s.as_str().into_deserializer())
|
2020-07-05 05:43:48 +02:00
|
|
|
} else {
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-09-26 12:14:45 +02:00
|
|
|
return self.0.downcast_ref::<crate::Map>().map_or_else(
|
2022-08-29 08:27:05 +02:00
|
|
|
|| 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,
|
2022-09-25 10:20:36 +02:00
|
|
|
content: DynamicDeserializer::new(value),
|
2022-08-29 08:27:05 +02:00
|
|
|
})
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2022-09-25 10:20:36 +02:00
|
|
|
#[inline(always)]
|
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)
|
|
|
|
}
|
|
|
|
|
2022-09-25 10:20:36 +02:00
|
|
|
#[inline(always)]
|
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"))]
|
2022-09-25 10:20:36 +02:00
|
|
|
struct IterateDynamicArray<'de, ITER: Iterator<Item = &'de 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"))]
|
2022-09-25 10:20:36 +02:00
|
|
|
impl<'de, ITER: Iterator<Item = &'de Dynamic>> IterateDynamicArray<'de, ITER> {
|
|
|
|
#[inline(always)]
|
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"))]
|
2022-09-25 10:20:36 +02:00
|
|
|
impl<'de, ITER: Iterator<Item = &'de Dynamic>> serde::de::SeqAccess<'de>
|
|
|
|
for IterateDynamicArray<'de, ITER>
|
2021-12-02 07:10:53 +01:00
|
|
|
{
|
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() {
|
2022-09-25 10:20:36 +02:00
|
|
|
Some(item) => seed.deserialize(item.into_deserializer()).map(Some),
|
2020-07-03 11:19:55 +02:00
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-25 10:20:36 +02:00
|
|
|
struct IterateMap<'de, K: Iterator<Item = &'de str>, V: Iterator<Item = &'de 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-09-25 10:20:36 +02:00
|
|
|
impl<'de, K: Iterator<Item = &'de str>, V: Iterator<Item = &'de Dynamic>> IterateMap<'de, K, V> {
|
|
|
|
#[inline(always)]
|
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-09-25 10:20:36 +02:00
|
|
|
impl<'de, K: Iterator<Item = &'de str>, V: Iterator<Item = &'de Dynamic>> serde::de::MapAccess<'de>
|
|
|
|
for IterateMap<'de, 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.
|
2022-09-25 10:20:36 +02:00
|
|
|
match self.keys.next().map(<_>::into_deserializer) {
|
|
|
|
Some(d) => seed.deserialize(d).map(Some),
|
2020-07-03 11:19:55 +02:00
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2022-09-25 10:20:36 +02:00
|
|
|
seed.deserialize(self.values.next().unwrap().into_deserializer())
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-05 05:43:48 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-09-25 10:20:36 +02:00
|
|
|
struct EnumDeserializer<'de> {
|
|
|
|
tag: &'de str,
|
2020-07-05 05:43:48 +02:00
|
|
|
content: DynamicDeserializer<'de>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-09-25 10:20:36 +02:00
|
|
|
impl<'de> serde::de::EnumAccess<'de> for EnumDeserializer<'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"))]
|
2022-09-25 10:20:36 +02:00
|
|
|
impl<'de> serde::de::VariantAccess<'de> for EnumDeserializer<'de> {
|
2021-12-25 16:49:14 +01:00
|
|
|
type Error = RhaiError;
|
2020-07-05 05:43:48 +02:00
|
|
|
|
2022-09-25 10:20:36 +02:00
|
|
|
#[inline(always)]
|
|
|
|
fn unit_variant(self) -> RhaiResultOf<()> {
|
|
|
|
Deserialize::deserialize(self.content)
|
2020-07-05 05:43:48 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 10:20:36 +02:00
|
|
|
#[inline(always)]
|
2021-12-25 16:49:14 +01:00
|
|
|
fn newtype_variant_seed<T: serde::de::DeserializeSeed<'de>>(
|
2022-09-25 10:20:36 +02:00
|
|
|
self,
|
2020-11-03 14:50:14 +01:00
|
|
|
seed: T,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<T::Value> {
|
2022-09-25 10:20:36 +02:00
|
|
|
seed.deserialize(self.content)
|
2020-07-05 05:43:48 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 10:20:36 +02:00
|
|
|
#[inline(always)]
|
|
|
|
fn tuple_variant<V: Visitor<'de>>(self, len: usize, visitor: V) -> RhaiResultOf<V::Value> {
|
2020-07-05 05:43:48 +02:00
|
|
|
self.content.deserialize_tuple(len, visitor)
|
|
|
|
}
|
|
|
|
|
2022-09-25 10:20:36 +02:00
|
|
|
#[inline(always)]
|
2020-11-03 14:50:14 +01:00
|
|
|
fn struct_variant<V: Visitor<'de>>(
|
2022-09-25 10:20:36 +02:00
|
|
|
self,
|
2020-07-05 05:43:48 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|