summaryrefslogtreecommitdiff
path: root/src/serde/mod.rs
diff options
context:
space:
mode:
authorAlex Orlenko <zxteam@protonmail.com>2021-09-15 23:37:30 +0100
committerAlex Orlenko <zxteam@protonmail.com>2021-09-15 23:45:08 +0100
commitfc1fe2c15ec5923440f678a4a9102f591e691b50 (patch)
tree43ee3a1a5bcb4d5ad3a9a9be6d9a0a8683f294e1 /src/serde/mod.rs
parent7e7a44f4cd16600f2ce0d72204859d0ad35995c8 (diff)
downloadmlua-fc1fe2c15ec5923440f678a4a9102f591e691b50.zip
Add `DeserializeOptions` struct to control deserializer behavior.
This solves #74 and provides a way to deserialize a Lua globals table.
Diffstat (limited to 'src/serde/mod.rs')
-rw-r--r--src/serde/mod.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/serde/mod.rs b/src/serde/mod.rs
index 27ce492..2980dc8 100644
--- a/src/serde/mod.rs
+++ b/src/serde/mod.rs
@@ -158,6 +158,41 @@ pub trait LuaSerdeExt<'lua> {
/// }
/// ```
fn from_value<T: Deserialize<'lua>>(&'lua self, value: Value<'lua>) -> Result<T>;
+
+ /// Deserializes a `Value` into any serde deserializable object with options.
+ ///
+ /// Requires `feature = "serialize"`
+ ///
+ /// [`Value`]: enum.Value.html
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use mlua::{Lua, Result, LuaSerdeExt, DeserializeOptions};
+ /// use serde::Deserialize;
+ ///
+ /// #[derive(Deserialize, Debug, PartialEq)]
+ /// struct User {
+ /// name: String,
+ /// age: u8,
+ /// }
+ ///
+ /// fn main() -> Result<()> {
+ /// let lua = Lua::new();
+ /// let val = lua.load(r#"{name = "John Smith", age = 20, f = function() end}"#).eval()?;
+ /// let options = DeserializeOptions::new().deny_unsupported_types(false);
+ /// let u: User = lua.from_value_with(val, options)?;
+ ///
+ /// assert_eq!(u, User { name: "John Smith".into(), age: 20 });
+ ///
+ /// Ok(())
+ /// }
+ /// ```
+ fn from_value_with<T: Deserialize<'lua>>(
+ &'lua self,
+ value: Value<'lua>,
+ options: de::Options,
+ ) -> Result<T>;
}
impl<'lua> LuaSerdeExt<'lua> for Lua {
@@ -196,6 +231,13 @@ impl<'lua> LuaSerdeExt<'lua> for Lua {
{
T::deserialize(de::Deserializer::new(value))
}
+
+ fn from_value_with<T>(&'lua self, value: Value<'lua>, options: de::Options) -> Result<T>
+ where
+ T: Deserialize<'lua>,
+ {
+ T::deserialize(de::Deserializer::new_with_options(value, options))
+ }
}
// Uses 6 stack spaces and calls checkstack.