Ensure rhai::INT can be deserialized into any integer types

This commit is contained in:
Alvin Wong 2020-07-05 11:35:50 +08:00
parent 8bc1b25edd
commit 8f53ce50d4
2 changed files with 120 additions and 24 deletions

View File

@ -49,6 +49,20 @@ impl<'de> DynamicDeserializer<'de> {
Position::none(),
)))
}
fn deserialize_int<V: Visitor<'de>>(
&mut self,
v: crate::INT,
visitor: V,
) -> Result<V::Value, Box<EvalAltResult>> {
#[cfg(not(feature = "only_i32"))]
{
visitor.visit_i64(v)
}
#[cfg(feature = "only_i32")]
{
visitor.visit_i32(v)
}
}
}
/// Deserialize a `Dynamic` value into a Rust type that implements `serde::Deserialize`.
@ -159,52 +173,88 @@ impl<'de> Deserializer<'de> for &mut DynamicDeserializer<'de> {
}
fn deserialize_i8<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
.downcast_ref::<i8>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_i8(x))
}
}
fn deserialize_i16<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
.downcast_ref::<i16>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_i16(x))
}
}
fn deserialize_i32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else if cfg!(feature = "only_i32") {
self.type_error()
} else {
self.value
.downcast_ref::<i32>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_i32(x))
}
}
fn deserialize_i64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else if cfg!(not(feature = "only_i32")) {
self.type_error()
} else {
self.value
.downcast_ref::<i64>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_i64(x))
}
}
fn deserialize_u8<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
.downcast_ref::<u8>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_u8(x))
}
}
fn deserialize_u16<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
.downcast_ref::<u16>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_u16(x))
}
}
fn deserialize_u32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
.downcast_ref::<u32>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_u32(x))
}
}
fn deserialize_u64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
if let Ok(v) = self.value.as_int() {
self.deserialize_int(v, visitor)
} else {
self.value
.downcast_ref::<u64>()
.map_or_else(|| self.type_error(), |&x| visitor.visit_u64(x))
}
}
fn deserialize_f32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Box<EvalAltResult>> {
#[cfg(not(feature = "no_float"))]

View File

@ -33,6 +33,38 @@ fn test_serde_ser_primary_types() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_serde_ser_integer_types() -> Result<(), Box<EvalAltResult>> {
assert_eq!(to_dynamic(42_i8)?.type_name(), std::any::type_name::<INT>());
assert_eq!(
to_dynamic(42_i16)?.type_name(),
std::any::type_name::<INT>()
);
assert_eq!(
to_dynamic(42_i32)?.type_name(),
std::any::type_name::<INT>()
);
assert_eq!(
to_dynamic(42_i64)?.type_name(),
std::any::type_name::<INT>()
);
assert_eq!(to_dynamic(42_u8)?.type_name(), std::any::type_name::<INT>());
assert_eq!(
to_dynamic(42_u16)?.type_name(),
std::any::type_name::<INT>()
);
assert_eq!(
to_dynamic(42_u32)?.type_name(),
std::any::type_name::<INT>()
);
assert_eq!(
to_dynamic(42_u64)?.type_name(),
std::any::type_name::<INT>()
);
Ok(())
}
#[test]
#[cfg(not(feature = "no_index"))]
fn test_serde_ser_array() -> Result<(), Box<EvalAltResult>> {
@ -106,6 +138,20 @@ fn test_serde_de_primary_types() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_serde_de_integer_types() -> Result<(), Box<EvalAltResult>> {
assert_eq!(42_i8, from_dynamic(&Dynamic::from(42 as INT))?);
assert_eq!(42_i16, from_dynamic(&Dynamic::from(42 as INT))?);
assert_eq!(42_i32, from_dynamic(&Dynamic::from(42 as INT))?);
assert_eq!(42_i64, from_dynamic(&Dynamic::from(42 as INT))?);
assert_eq!(42_u8, from_dynamic(&Dynamic::from(42 as INT))?);
assert_eq!(42_u16, from_dynamic(&Dynamic::from(42 as INT))?);
assert_eq!(42_u32, from_dynamic(&Dynamic::from(42 as INT))?);
assert_eq!(42_u64, from_dynamic(&Dynamic::from(42 as INT))?);
Ok(())
}
#[test]
#[cfg(not(feature = "no_index"))]
fn test_serde_de_array() -> Result<(), Box<EvalAltResult>> {