summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/serde/mod.rs6
-rw-r--r--src/serde/ser.rs38
2 files changed, 37 insertions, 7 deletions
diff --git a/src/serde/mod.rs b/src/serde/mod.rs
index e90998e..d9253d0 100644
--- a/src/serde/mod.rs
+++ b/src/serde/mod.rs
@@ -116,10 +116,8 @@ pub trait LuaSerdeExt<'lua> {
/// fn main() -> Result<()> {
/// let lua = Lua::new();
/// let v = vec![1, 2, 3];
- /// lua.globals().set("v", lua.to_value_with(&v, SerializeOptions {
- /// set_array_metatable: false,
- /// ..SerializeOptions::default()
- /// })?)?;
+ /// let options = SerializeOptions::new().set_array_metatable(false);
+ /// lua.globals().set("v", lua.to_value_with(&v, options)?)?;
///
/// lua.load(r#"
/// assert(#v == 3 and v[1] == 1 and v[2] == 2 and v[3] == 3)
diff --git a/src/serde/ser.rs b/src/serde/ser.rs
index f8b0b17..f94d2f7 100644
--- a/src/serde/ser.rs
+++ b/src/serde/ser.rs
@@ -21,11 +21,12 @@ pub struct Serializer<'lua> {
/// A struct with options to change default serializer behaviour.
#[derive(Debug, Clone, Copy)]
+#[non_exhaustive]
pub struct Options {
/// If true, sequence serialization to a Lua table will create table
/// with the [`array_metatable`] attached.
///
- /// Default: true
+ /// Default: **true**
///
/// [`array_metatable`]: ../trait.LuaSerdeExt.html#tymethod.array_metatable
pub set_array_metatable: bool,
@@ -33,7 +34,7 @@ pub struct Options {
/// If true, serialize `None` (part of `Option` type) to [`null`].
/// Otherwise it will be set to Lua [`Nil`].
///
- /// Default: true
+ /// Default: **true**
///
/// [`null`]: ../trait.LuaSerdeExt.html#tymethod.null
/// [`Nil`]: ../../enum.Value.html#variant.Nil
@@ -42,7 +43,7 @@ pub struct Options {
/// If true, serialize `Unit` (type of `()` in Rust) and Unit structs to [`null`].
/// Otherwise it will be set to Lua [`Nil`].
///
- /// Default: true
+ /// Default: **true**
///
/// [`null`]: ../trait.LuaSerdeExt.html#tymethod.null
/// [`Nil`]: ../../enum.Value.html#variant.Nil
@@ -59,6 +60,37 @@ impl Default for Options {
}
}
+impl Options {
+ /// Retruns a new instance of `Options` with default parameters.
+ pub fn new() -> Self {
+ Self::default()
+ }
+
+ /// Sets [`set_array_metatable`] option.
+ ///
+ /// [`set_array_metatable`]: #structfield.set_array_metatable
+ pub fn set_array_metatable(mut self, enabled: bool) -> Self {
+ self.set_array_metatable = enabled;
+ self
+ }
+
+ /// Sets [`serialize_none_to_null`] option.
+ ///
+ /// [`serialize_none_to_null`]: #structfield.serialize_none_to_null
+ pub fn serialize_none_to_null(mut self, enabled: bool) -> Self {
+ self.serialize_none_to_null = enabled;
+ self
+ }
+
+ /// Sets [`serialize_unit_to_null`] option.
+ ///
+ /// [`serialize_unit_to_null`]: #structfield.serialize_unit_to_null
+ pub fn serialize_unit_to_null(mut self, enabled: bool) -> Self {
+ self.serialize_unit_to_null = enabled;
+ self
+ }
+}
+
impl<'lua> Serializer<'lua> {
/// Creates a new Lua Serializer with default options.
pub fn new(lua: &'lua Lua) -> Self {