rhai/src/serde/ser.rs

689 lines
20 KiB
Rust
Raw Normal View History

2020-11-20 09:52:28 +01:00
//! Implement serialization support of [`Dynamic`][crate::Dynamic] for [`serde`].
2020-07-04 09:39:40 +02:00
2021-12-27 05:27:31 +01:00
use crate::{Dynamic, Position, RhaiError, RhaiResult, RhaiResultOf, ERR};
2020-07-03 16:42:56 +02:00
use serde::ser::{
2020-07-26 09:53:22 +02:00
Error, SerializeMap, SerializeSeq, SerializeStruct, SerializeTuple, SerializeTupleStruct,
2020-07-03 16:42:56 +02:00
};
use serde::{Serialize, Serializer};
2021-04-17 09:15:54 +02:00
use std::fmt;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
2020-07-03 16:42:56 +02:00
2020-11-20 09:52:28 +01:00
/// Serializer for [`Dynamic`][crate::Dynamic] which is kept as a reference.
2021-02-19 08:17:14 +01:00
struct DynamicSerializer {
2020-07-04 09:39:40 +02:00
/// Buffer to hold a temporary key.
2020-07-26 09:53:22 +02:00
_key: Dynamic,
2020-07-04 09:39:40 +02:00
/// Buffer to hold a temporary value.
2020-07-26 09:53:22 +02:00
_value: Dynamic,
2020-07-03 16:42:56 +02:00
}
impl DynamicSerializer {
2020-11-20 09:52:28 +01:00
/// Create a [`DynamicSerializer`] from a [`Dynamic`][crate::Dynamic] value.
2021-06-12 16:47:43 +02:00
#[must_use]
2022-08-29 08:27:05 +02:00
pub const fn new(_value: Dynamic) -> Self {
2020-07-03 16:42:56 +02:00
Self {
_key: Dynamic::UNIT,
2020-07-26 09:53:22 +02:00
_value,
2020-07-03 16:42:56 +02:00
}
}
}
2020-11-20 09:52:28 +01:00
/// Serialize a Rust type that implements [`serde::Serialize`] into a [`Dynamic`][crate::Dynamic].
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"))]
/// # #[cfg(not(feature = "no_float"))]
/// # {
/// use rhai::{Dynamic, Array, Map, INT};
2020-10-10 07:43:14 +02:00
/// use rhai::serde::to_dynamic;
2020-07-04 09:39:40 +02:00
/// use serde::Serialize;
///
/// #[derive(Debug, serde::Serialize, PartialEq)]
/// struct Point {
/// x: f64,
/// y: f64
/// }
///
/// #[derive(Debug, serde::Serialize, PartialEq)]
/// struct MyStruct {
/// a: i64,
/// b: Vec<String>,
/// c: bool,
/// d: Point
/// }
///
/// let x = MyStruct {
/// a: 42,
/// b: vec![ "hello".into(), "world".into() ],
/// c: true,
/// d: Point { x: 123.456, y: 999.0 }
/// };
///
/// // Convert the 'MyStruct' into a 'Dynamic'
/// let value = to_dynamic(x)?;
///
/// assert!(value.is::<Map>());
///
/// let map = value.cast::<Map>();
2020-08-06 16:47:10 +02:00
/// let point = map["d"].read_lock::<Map>().unwrap();
/// assert_eq!(*point["x"].read_lock::<f64>().unwrap(), 123.456);
/// assert_eq!(*point["y"].read_lock::<f64>().unwrap(), 999.0);
2020-07-04 09:39:40 +02:00
/// # }
/// # Ok(())
/// # }
/// ```
2021-03-02 08:02:28 +01:00
pub fn to_dynamic<T: Serialize>(value: T) -> RhaiResult {
let mut s = DynamicSerializer::new(Dynamic::UNIT);
2020-07-03 16:42:56 +02:00
value.serialize(&mut s)
}
2021-12-25 16:49:14 +01:00
impl Error for RhaiError {
2020-07-03 16:42:56 +02:00
fn custom<T: fmt::Display>(err: T) -> Self {
2021-12-27 05:27:31 +01:00
ERR::ErrorRuntime(err.to_string().into(), Position::NONE).into()
2020-07-03 16:42:56 +02:00
}
}
impl Serializer for &mut DynamicSerializer {
type Ok = Dynamic;
2021-12-25 16:49:14 +01:00
type Error = RhaiError;
2020-07-03 16:42:56 +02:00
type SerializeSeq = DynamicSerializer;
type SerializeTuple = DynamicSerializer;
type SerializeTupleStruct = DynamicSerializer;
2022-01-04 15:16:20 +01:00
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_index"))]
type SerializeTupleVariant = TupleVariantSerializer;
#[cfg(any(feature = "no_object", feature = "no_index"))]
2021-12-25 16:49:14 +01:00
type SerializeTupleVariant = serde::ser::Impossible<Dynamic, RhaiError>;
2020-07-03 16:42:56 +02:00
type SerializeMap = DynamicSerializer;
type SerializeStruct = DynamicSerializer;
#[cfg(not(feature = "no_object"))]
type SerializeStructVariant = StructVariantSerializer;
#[cfg(feature = "no_object")]
2021-12-25 16:49:14 +01:00
type SerializeStructVariant = serde::ser::Impossible<Dynamic, RhaiError>;
2020-07-03 16:42:56 +02:00
2021-12-25 16:49:14 +01:00
fn serialize_bool(self, v: bool) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
Ok(v.into())
}
2021-12-25 16:49:14 +01:00
fn serialize_i8(self, v: i8) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "only_i32"))]
return self.serialize_i64(i64::from(v));
#[cfg(feature = "only_i32")]
return self.serialize_i32(i32::from(v));
}
2021-12-25 16:49:14 +01:00
fn serialize_i16(self, v: i16) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "only_i32"))]
return self.serialize_i64(i64::from(v));
#[cfg(feature = "only_i32")]
return self.serialize_i32(i32::from(v));
}
2021-12-25 16:49:14 +01:00
fn serialize_i32(self, v: i32) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "only_i32"))]
return self.serialize_i64(i64::from(v));
#[cfg(feature = "only_i32")]
return Ok(v.into());
}
2021-12-25 16:49:14 +01:00
fn serialize_i64(self, v: i64) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "only_i32"))]
2021-07-26 16:22:27 +02:00
{
Ok(v.into())
}
2020-07-03 16:42:56 +02:00
#[cfg(feature = "only_i32")]
if v > i32::MAX as i64 {
2021-07-26 16:22:27 +02:00
Ok(Dynamic::from(v))
2020-07-03 16:42:56 +02:00
} else {
2021-07-26 16:22:27 +02:00
self.serialize_i32(v as i32)
2020-07-03 16:42:56 +02:00
}
}
2021-12-25 16:49:14 +01:00
fn serialize_i128(self, v: i128) -> RhaiResultOf<Self::Ok> {
#[cfg(not(feature = "only_i32"))]
if v > i64::MAX as i128 {
2021-07-26 16:22:27 +02:00
Ok(Dynamic::from(v))
} else {
2021-07-26 16:22:27 +02:00
self.serialize_i64(v as i64)
}
#[cfg(feature = "only_i32")]
if v > i32::MAX as i128 {
2021-07-26 16:22:27 +02:00
Ok(Dynamic::from(v))
} else {
2021-07-26 16:22:27 +02:00
self.serialize_i32(v as i32)
}
}
2021-12-25 16:49:14 +01:00
fn serialize_u8(self, v: u8) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "only_i32"))]
return self.serialize_i64(i64::from(v));
#[cfg(feature = "only_i32")]
return self.serialize_i32(i32::from(v));
}
2021-12-25 16:49:14 +01:00
fn serialize_u16(self, v: u16) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "only_i32"))]
return self.serialize_i64(i64::from(v));
#[cfg(feature = "only_i32")]
return self.serialize_i32(i32::from(v));
}
2021-12-25 16:49:14 +01:00
fn serialize_u32(self, v: u32) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "only_i32"))]
2021-07-26 16:22:27 +02:00
{
self.serialize_i64(i64::from(v))
}
2020-07-03 16:42:56 +02:00
#[cfg(feature = "only_i32")]
2020-07-06 09:30:33 +02:00
if v > i32::MAX as u32 {
2021-07-26 16:22:27 +02:00
Ok(Dynamic::from(v))
2020-07-03 16:42:56 +02:00
} else {
2021-07-26 16:22:27 +02:00
self.serialize_i32(v as i32)
2020-07-03 16:42:56 +02:00
}
}
2021-12-25 16:49:14 +01:00
fn serialize_u64(self, v: u64) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "only_i32"))]
if v > i64::MAX as u64 {
2021-07-26 16:22:27 +02:00
Ok(Dynamic::from(v))
2020-07-03 16:42:56 +02:00
} else {
2021-07-26 16:22:27 +02:00
self.serialize_i64(v as i64)
2020-07-03 16:42:56 +02:00
}
#[cfg(feature = "only_i32")]
if v > i32::MAX as u64 {
2021-07-26 16:22:27 +02:00
Ok(Dynamic::from(v))
2020-07-03 16:42:56 +02:00
} else {
2021-07-26 16:22:27 +02:00
self.serialize_i32(v as i32)
2020-07-03 16:42:56 +02:00
}
}
2021-12-25 16:49:14 +01:00
fn serialize_u128(self, v: u128) -> RhaiResultOf<Self::Ok> {
#[cfg(not(feature = "only_i32"))]
if v > i64::MAX as u128 {
2021-07-26 16:22:27 +02:00
Ok(Dynamic::from(v))
} else {
2021-07-26 16:22:27 +02:00
self.serialize_i64(v as i64)
}
#[cfg(feature = "only_i32")]
if v > i32::MAX as u128 {
2021-07-26 16:22:27 +02:00
Ok(Dynamic::from(v))
} else {
2021-07-26 16:22:27 +02:00
self.serialize_i32(v as i32)
}
}
2021-12-25 16:49:14 +01:00
fn serialize_f32(self, v: f32) -> RhaiResultOf<Self::Ok> {
2021-02-13 13:57:56 +01:00
#[cfg(any(not(feature = "no_float"), not(feature = "decimal")))]
return Ok(Dynamic::from(v));
#[cfg(feature = "no_float")]
#[cfg(feature = "decimal")]
{
use rust_decimal::Decimal;
2021-04-17 09:15:54 +02:00
use std::convert::TryFrom;
2021-02-13 13:57:56 +01:00
Decimal::try_from(v)
.map(|v| v.into())
.map_err(Error::custom)
}
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn serialize_f64(self, v: f64) -> RhaiResultOf<Self::Ok> {
2021-02-13 13:57:56 +01:00
#[cfg(any(not(feature = "no_float"), not(feature = "decimal")))]
return Ok(Dynamic::from(v));
#[cfg(feature = "no_float")]
#[cfg(feature = "decimal")]
{
use rust_decimal::Decimal;
2021-04-17 09:15:54 +02:00
use std::convert::TryFrom;
2021-02-13 13:57:56 +01:00
Decimal::try_from(v)
.map(|v| v.into())
.map_err(Error::custom)
}
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn serialize_char(self, v: char) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
Ok(v.into())
}
2021-12-25 16:49:14 +01:00
fn serialize_str(self, v: &str) -> RhaiResultOf<Self::Ok> {
Ok(v.into())
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn serialize_bytes(self, _v: &[u8]) -> RhaiResultOf<Self::Ok> {
2021-12-02 10:46:39 +01:00
#[cfg(not(feature = "no_index"))]
return Ok(Dynamic::from_blob(_v.to_vec()));
#[cfg(feature = "no_index")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
2021-12-02 10:46:39 +01:00
"".into(),
"BLOB's are not supported with 'no_index'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn serialize_none(self) -> RhaiResultOf<Self::Ok> {
2020-11-15 16:14:29 +01:00
Ok(Dynamic::UNIT)
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
value.serialize(&mut *self)
}
2021-12-25 16:49:14 +01:00
fn serialize_unit(self) -> RhaiResultOf<Self::Ok> {
2020-11-15 16:14:29 +01:00
Ok(Dynamic::UNIT)
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn serialize_unit_struct(self, _name: &'static str) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
self.serialize_unit()
}
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
self.serialize_str(variant)
}
fn serialize_newtype_struct<T: ?Sized + Serialize>(
self,
_name: &'static str,
value: &T,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
value.serialize(&mut *self)
}
fn serialize_newtype_variant<T: ?Sized + Serialize>(
self,
_name: &'static str,
_variant_index: u32,
2020-07-26 09:53:22 +02:00
_variant: &'static str,
_value: &T,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<Self::Ok> {
#[cfg(not(feature = "no_object"))]
{
2020-07-26 09:53:22 +02:00
let content = to_dynamic(_value)?;
make_variant(_variant, content)
}
#[cfg(feature = "no_object")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"object maps are not supported with 'no_object'".into(),
2020-11-20 09:52:28 +01:00
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn serialize_seq(self, _len: Option<usize>) -> RhaiResultOf<Self::SerializeSeq> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_index"))]
2021-12-06 13:52:47 +01:00
return Ok(DynamicSerializer::new(crate::Array::new().into()));
2020-07-03 16:42:56 +02:00
#[cfg(feature = "no_index")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"arrays are not supported with 'no_index'".into(),
2020-11-20 09:52:28 +01:00
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn serialize_tuple(self, len: usize) -> RhaiResultOf<Self::SerializeTuple> {
2020-07-03 16:42:56 +02:00
self.serialize_seq(Some(len))
}
fn serialize_tuple_struct(
self,
_name: &'static str,
len: usize,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<Self::SerializeTupleStruct> {
2020-07-03 16:42:56 +02:00
self.serialize_seq(Some(len))
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
2020-07-26 09:53:22 +02:00
_variant: &'static str,
_len: usize,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<Self::SerializeTupleVariant> {
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_index"))]
return Ok(TupleVariantSerializer {
2020-07-26 09:53:22 +02:00
variant: _variant,
2021-12-06 13:52:47 +01:00
array: crate::Array::with_capacity(_len),
});
#[cfg(any(feature = "no_object", feature = "no_index"))]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"tuples are not supported with 'no_index' or 'no_object'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn serialize_map(self, _len: Option<usize>) -> RhaiResultOf<Self::SerializeMap> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_object"))]
2021-12-06 13:52:47 +01:00
return Ok(DynamicSerializer::new(crate::Map::new().into()));
2020-07-03 16:42:56 +02:00
#[cfg(feature = "no_object")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"object maps are not supported with 'no_object'".into(),
2020-11-20 09:52:28 +01:00
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
fn serialize_struct(
self,
_name: &'static str,
len: usize,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<Self::SerializeStruct> {
2020-07-03 16:42:56 +02:00
self.serialize_map(Some(len))
}
fn serialize_struct_variant(
self,
_name: &'static str,
_variant_index: u32,
2020-07-26 09:53:22 +02:00
_variant: &'static str,
_len: usize,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<Self::SerializeStructVariant> {
#[cfg(not(feature = "no_object"))]
return Ok(StructVariantSerializer {
2020-07-26 09:53:22 +02:00
variant: _variant,
2021-12-06 13:52:47 +01:00
map: crate::Map::new(),
});
#[cfg(feature = "no_object")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"object maps are not supported with 'no_object'".into(),
2020-11-20 09:52:28 +01:00
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
}
impl SerializeSeq for DynamicSerializer {
type Ok = Dynamic;
2021-12-25 16:49:14 +01:00
type Error = RhaiError;
2020-07-03 16:42:56 +02:00
2021-12-25 16:49:14 +01:00
fn serialize_element<T: ?Sized + Serialize>(&mut self, _value: &T) -> RhaiResultOf<()> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_index"))]
{
2022-04-11 10:29:16 +02:00
let value = _value.serialize(&mut *self)?;
2021-12-06 13:52:47 +01:00
let arr = self._value.downcast_mut::<crate::Array>().unwrap();
2022-04-11 10:29:16 +02:00
arr.push(value);
2020-07-04 09:39:40 +02:00
Ok(())
2020-07-03 16:42:56 +02:00
}
#[cfg(feature = "no_index")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"arrays are not supported with 'no_index'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
// Close the sequence.
2021-12-25 16:49:14 +01:00
fn end(self) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_index"))]
2020-07-26 09:53:22 +02:00
return Ok(self._value);
2020-07-03 16:42:56 +02:00
#[cfg(feature = "no_index")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"arrays are not supported with 'no_index'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
}
impl SerializeTuple for DynamicSerializer {
type Ok = Dynamic;
2021-12-25 16:49:14 +01:00
type Error = RhaiError;
2020-07-03 16:42:56 +02:00
2021-12-25 16:49:14 +01:00
fn serialize_element<T: ?Sized + Serialize>(&mut self, _value: &T) -> RhaiResultOf<()> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_index"))]
{
2022-04-11 10:29:16 +02:00
let value = _value.serialize(&mut *self)?;
2021-12-06 13:52:47 +01:00
let arr = self._value.downcast_mut::<crate::Array>().unwrap();
2022-04-11 10:29:16 +02:00
arr.push(value);
2020-07-04 09:39:40 +02:00
Ok(())
2020-07-03 16:42:56 +02:00
}
#[cfg(feature = "no_index")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"tuples are not supported with 'no_index'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn end(self) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_index"))]
2020-07-26 09:53:22 +02:00
return Ok(self._value);
2020-07-03 16:42:56 +02:00
#[cfg(feature = "no_index")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"tuples are not supported with 'no_index'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
}
impl SerializeTupleStruct for DynamicSerializer {
type Ok = Dynamic;
2021-12-25 16:49:14 +01:00
type Error = RhaiError;
2020-07-03 16:42:56 +02:00
2021-12-25 16:49:14 +01:00
fn serialize_field<T: ?Sized + Serialize>(&mut self, _value: &T) -> RhaiResultOf<()> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_index"))]
{
2022-04-11 10:29:16 +02:00
let value = _value.serialize(&mut *self)?;
2021-12-06 13:52:47 +01:00
let arr = self._value.downcast_mut::<crate::Array>().unwrap();
2022-04-11 10:29:16 +02:00
arr.push(value);
2020-07-04 09:39:40 +02:00
Ok(())
2020-07-03 16:42:56 +02:00
}
#[cfg(feature = "no_index")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"tuples are not supported with 'no_index'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn end(self) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_index"))]
2020-07-26 09:53:22 +02:00
return Ok(self._value);
2020-07-03 16:42:56 +02:00
#[cfg(feature = "no_index")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"tuples are not supported with 'no_index'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
}
impl SerializeMap for DynamicSerializer {
type Ok = Dynamic;
2021-12-25 16:49:14 +01:00
type Error = RhaiError;
2020-07-03 16:42:56 +02:00
2021-12-25 16:49:14 +01:00
fn serialize_key<T: ?Sized + Serialize>(&mut self, _key: &T) -> RhaiResultOf<()> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_object"))]
{
2020-07-26 09:53:22 +02:00
self._key = _key.serialize(&mut *self)?;
2020-07-03 16:42:56 +02:00
Ok(())
}
#[cfg(feature = "no_object")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"object maps are not supported with 'no_object'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn serialize_value<T: ?Sized + Serialize>(&mut self, _value: &T) -> RhaiResultOf<()> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_object"))]
{
2021-04-17 09:15:54 +02:00
let key = std::mem::take(&mut self._key)
.into_immutable_string()
2020-07-04 09:39:40 +02:00
.map_err(|typ| {
2021-12-27 05:27:31 +01:00
ERR::ErrorMismatchDataType("string".into(), typ.into(), Position::NONE)
2020-07-04 09:39:40 +02:00
})?;
2022-04-11 10:29:16 +02:00
let value = _value.serialize(&mut *self)?;
2021-12-06 13:52:47 +01:00
let map = self._value.downcast_mut::<crate::Map>().unwrap();
2022-04-11 10:29:16 +02:00
map.insert(key.into(), value);
2020-07-04 09:39:40 +02:00
Ok(())
2020-07-03 16:42:56 +02:00
}
#[cfg(feature = "no_object")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"object maps are not supported with 'no_object'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
fn serialize_entry<K: ?Sized + Serialize, T: ?Sized + Serialize>(
&mut self,
2020-07-26 09:53:22 +02:00
_key: &K,
_value: &T,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<()> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_object"))]
{
2022-04-11 10:29:16 +02:00
let key: Dynamic = _key.serialize(&mut *self)?;
let key = key.into_immutable_string().map_err(|typ| {
2021-12-27 05:27:31 +01:00
ERR::ErrorMismatchDataType("string".into(), typ.into(), Position::NONE)
2020-07-04 09:39:40 +02:00
})?;
2022-04-11 10:29:16 +02:00
let value = _value.serialize(&mut *self)?;
2021-12-06 13:52:47 +01:00
let map = self._value.downcast_mut::<crate::Map>().unwrap();
2022-04-11 10:29:16 +02:00
map.insert(key.into(), value);
2020-07-04 09:39:40 +02:00
Ok(())
2020-07-03 16:42:56 +02:00
}
#[cfg(feature = "no_object")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"object maps are not supported with 'no_object'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn end(self) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_object"))]
2020-07-26 09:53:22 +02:00
return Ok(self._value);
2020-07-03 16:42:56 +02:00
#[cfg(feature = "no_object")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"object maps are not supported with 'no_object'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
}
impl SerializeStruct for DynamicSerializer {
type Ok = Dynamic;
2021-12-25 16:49:14 +01:00
type Error = RhaiError;
2020-07-03 16:42:56 +02:00
fn serialize_field<T: ?Sized + Serialize>(
&mut self,
2020-07-26 09:53:22 +02:00
_key: &'static str,
_value: &T,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<()> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_object"))]
{
2022-04-11 10:29:16 +02:00
let value = _value.serialize(&mut *self)?;
2021-12-06 13:52:47 +01:00
let map = self._value.downcast_mut::<crate::Map>().unwrap();
2022-04-11 10:29:16 +02:00
map.insert(_key.into(), value);
2020-07-04 09:39:40 +02:00
Ok(())
2020-07-03 16:42:56 +02:00
}
#[cfg(feature = "no_object")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"object maps are not supported with 'no_object'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn end(self) -> RhaiResultOf<Self::Ok> {
2020-07-03 16:42:56 +02:00
#[cfg(not(feature = "no_object"))]
2020-07-26 09:53:22 +02:00
return Ok(self._value);
2020-07-03 16:42:56 +02:00
#[cfg(feature = "no_object")]
2021-12-27 05:27:31 +01:00
return Err(ERR::ErrorMismatchDataType(
"".into(),
"object maps are not supported with 'no_object'".into(),
Position::NONE,
)
.into());
2020-07-03 16:42:56 +02:00
}
}
2022-01-04 15:16:20 +01:00
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_index"))]
2021-02-19 08:17:14 +01:00
struct TupleVariantSerializer {
variant: &'static str,
2021-12-06 13:52:47 +01:00
array: crate::Array,
}
2022-01-04 15:16:20 +01:00
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_index"))]
2020-11-16 09:28:04 +01:00
impl serde::ser::SerializeTupleVariant for TupleVariantSerializer {
type Ok = Dynamic;
2021-12-25 16:49:14 +01:00
type Error = RhaiError;
2021-12-25 16:49:14 +01:00
fn serialize_field<T: ?Sized + Serialize>(&mut self, value: &T) -> RhaiResultOf<()> {
let value = to_dynamic(value)?;
self.array.push(value);
Ok(())
}
2021-12-25 16:49:14 +01:00
fn end(self) -> RhaiResultOf<Self::Ok> {
make_variant(self.variant, self.array.into())
}
}
#[cfg(not(feature = "no_object"))]
2021-02-19 08:17:14 +01:00
struct StructVariantSerializer {
variant: &'static str,
2021-12-06 13:52:47 +01:00
map: crate::Map,
}
#[cfg(not(feature = "no_object"))]
2020-11-16 09:28:04 +01:00
impl serde::ser::SerializeStructVariant for StructVariantSerializer {
2020-07-03 16:42:56 +02:00
type Ok = Dynamic;
2021-12-25 16:49:14 +01:00
type Error = RhaiError;
2020-07-03 16:42:56 +02:00
fn serialize_field<T: ?Sized + Serialize>(
&mut self,
key: &'static str,
value: &T,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<()> {
let value = to_dynamic(value)?;
self.map.insert(key.into(), value);
Ok(())
2020-07-03 16:42:56 +02:00
}
2021-12-25 16:49:14 +01:00
fn end(self) -> RhaiResultOf<Self::Ok> {
make_variant(self.variant, self.map.into())
2020-07-03 16:42:56 +02:00
}
}
#[cfg(not(feature = "no_object"))]
2021-03-02 08:02:28 +01:00
fn make_variant(variant: &'static str, value: Dynamic) -> RhaiResult {
2021-12-06 13:52:47 +01:00
let mut map = crate::Map::new();
map.insert(variant.into(), value);
Ok(map.into())
}