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 { diff --git a/tests/serde.rs b/tests/serde.rs index d5b8a79..86ecb50 100644 --- a/tests/serde.rs +++ b/tests/serde.rs @@ -241,10 +241,7 @@ fn test_to_value_with_options() -> Result<(), Box> { // set_array_metatable let data = lua.to_value_with( &Vec::::new(), - SerializeOptions { - set_array_metatable: false, - ..SerializeOptions::default() - }, + SerializeOptions::new().set_array_metatable(false), )?; globals.set("data", data)?; lua.load( @@ -275,10 +272,7 @@ fn test_to_value_with_options() -> Result<(), Box> { }; let data2 = lua.to_value_with( &mydata, - SerializeOptions { - serialize_none_to_null: false, - ..SerializeOptions::default() - }, + SerializeOptions::new().serialize_none_to_null(false), )?; globals.set("data2", data2)?; lua.load( @@ -293,10 +287,7 @@ fn test_to_value_with_options() -> Result<(), Box> { // serialize_unit_to_null let data3 = lua.to_value_with( &mydata, - SerializeOptions { - serialize_unit_to_null: false, - ..SerializeOptions::default() - }, + SerializeOptions::new().serialize_unit_to_null(false), )?; globals.set("data3", data3)?; lua.load(