Make `SerializeOptions` non_exhaustive.

Add builder implementation similar to `LuaOptions` to set individual options.
This commit is contained in:
Alex Orlenko 2021-05-03 21:43:56 +01:00
parent 0f4bcca7ce
commit af67971e0d
3 changed files with 40 additions and 19 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -241,10 +241,7 @@ fn test_to_value_with_options() -> Result<(), Box<dyn std::error::Error>> {
// set_array_metatable
let data = lua.to_value_with(
&Vec::<i32>::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<dyn std::error::Error>> {
};
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<dyn std::error::Error>> {
// 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(