2020-11-20 09:52:28 +01:00
|
|
|
//! Implement deserialization support of [`ImmutableString`][crate::ImmutableString] for [`serde`].
|
2020-07-04 09:39:40 +02:00
|
|
|
|
2021-12-27 05:27:31 +01:00
|
|
|
use crate::{Position, RhaiError, RhaiResultOf, ERR};
|
2020-11-16 16:10:14 +01:00
|
|
|
use serde::de::{Deserializer, Visitor};
|
2021-04-17 09:15:54 +02:00
|
|
|
use std::any::type_name;
|
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
2020-07-03 11:19:55 +02:00
|
|
|
|
2020-07-04 09:39:40 +02:00
|
|
|
/// Deserializer for `ImmutableString`.
|
2021-03-29 12:46:32 +02:00
|
|
|
pub struct StringSliceDeserializer<'a> {
|
|
|
|
value: &'a str,
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-29 12:46:32 +02:00
|
|
|
impl<'a> StringSliceDeserializer<'a> {
|
|
|
|
/// Create an `ImmutableStringDeserializer` from an `&str` reference.
|
2021-06-12 16:47:43 +02:00
|
|
|
#[must_use]
|
2022-08-29 08:27:05 +02:00
|
|
|
pub const fn from_str(value: &'a str) -> 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> {
|
2021-12-27 05:27:31 +01:00
|
|
|
Err(
|
|
|
|
ERR::ErrorMismatchOutputType(type_name::<T>().into(), "string".into(), Position::NONE)
|
|
|
|
.into(),
|
2020-11-20 09:52:28 +01:00
|
|
|
)
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 12:46:32 +02:00
|
|
|
impl<'de> Deserializer<'de> for &mut StringSliceDeserializer<'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, v: V) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_str(v)
|
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_bool<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
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, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_i16<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_i32<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_i64<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_u8<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_u16<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_u32<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_u64<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_f32<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_f64<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_char<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_str<V: Visitor<'de>>(self, v: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
// Only allow deserialization into a string.
|
2021-03-29 12:46:32 +02:00
|
|
|
v.visit_borrowed_str(self.value)
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
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, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
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, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_option<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_unit<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
self.type_error()
|
2020-07-03 11:19:55 +02:00
|
|
|
}
|
|
|
|
fn deserialize_unit_struct<V: Visitor<'de>>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
v: V,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_unit(v)
|
|
|
|
}
|
|
|
|
fn deserialize_newtype_struct<V: Visitor<'de>>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
v: V,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
v.visit_newtype_struct(self)
|
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_seq<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
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, v: V) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_seq(v)
|
|
|
|
}
|
|
|
|
fn deserialize_tuple_struct<V: Visitor<'de>>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_len: usize,
|
|
|
|
v: V,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_seq(v)
|
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_map<V: Visitor<'de>>(self, _: V) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
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],
|
|
|
|
v: V,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_map(v)
|
|
|
|
}
|
|
|
|
fn deserialize_enum<V: Visitor<'de>>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variants: &'static [&'static str],
|
|
|
|
_: V,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> RhaiResultOf<V::Value> {
|
2020-07-04 09:39:40 +02:00
|
|
|
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, v: V) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_str(v)
|
|
|
|
}
|
2021-12-25 16:49:14 +01:00
|
|
|
fn deserialize_ignored_any<V: Visitor<'de>>(self, v: V) -> RhaiResultOf<V::Value> {
|
2020-07-03 11:19:55 +02:00
|
|
|
self.deserialize_any(v)
|
|
|
|
}
|
|
|
|
}
|