use crate::result::EvalAltResult; use crate::token::Position; use crate::utils::ImmutableString; use serde::de::{Deserializer, Visitor}; use crate::stdlib::any::type_name; pub struct ImmutableStringDeserializer<'a> { value: &'a ImmutableString, } impl<'a> ImmutableStringDeserializer<'a> { pub fn from_str(value: &'a ImmutableString) -> Self { Self { value } } pub fn type_error(&self) -> Result> { self.type_error_str(type_name::()) } pub fn type_error_str(&self, name: &str) -> Result> { Err(Box::new(EvalAltResult::ErrorMismatchOutputType( name.into(), "string".into(), Position::none(), ))) } } 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_str("f32") } fn deserialize_f64>(self, _: V) -> Result> { self.type_error_str("f64") } fn deserialize_char>(self, _: V) -> Result> { self.type_error::() } fn deserialize_str>(self, v: V) -> Result> { 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_str("bytes array") } fn deserialize_byte_buf>(self, _: V) -> Result> { self.type_error_str("bytes array") } fn deserialize_option>(self, _: V) -> Result> { self.type_error_str("option") } 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_str("array") } 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_str("map") } 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_str("enum") } fn deserialize_identifier>(self, v: V) -> Result> { self.deserialize_str(v) } fn deserialize_ignored_any>( self, v: V, ) -> Result> { self.deserialize_any(v) } }