Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Change Log
==========

## Version 0.10.0

* Update to neon 0.10.0

## Version 0.8.0

* Update to neon 0.8.0
Expand Down
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "neon-serde2"
version = "0.8.0"
version = "0.10.0"
authors = [
"Damir Jelić <poljar@termina.org.uk>",
"Gabriel Castro <dev@GabrielCastro.ca>"
Expand All @@ -9,19 +9,19 @@ description = "Easily serialize object for use with neon, fork of neon-serde"
license = "MIT"
repository = "https://github.com/matrix-org/neon-serde"
readme = "readme.md"
edition = "2021"

[dependencies]
serde = "1.0"
error-chain = "0.12.4"

[dependencies.neon]
version = "0.8"
version = "0.10"
default-features = false
features = ["default-panic-hook", "napi-6", "try-catch-api", "event-queue-api"]

[dependencies.num]
version = "0.4.0"
default-features = false

[dev-dependencies]
serde_derive = "1.0"
[dev-dependencies.serde]
features = ["derive"]
80 changes: 51 additions & 29 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
//! Deserialize a `JsValue` into a Rust data structure
//!

use errors::Error as LibError;
use errors::ErrorKind;
use errors::Result as LibResult;
use neon::prelude::*;
use serde;
use serde::de::Visitor;
use serde::de::{DeserializeOwned, DeserializeSeed, EnumAccess, MapAccess, SeqAccess, Unexpected,
VariantAccess};
use crate::errors::{Error as LibError, Result as LibResult};
use neon::{prelude::*, types::buffer::TypedArray};
use serde::{
de::{
DeserializeOwned, DeserializeSeed, EnumAccess, MapAccess, SeqAccess, Unexpected,
VariantAccess, Visitor,
},
forward_to_deserialize_any,
};

/// Deserialize an instance of type `T` from a `Handle<JsValue>`
///
/// # Errors
///
/// Can fail for various reasons see `ErrorKind`
/// Can fail for various reasons see `Error`
///
pub fn from_value<'j, C, T>(cx: &mut C, value: Handle<'j, JsValue>) -> LibResult<T>
where
Expand All @@ -27,6 +28,12 @@ where
Ok(t)
}

/// Deserialize an instance of type `T` from an `Option<Handle<JsValue>>`
///
/// # Errors
///
/// Can fail for various reasons see `Error`
///
pub fn from_value_opt<'j, C, T>(cx: &mut C, value: Option<Handle<'j, JsValue>>) -> LibResult<T>
where
C: Context<'j>,
Expand All @@ -50,22 +57,28 @@ impl<'a, 'j, C: Context<'j>> Deserializer<'a, 'j, C> {
}

#[doc(hidden)]
impl<'x, 'd, 'a, 'j, C: Context<'j>> serde::de::Deserializer<'x> for &'d mut Deserializer<'a, 'j, C> {
impl<'x, 'd, 'a, 'j, C: Context<'j>> serde::de::Deserializer<'x>
for &'d mut Deserializer<'a, 'j, C>
{
type Error = LibError;

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'x>,
{
if self.input.downcast::<JsNull, C>(self.cx).is_ok() || self.input.downcast::<JsUndefined, C>(self.cx).is_ok() {
if self.input.downcast::<JsNull, C>(self.cx).is_ok()
|| self.input.downcast::<JsUndefined, C>(self.cx).is_ok()
{
visitor.visit_unit()
} else if let Ok(val) = self.input.downcast::<JsBoolean, C>(self.cx) {
visitor.visit_bool(val.value(self.cx))
} else if let Ok(val) = self.input.downcast::<JsString, C>(self.cx) {
visitor.visit_string(val.value(self.cx))
} else if let Ok(val) = self.input.downcast::<JsNumber, C>(self.cx) {
let v = val.value(self.cx);
#[allow(clippy::float_cmp)]
if v.trunc() == v {
#[allow(clippy::cast_possible_truncation)]
visitor.visit_i64(v as i64)
} else {
visitor.visit_f64(v)
Expand All @@ -79,17 +92,19 @@ impl<'x, 'd, 'a, 'j, C: Context<'j>> serde::de::Deserializer<'x> for &'d mut Des
let mut deserializer = JsObjectAccess::new(self.cx, val)?;
visitor.visit_map(&mut deserializer)
} else {
bail!(ErrorKind::NotImplemented(
"unimplemented Deserializer::Deserializer",
));
Err(LibError::NotImplemented {
name: "unimplemented Deserializer::Deserializer",
})
}
}

fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'x>,
{
if self.input.downcast::<JsNull, C>(self.cx).is_ok() || self.input.downcast::<JsUndefined, C>(self.cx).is_ok() {
if self.input.downcast::<JsNull, C>(self.cx).is_ok()
|| self.input.downcast::<JsUndefined, C>(self.cx).is_ok()
{
visitor.visit_none()
} else {
visitor.visit_some(self)
Expand All @@ -112,36 +127,40 @@ impl<'x, 'd, 'a, 'j, C: Context<'j>> serde::de::Deserializer<'x> for &'d mut Des
let prop_names = val.get_own_property_names(self.cx)?;
let len = prop_names.len(self.cx);
if len != 1 {
Err(ErrorKind::InvalidKeyType(format!(
"object key with {} properties",
len
)))?
return Err(LibError::InvalidKeyType {
key: format!("object key with {} properties", len),
});
}
let key = prop_names.get(self.cx, 0)?.downcast::<JsString, C>(self.cx).or_throw(self.cx)?;
let key = prop_names
.get::<JsValue, _, _>(self.cx, 0)?
.downcast_or_throw::<JsString, C>(self.cx)?;
let enum_value = val.get(self.cx, key)?;
let key_value = key.value(self.cx);
visitor.visit_enum(JsEnumAccess::new(self.cx, key_value, Some(enum_value)))
} else {
let m = self.input.to_string(self.cx)?.value(self.cx);
Err(ErrorKind::InvalidKeyType(m))?
Err(LibError::InvalidKeyType { key: m })
}
}

fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'x>,
{
let buff = self.input.downcast::<JsBuffer, C>(self.cx).or_throw(self.cx)?;
let copy = self.cx.borrow(&buff, |buff| Vec::from(buff.as_slice()));
let buff = self.input.downcast_or_throw::<JsBuffer, C>(self.cx)?;
let copy = Vec::from(buff.as_slice(self.cx));
visitor.visit_bytes(&copy)
}

fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'x>,
{
let buff = self.input.downcast::<JsBuffer, C>(self.cx).or_throw(self.cx)?;
let copy = self.cx.borrow(&buff, |buff| Vec::from(buff.as_slice()));
let buff = self
.input
.downcast::<JsBuffer, C>(self.cx)
.or_throw(self.cx)?;
let copy = Vec::from(buff.as_slice(self.cx));
visitor.visit_byte_buf(copy)
}

Expand Down Expand Up @@ -247,9 +266,12 @@ impl<'x, 'a, 'j, C: Context<'j>> MapAccess<'x> for JsObjectAccess<'a, 'j, C> {
V: DeserializeSeed<'x>,
{
if self.idx >= self.len {
return Err(ErrorKind::ArrayIndexOutOfBounds(self.len, self.idx))?;
return Err(LibError::ArrayIndexOutOfBounds {
length: self.len,
index: self.idx,
});
}
let prop_name = self.prop_names.get(self.cx, self.idx)?;
let prop_name = self.prop_names.get::<JsString, _, _>(self.cx, self.idx)?;
let value = self.input.get(self.cx, prop_name)?;

self.idx += 1;
Expand Down Expand Up @@ -351,7 +373,7 @@ impl<'x, 'a, 'j, C: Context<'j>> VariantAccess<'x> for JsVariantAccess<'a, 'j, C
&"tuple variant",
))
}
},
}
None => Err(serde::de::Error::invalid_type(
Unexpected::UnitVariant,
&"tuple variant",
Expand All @@ -378,7 +400,7 @@ impl<'x, 'a, 'j, C: Context<'j>> VariantAccess<'x> for JsVariantAccess<'a, 'j, C
&"struct variant",
))
}
},
}
_ => Err(serde::de::Error::invalid_type(
Unexpected::UnitVariant,
&"struct variant",
Expand Down
Loading