//! Implement deserialization support of [`ImmutableString`][crate::ImmutableString] for [`serde`]. use crate::{Position, RhaiError, RhaiResultOf, ERR}; use serde::de::{Deserializer, Visitor}; use std::any::type_name; #[cfg(feature = "no_std")] use std::prelude::v1::*; /// Deserializer for `ImmutableString`. pub struct StringSliceDeserializer<'a> { value: &'a str, } impl<'a> StringSliceDeserializer<'a> { /// Create an `ImmutableStringDeserializer` from an `&str` reference. #[must_use] pub const fn from_str(value: &'a str) -> Self { Self { value } } /// Shortcut for a type conversion error. fn type_error(&self) -> RhaiResultOf { Err( ERR::ErrorMismatchOutputType(type_name::().into(), "string".into(), Position::NONE) .into(), ) } } impl<'de> Deserializer<'de> for &mut StringSliceDeserializer<'de> { type Error = RhaiError; fn deserialize_any>(self, v: V) -> RhaiResultOf { self.deserialize_str(v) } fn deserialize_bool>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_i8>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_i16>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_i32>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_i64>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_u8>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_u16>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_u32>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_u64>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_f32>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_f64>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_char>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_str>(self, v: V) -> RhaiResultOf { // Only allow deserialization into a string. v.visit_borrowed_str(self.value) } fn deserialize_string>(self, visitor: V) -> RhaiResultOf { self.deserialize_str(visitor) } fn deserialize_bytes>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_byte_buf>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_option>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_unit>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_unit_struct>( self, _name: &'static str, v: V, ) -> RhaiResultOf { self.deserialize_unit(v) } fn deserialize_newtype_struct>( self, _name: &'static str, v: V, ) -> RhaiResultOf { v.visit_newtype_struct(self) } fn deserialize_seq>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_tuple>(self, _len: usize, v: V) -> RhaiResultOf { self.deserialize_seq(v) } fn deserialize_tuple_struct>( self, _name: &'static str, _len: usize, v: V, ) -> RhaiResultOf { self.deserialize_seq(v) } fn deserialize_map>(self, _: V) -> RhaiResultOf { self.type_error() } fn deserialize_struct>( self, _name: &'static str, _fields: &'static [&'static str], v: V, ) -> RhaiResultOf { self.deserialize_map(v) } fn deserialize_enum>( self, _name: &'static str, _variants: &'static [&'static str], _: V, ) -> RhaiResultOf { self.type_error() } fn deserialize_identifier>(self, v: V) -> RhaiResultOf { self.deserialize_str(v) } fn deserialize_ignored_any>(self, v: V) -> RhaiResultOf { self.deserialize_any(v) } }