//! Implement deserialization support of `ImmutableString` for [`serde`](https://crates.io/crates/serde). use crate::result::EvalAltResult; use crate::token::Position; use crate::utils::ImmutableString; use serde::de::{Deserializer, Visitor}; use crate::stdlib::{any::type_name, boxed::Box}; /// Deserializer for `ImmutableString`. pub struct ImmutableStringDeserializer<'a> { value: &'a ImmutableString, } impl<'a> ImmutableStringDeserializer<'a> { /// Create an `ImmutableStringDeserializer` from an `ImmutableString` reference. pub fn from_str(value: &'a ImmutableString) -> Self { Self { value } } /// Shortcut for a type conversion error. fn type_error(&self) -> Result> { EvalAltResult::ErrorMismatchOutputType( type_name::().into(), "string".into(), Position::none(), ) .into() } } impl<'de> Deserializer<'de> for &mut ImmutableStringDeserializer<'de> { type Error = Box; fn deserialize_any>(self, v: V) -> Result> { self.deserialize_str(v) } fn deserialize_bool>(self, _: V) -> Result> { self.type_error() } fn deserialize_i8>(self, _: V) -> Result> { self.type_error() } fn deserialize_i16>(self, _: V) -> Result> { self.type_error() } fn deserialize_i32>(self, _: V) -> Result> { self.type_error() } fn deserialize_i64>(self, _: V) -> Result> { self.type_error() } fn deserialize_u8>(self, _: V) -> Result> { self.type_error() } fn deserialize_u16>(self, _: V) -> Result> { self.type_error() } fn deserialize_u32>(self, _: V) -> Result> { self.type_error() } fn deserialize_u64>(self, _: V) -> Result> { self.type_error() } fn deserialize_f32>(self, _: V) -> Result> { self.type_error() } fn deserialize_f64>(self, _: V) -> Result> { self.type_error() } fn deserialize_char>(self, _: V) -> Result> { self.type_error() } fn deserialize_str>(self, v: V) -> Result> { // Only allow deserialization into a string. v.visit_borrowed_str(self.value.as_str()) } fn deserialize_string>( self, visitor: V, ) -> Result> { self.deserialize_str(visitor) } fn deserialize_bytes>(self, _: V) -> Result> { self.type_error() } fn deserialize_byte_buf>(self, _: V) -> Result> { self.type_error() } fn deserialize_option>(self, _: V) -> Result> { self.type_error() } fn deserialize_unit>(self, _: V) -> Result> { self.type_error() } fn deserialize_unit_struct>( self, _name: &'static str, v: V, ) -> Result> { self.deserialize_unit(v) } fn deserialize_newtype_struct>( self, _name: &'static str, v: V, ) -> Result> { v.visit_newtype_struct(self) } fn deserialize_seq>(self, _: V) -> Result> { self.type_error() } fn deserialize_tuple>( self, _len: usize, v: V, ) -> Result> { self.deserialize_seq(v) } fn deserialize_tuple_struct>( self, _name: &'static str, _len: usize, v: V, ) -> Result> { self.deserialize_seq(v) } fn deserialize_map>(self, _: V) -> Result> { self.type_error() } fn deserialize_struct>( self, _name: &'static str, _fields: &'static [&'static str], v: V, ) -> Result> { self.deserialize_map(v) } fn deserialize_enum>( self, _name: &'static str, _variants: &'static [&'static str], _: V, ) -> Result> { self.type_error() } fn deserialize_identifier>(self, v: V) -> Result> { self.deserialize_str(v) } fn deserialize_ignored_any>( self, v: V, ) -> Result> { self.deserialize_any(v) } }