fully adapt to mlua

This commit is contained in:
Cadey Ratio 2020-07-05 20:57:24 -04:00
parent 9d12a4fc4b
commit 964efbbc73
5 changed files with 259 additions and 188 deletions

View File

@ -1,22 +1,22 @@
# rlue_serde
Implementation of [serde](https://serde.rs/) Serializer/Deserializer for [rlua::Value](https://docs.rs/rlua/0.12/rlua/enum.Value.html)
Implementation of [serde](https://serde.rs/) Serializer/Deserializer for [mlua::Value](https://docs.rs/mlua/0.12/mlua/enum.Value.html)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![Crates.io](https://img.shields.io/crates/v/rlua_serde.svg)](https://crates.io/crates/rlua_serde)
[![Documentation](https://docs.rs/rlua_serde/badge.svg)][dox]
[![Crates.io](https://img.shields.io/crates/v/mlua_serde.svg)](https://crates.io/crates/mlua_serde)
[![Documentation](https://docs.rs/mlua_serde/badge.svg)][dox]
More information about this crate can be found in the [crate documentation][dox].
[dox]: https://docs.rs/rlua_serde/*/rlua_serde/
[dox]: https://docs.rs/mlua_serde/*/mlua_serde/
## Usage
To use `rlua_serde`, first add this to your `Cargo.toml`:
To use `mlua_serde`, first add this to your `Cargo.toml`:
```toml
[dependencies]
rlua_serde = "0.4"
mlua_serde = "0.4"
```
Next, you can use `to_value`/`from_value` functions to serialize/deserialize:
@ -29,20 +29,18 @@ struct Foo {
}
fn main() {
let lua = rlua::Lua::new();
lua.context(|lua| {
let lua = mlua::Lua::new();
let foo = Foo {
bar: 42,
baz: vec![String::from("fizz"), String::from("buzz")],
};
let value = rlua_serde::to_value(lua, &foo).unwrap();
let value = mlua_serde::to_value(lua, &foo).unwrap();
lua.globals().set("value", value).unwrap();
lua.load(
r#"
assert(value["bar"] == 42)
assert(value["baz"][2] == "buzz")
"#).exec().unwrap();
});
}
```

View File

@ -3,6 +3,6 @@ let
in
pkgs.mkShell {
buildInputs = with pkgs; [
rustc cargo rls lua5_3 pkg-config
rustc cargo rls lua5_3 pkg-config rustfmt
];
}

173
src/de.rs
View File

@ -1,11 +1,10 @@
use serde;
use serde::de::IntoDeserializer;
use mlua::{Value, TablePairs, TableSequence};
use mlua::{TablePairs, TableSequence, Value};
use error::{Error, Result};
pub struct Deserializer<'lua> {
pub value: Value<'lua>,
}
@ -15,7 +14,8 @@ impl<'lua, 'de> serde::Deserializer<'de> for Deserializer<'lua> {
#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
where V: serde::de::Visitor<'de>
where
V: serde::de::Visitor<'de>,
{
match self.value {
Value::Nil => visitor.visit_unit(),
@ -31,16 +31,20 @@ impl<'lua, 'de> serde::Deserializer<'de> for Deserializer<'lua> {
if remaining == 0 {
Ok(map)
} else {
Err(serde::de::Error::invalid_length(len, &"fewer elements in array"))
Err(serde::de::Error::invalid_length(
len,
&"fewer elements in array",
))
}
}
},
_ => Err(serde::de::Error::custom("invalid value type")),
}
}
#[inline]
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
where V: serde::de::Visitor<'de>
where
V: serde::de::Visitor<'de>,
{
match self.value {
Value::Nil => visitor.visit_none(),
@ -50,26 +54,32 @@ impl<'lua, 'de> serde::Deserializer<'de> for Deserializer<'lua> {
#[inline]
fn deserialize_enum<V>(
self, _name: &str, _variants: &'static [&'static str], visitor: V
self,
_name: &str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where V: serde::de::Visitor<'de>
where
V: serde::de::Visitor<'de>,
{
let (variant, value) = match self.value {
Value::Table(value) => {
let mut iter = value.pairs::<String, Value>();
let (variant, value) = match iter.next() {
Some(v) => v?,
None => return Err(serde::de::Error::invalid_value(
None => {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Map,
&"map with a single key",
)),
))
}
};
if iter.next().is_some() {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Map,
&"map with a single key",
))
));
}
(variant, Some(value))
}
@ -82,7 +92,8 @@ impl<'lua, 'de> serde::Deserializer<'de> for Deserializer<'lua> {
#[inline]
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
where V: serde::de::Visitor<'de>
where
V: serde::de::Visitor<'de>,
{
match self.value {
Value::Table(v) => {
@ -93,7 +104,10 @@ impl<'lua, 'de> serde::Deserializer<'de> for Deserializer<'lua> {
if remaining == 0 {
Ok(seq)
} else {
Err(serde::de::Error::invalid_length(len, &"fewer elements in array"))
Err(serde::de::Error::invalid_length(
len,
&"fewer elements in array",
))
}
}
_ => Err(serde::de::Error::custom("invalid value type")),
@ -102,14 +116,21 @@ impl<'lua, 'de> serde::Deserializer<'de> for Deserializer<'lua> {
#[inline]
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
where V: serde::de::Visitor<'de>
where
V: serde::de::Visitor<'de>,
{
self.deserialize_seq(visitor)
}
#[inline]
fn deserialize_tuple_struct<V>(self, _name: &'static str, _len: usize, visitor: V) -> Result<V::Value>
where V: serde::de::Visitor<'de>
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
_len: usize,
visitor: V,
) -> Result<V::Value>
where
V: serde::de::Visitor<'de>,
{
self.deserialize_seq(visitor)
}
@ -121,19 +142,18 @@ impl<'lua, 'de> serde::Deserializer<'de> for Deserializer<'lua> {
}
}
struct SeqDeserializer<'lua>(TableSequence<'lua, Value<'lua>>);
impl<'lua, 'de> serde::de::SeqAccess<'de> for SeqDeserializer<'lua> {
type Error = Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
where T: serde::de::DeserializeSeed<'de>
where
T: serde::de::DeserializeSeed<'de>,
{
match self.0.next() {
Some(value) => seed.deserialize(Deserializer { value: value? })
.map(Some),
None => Ok(None)
Some(value) => seed.deserialize(Deserializer { value: value? }).map(Some),
None => Ok(None),
}
}
@ -145,17 +165,17 @@ impl<'lua, 'de> serde::de::SeqAccess<'de> for SeqDeserializer<'lua> {
}
}
struct MapDeserializer<'lua>(
TablePairs<'lua, Value<'lua>, Value<'lua>>,
Option<Value<'lua>>
Option<Value<'lua>>,
);
impl<'lua, 'de> serde::de::MapAccess<'de> for MapDeserializer<'lua> {
type Error = Error;
fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
where T: serde::de::DeserializeSeed<'de>
where
T: serde::de::DeserializeSeed<'de>,
{
match self.0.next() {
Some(item) => {
@ -163,13 +183,14 @@ impl<'lua, 'de> serde::de::MapAccess<'de> for MapDeserializer<'lua> {
self.1 = Some(value);
let key_de = Deserializer { value: key };
seed.deserialize(key_de).map(Some)
},
}
None => Ok(None),
}
}
fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value>
where T: serde::de::DeserializeSeed<'de>
where
T: serde::de::DeserializeSeed<'de>,
{
match self.1.take() {
Some(value) => seed.deserialize(Deserializer { value }),
@ -185,7 +206,6 @@ impl<'lua, 'de> serde::de::MapAccess<'de> for MapDeserializer<'lua> {
}
}
struct EnumDeserializer<'lua> {
variant: String,
value: Option<Value<'lua>>,
@ -196,7 +216,8 @@ impl<'lua, 'de> serde::de::EnumAccess<'de> for EnumDeserializer<'lua> {
type Variant = VariantDeserializer<'lua>;
fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant)>
where T: serde::de::DeserializeSeed<'de>
where
T: serde::de::DeserializeSeed<'de>,
{
let variant = self.variant.into_deserializer();
let variant_access = VariantDeserializer { value: self.value };
@ -204,7 +225,6 @@ impl<'lua, 'de> serde::de::EnumAccess<'de> for EnumDeserializer<'lua> {
}
}
struct VariantDeserializer<'lua> {
value: Option<Value<'lua>>,
}
@ -218,49 +238,46 @@ impl<'lua, 'de> serde::de::VariantAccess<'de> for VariantDeserializer<'lua> {
serde::de::Unexpected::NewtypeVariant,
&"unit variant",
)),
None => Ok(())
None => Ok(()),
}
}
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
where T: serde::de::DeserializeSeed<'de>
where
T: serde::de::DeserializeSeed<'de>,
{
match self.value {
Some(value) => seed.deserialize(Deserializer { value }),
None => Err(serde::de::Error::invalid_type(
serde::de::Unexpected::UnitVariant,
&"newtype variant",
))
)),
}
}
fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
where V: serde::de::Visitor<'de>
where
V: serde::de::Visitor<'de>,
{
match self.value {
Some(value) => serde::Deserializer::deserialize_seq(
Deserializer { value }, visitor
),
Some(value) => serde::Deserializer::deserialize_seq(Deserializer { value }, visitor),
None => Err(serde::de::Error::invalid_type(
serde::de::Unexpected::UnitVariant,
&"tuple variant",
))
)),
}
}
fn struct_variant<V>(
self, _fields: &'static [&'static str], visitor: V
) -> Result<V::Value>
where V: serde::de::Visitor<'de>
fn struct_variant<V>(self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value>
where
V: serde::de::Visitor<'de>,
{
match self.value {
Some(value) => serde::Deserializer::deserialize_map(
Deserializer { value }, visitor
),
Some(value) => serde::Deserializer::deserialize_map(Deserializer { value }, visitor),
None => Err(serde::de::Error::invalid_type(
serde::de::Unexpected::UnitVariant,
&"struct variant",
))
)),
}
}
}
@ -285,23 +302,26 @@ mod tests {
int: 1,
seq: vec!["a".to_owned(), "b".to_owned()],
map: vec![(1, 2), (4, 1)].into_iter().collect(),
empty: vec![]
empty: vec![],
};
println!("{:?}", expected);
let lua = Lua::new();
lua.context(|lua| {
let value = lua.load(r#"
let value = lua
.load(
r#"
a = {}
a.int = 1
a.seq = {"a", "b"}
a.map = {2, [4]=1}
a.empty = {}
return a
"#).eval().unwrap();
"#,
)
.eval()
.unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
});
}
#[test]
@ -310,25 +330,31 @@ mod tests {
struct Rgb(u8, u8, u8);
let lua = Lua::new();
lua.context(|lua| {
let expected = Rgb(1, 2, 3);
let value = lua.load(
let value = lua
.load(
r#"
a = {1, 2, 3}
return a
"#).eval().unwrap();
"#,
)
.eval()
.unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
let expected = (1, 2, 3);
let value = lua.load(
let value = lua
.load(
r#"
a = {1, 2, 3}
return a
"#).eval().unwrap();
"#,
)
.eval()
.unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
});
}
#[test]
@ -342,46 +368,59 @@ mod tests {
}
let lua = Lua::new();
lua.context(|lua| {
let expected = E::Unit;
let value = lua.load(
let value = lua
.load(
r#"
return "Unit"
"#).eval().unwrap();
"#,
)
.eval()
.unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
let expected = E::Newtype(1);
let value = lua.load(
let value = lua
.load(
r#"
a = {}
a["Newtype"] = 1
return a
"#).eval().unwrap();
"#,
)
.eval()
.unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
let expected = E::Tuple(1, 2);
let value = lua.load(
let value = lua
.load(
r#"
a = {}
a["Tuple"] = {1, 2}
return a
"#).eval().unwrap();
"#,
)
.eval()
.unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
let expected = E::Struct { a: 1 };
let value = lua.load(
let value = lua
.load(
r#"
a = {}
a["Struct"] = {}
a["Struct"]["a"] = 1
return a
"#).eval().unwrap();
"#,
)
.eval()
.unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
});
}
}

View File

@ -20,20 +20,18 @@
//! }
//!
//! let lua = mlua::Lua::new();
//! lua.context(|lua| {
//! let foo = Foo {
//! bar: 42,
//! baz: vec![String::from("fizz"), String::from("buzz")],
//! };
//!
//! let value = mlua_serde::to_value(lua, &foo).unwrap();
//! let value = mlua_serde::to_value(&lua, &foo).unwrap();
//! lua.globals().set("value", value).unwrap();
//! lua.load(
//! r#"
//! assert(value["bar"] == 42)
//! assert(value["baz"][2] == "buzz")
//! "#).exec().unwrap();
//! });
//! }
//! ```

View File

@ -1,10 +1,9 @@
use serde;
use mlua::{Lua, Value, Table, String as LuaString};
use mlua::{Lua, String as LuaString, Table, Value};
use to_value;
use error::{Error, Result};
use to_value;
pub struct Serializer<'lua> {
pub lua: &'lua Lua,
@ -91,7 +90,9 @@ impl<'lua> serde::Serializer for Serializer<'lua> {
#[inline]
fn serialize_bytes(self, value: &[u8]) -> Result<Value<'lua>> {
Ok(Value::Table(self.lua.create_sequence_from(value.iter().cloned())?))
Ok(Value::Table(
self.lua.create_sequence_from(value.iter().cloned())?,
))
}
#[inline]
@ -106,25 +107,31 @@ impl<'lua> serde::Serializer for Serializer<'lua> {
#[inline]
fn serialize_unit_variant(
self, _name: &'static str, _variant_index: u32, variant: &'static str
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
) -> Result<Value<'lua>> {
self.serialize_str(variant)
}
#[inline]
fn serialize_newtype_struct<T>(
self, _name: &'static str, value: &T
) -> Result<Value<'lua>>
where T: ?Sized + serde::Serialize,
fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Value<'lua>>
where
T: ?Sized + serde::Serialize,
{
value.serialize(self)
}
fn serialize_newtype_variant<T>(
self, _name: &'static str, _variant_index: u32,
variant: &'static str, value: &T,
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
value: &T,
) -> Result<Value<'lua>>
where T: ?Sized + serde::Serialize,
where
T: ?Sized + serde::Serialize,
{
let table = self.lua.create_table()?;
let variant = self.lua.create_string(variant)?;
@ -140,7 +147,8 @@ impl<'lua> serde::Serializer for Serializer<'lua> {
#[inline]
fn serialize_some<T>(self, value: &T) -> Result<Value<'lua>>
where T: ?Sized + serde::Serialize,
where
T: ?Sized + serde::Serialize,
{
value.serialize(self)
}
@ -159,14 +167,19 @@ impl<'lua> serde::Serializer for Serializer<'lua> {
}
fn serialize_tuple_struct(
self, _name: &'static str, len: usize,
self,
_name: &'static str,
len: usize,
) -> Result<Self::SerializeTupleStruct> {
self.serialize_seq(Some(len))
}
fn serialize_tuple_variant(
self, _name: &'static str, _variant_index: u32,
variant: &'static str, _len: usize,
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleVariant> {
let name = self.lua.create_string(variant)?;
let table = self.lua.create_table()?;
@ -174,7 +187,7 @@ impl<'lua> serde::Serializer for Serializer<'lua> {
lua: self.lua,
idx: 1,
name,
table
table,
})
}
@ -192,8 +205,11 @@ impl<'lua> serde::Serializer for Serializer<'lua> {
}
fn serialize_struct_variant(
self, _name: &'static str, _variant_index: u32,
variant: &'static str, _len: usize,
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
_len: usize,
) -> Result<Self::SerializeStructVariant> {
let name = self.lua.create_string(variant)?;
let table = self.lua.create_table()?;
@ -203,10 +219,8 @@ impl<'lua> serde::Serializer for Serializer<'lua> {
table,
})
}
}
pub struct SerializeVec<'lua> {
lua: &'lua Lua,
table: Table<'lua>,
@ -218,7 +232,8 @@ impl<'lua> serde::ser::SerializeSeq for SerializeVec<'lua> {
type Error = Error;
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
where T: ?Sized + serde::Serialize,
where
T: ?Sized + serde::Serialize,
{
self.table.set(self.idx, to_value(self.lua, value)?)?;
self.idx += 1;
@ -235,7 +250,8 @@ impl<'lua> serde::ser::SerializeTuple for SerializeVec<'lua> {
type Error = Error;
fn serialize_element<T>(&mut self, value: &T) -> Result<()>
where T: ?Sized + serde::Serialize,
where
T: ?Sized + serde::Serialize,
{
serde::ser::SerializeSeq::serialize_element(self, value)
}
@ -250,7 +266,8 @@ impl<'lua> serde::ser::SerializeTupleStruct for SerializeVec<'lua> {
type Error = Error;
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
where T: ?Sized + serde::Serialize,
where
T: ?Sized + serde::Serialize,
{
serde::ser::SerializeSeq::serialize_element(self, value)
}
@ -260,7 +277,6 @@ impl<'lua> serde::ser::SerializeTupleStruct for SerializeVec<'lua> {
}
}
pub struct SerializeTupleVariant<'lua> {
lua: &'lua Lua,
name: LuaString<'lua>,
@ -273,7 +289,8 @@ impl<'lua> serde::ser::SerializeTupleVariant for SerializeTupleVariant<'lua> {
type Error = Error;
fn serialize_field<T>(&mut self, value: &T) -> Result<()>
where T: ?Sized + serde::Serialize,
where
T: ?Sized + serde::Serialize,
{
self.table.set(self.idx, to_value(self.lua, value)?)?;
self.idx += 1;
@ -287,11 +304,10 @@ impl<'lua> serde::ser::SerializeTupleVariant for SerializeTupleVariant<'lua> {
}
}
pub struct SerializeMap<'lua> {
lua: &'lua Lua,
table: Table<'lua>,
next_key: Option<Value<'lua>>
next_key: Option<Value<'lua>>,
}
impl<'lua> serde::ser::SerializeMap for SerializeMap<'lua> {
@ -299,14 +315,16 @@ impl<'lua> serde::ser::SerializeMap for SerializeMap<'lua> {
type Error = Error;
fn serialize_key<T>(&mut self, key: &T) -> Result<()>
where T: ?Sized + serde::Serialize,
where
T: ?Sized + serde::Serialize,
{
self.next_key = Some(to_value(self.lua, key)?);
Ok(())
}
fn serialize_value<T>(&mut self, value: &T) -> Result<()>
where T: ?Sized + serde::Serialize,
where
T: ?Sized + serde::Serialize,
{
let key = self.next_key.take();
// Panic because this indicates a bug in the program rather than an
@ -326,7 +344,8 @@ impl<'lua> serde::ser::SerializeStruct for SerializeMap<'lua> {
type Error = Error;
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
where T: ?Sized + serde::Serialize,
where
T: ?Sized + serde::Serialize,
{
serde::ser::SerializeMap::serialize_key(self, key)?;
serde::ser::SerializeMap::serialize_value(self, value)
@ -337,7 +356,6 @@ impl<'lua> serde::ser::SerializeStruct for SerializeMap<'lua> {
}
}
pub struct SerializeStructVariant<'lua> {
lua: &'lua Lua,
name: LuaString<'lua>,
@ -349,10 +367,10 @@ impl<'lua> serde::ser::SerializeStructVariant for SerializeStructVariant<'lua> {
type Error = Error;
fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
where T: ?Sized + serde::Serialize,
where
T: ?Sized + serde::Serialize,
{
self.table
.set(key, to_value(self.lua, value)?)?;
self.table.set(key, to_value(self.lua, value)?)?;
Ok(())
}
@ -365,8 +383,8 @@ impl<'lua> serde::ser::SerializeStructVariant for SerializeStructVariant<'lua> {
#[cfg(test)]
mod tests {
use mlua::Lua;
use super::*;
use mlua::Lua;
#[test]
fn test_struct() {
@ -376,19 +394,23 @@ mod tests {
seq: Vec<&'static str>,
}
let test = Test { int: 1, seq: vec!["a", "b"] };
let test = Test {
int: 1,
seq: vec!["a", "b"],
};
let lua = Lua::new();
lua.context(|lua| {
let value = to_value(lua, &test).unwrap();
let value = to_value(&lua, &test).unwrap();
lua.globals().set("value", value).unwrap();
lua.load(
r#"
assert(value["int"] == 1)
assert(value["seq"][1] == "a")
assert(value["seq"][2] == "b")
"#).exec()
}).unwrap()
"#,
)
.exec()
.unwrap()
}
#[test]
@ -403,35 +425,49 @@ mod tests {
let lua = Lua::new();
lua.context(|lua| {
let u = E::Unit;
let value = to_value(lua, &u).unwrap();
let value = to_value(&lua, &u).unwrap();
lua.globals().set("value", value).unwrap();
lua.load(r#"
lua.load(
r#"
assert(value == "Unit")
"#).exec().unwrap();
"#,
)
.exec()
.unwrap();
let n = E::Newtype(1);
let value = to_value(lua, &n).unwrap();
let value = to_value(&lua, &n).unwrap();
lua.globals().set("value", value).unwrap();
lua.load(r#"
lua.load(
r#"
assert(value["Newtype"] == 1)
"#).exec().unwrap();
"#,
)
.exec()
.unwrap();
let t = E::Tuple(1, 2);
let value = to_value(lua, &t).unwrap();
let value = to_value(&lua, &t).unwrap();
lua.globals().set("value", value).unwrap();
lua.load(r#"
lua.load(
r#"
assert(value["Tuple"][1] == 1)
assert(value["Tuple"][2] == 2)
"#).exec().unwrap();
"#,
)
.exec()
.unwrap();
let s = E::Struct { a: 1 };
let value = to_value(lua, &s).unwrap();
let value = to_value(&lua, &s).unwrap();
lua.globals().set("value", value).unwrap();
lua.load(r#"
lua.load(
r#"
assert(value["Struct"]["a"] == 1)
"#).exec()
}).unwrap();
"#,
)
.exec()
.unwrap();
}
}