Make `SerializeOptions` non_exhaustive.
Add builder implementation similar to `LuaOptions` to set individual options.
This commit is contained in:
parent
0f4bcca7ce
commit
af67971e0d
|
@ -116,10 +116,8 @@ pub trait LuaSerdeExt<'lua> {
|
||||||
/// fn main() -> Result<()> {
|
/// fn main() -> Result<()> {
|
||||||
/// let lua = Lua::new();
|
/// let lua = Lua::new();
|
||||||
/// let v = vec![1, 2, 3];
|
/// let v = vec![1, 2, 3];
|
||||||
/// lua.globals().set("v", lua.to_value_with(&v, SerializeOptions {
|
/// let options = SerializeOptions::new().set_array_metatable(false);
|
||||||
/// set_array_metatable: false,
|
/// lua.globals().set("v", lua.to_value_with(&v, options)?)?;
|
||||||
/// ..SerializeOptions::default()
|
|
||||||
/// })?)?;
|
|
||||||
///
|
///
|
||||||
/// lua.load(r#"
|
/// lua.load(r#"
|
||||||
/// assert(#v == 3 and v[1] == 1 and v[2] == 2 and v[3] == 3)
|
/// assert(#v == 3 and v[1] == 1 and v[2] == 2 and v[3] == 3)
|
||||||
|
|
|
@ -21,11 +21,12 @@ pub struct Serializer<'lua> {
|
||||||
|
|
||||||
/// A struct with options to change default serializer behaviour.
|
/// A struct with options to change default serializer behaviour.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
#[non_exhaustive]
|
||||||
pub struct Options {
|
pub struct Options {
|
||||||
/// If true, sequence serialization to a Lua table will create table
|
/// If true, sequence serialization to a Lua table will create table
|
||||||
/// with the [`array_metatable`] attached.
|
/// with the [`array_metatable`] attached.
|
||||||
///
|
///
|
||||||
/// Default: true
|
/// Default: **true**
|
||||||
///
|
///
|
||||||
/// [`array_metatable`]: ../trait.LuaSerdeExt.html#tymethod.array_metatable
|
/// [`array_metatable`]: ../trait.LuaSerdeExt.html#tymethod.array_metatable
|
||||||
pub set_array_metatable: bool,
|
pub set_array_metatable: bool,
|
||||||
|
@ -33,7 +34,7 @@ pub struct Options {
|
||||||
/// If true, serialize `None` (part of `Option` type) to [`null`].
|
/// If true, serialize `None` (part of `Option` type) to [`null`].
|
||||||
/// Otherwise it will be set to Lua [`Nil`].
|
/// Otherwise it will be set to Lua [`Nil`].
|
||||||
///
|
///
|
||||||
/// Default: true
|
/// Default: **true**
|
||||||
///
|
///
|
||||||
/// [`null`]: ../trait.LuaSerdeExt.html#tymethod.null
|
/// [`null`]: ../trait.LuaSerdeExt.html#tymethod.null
|
||||||
/// [`Nil`]: ../../enum.Value.html#variant.Nil
|
/// [`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`].
|
/// If true, serialize `Unit` (type of `()` in Rust) and Unit structs to [`null`].
|
||||||
/// Otherwise it will be set to Lua [`Nil`].
|
/// Otherwise it will be set to Lua [`Nil`].
|
||||||
///
|
///
|
||||||
/// Default: true
|
/// Default: **true**
|
||||||
///
|
///
|
||||||
/// [`null`]: ../trait.LuaSerdeExt.html#tymethod.null
|
/// [`null`]: ../trait.LuaSerdeExt.html#tymethod.null
|
||||||
/// [`Nil`]: ../../enum.Value.html#variant.Nil
|
/// [`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> {
|
impl<'lua> Serializer<'lua> {
|
||||||
/// Creates a new Lua Serializer with default options.
|
/// Creates a new Lua Serializer with default options.
|
||||||
pub fn new(lua: &'lua Lua) -> Self {
|
pub fn new(lua: &'lua Lua) -> Self {
|
||||||
|
|
|
@ -241,10 +241,7 @@ fn test_to_value_with_options() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
// set_array_metatable
|
// set_array_metatable
|
||||||
let data = lua.to_value_with(
|
let data = lua.to_value_with(
|
||||||
&Vec::<i32>::new(),
|
&Vec::<i32>::new(),
|
||||||
SerializeOptions {
|
SerializeOptions::new().set_array_metatable(false),
|
||||||
set_array_metatable: false,
|
|
||||||
..SerializeOptions::default()
|
|
||||||
},
|
|
||||||
)?;
|
)?;
|
||||||
globals.set("data", data)?;
|
globals.set("data", data)?;
|
||||||
lua.load(
|
lua.load(
|
||||||
|
@ -275,10 +272,7 @@ fn test_to_value_with_options() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
};
|
};
|
||||||
let data2 = lua.to_value_with(
|
let data2 = lua.to_value_with(
|
||||||
&mydata,
|
&mydata,
|
||||||
SerializeOptions {
|
SerializeOptions::new().serialize_none_to_null(false),
|
||||||
serialize_none_to_null: false,
|
|
||||||
..SerializeOptions::default()
|
|
||||||
},
|
|
||||||
)?;
|
)?;
|
||||||
globals.set("data2", data2)?;
|
globals.set("data2", data2)?;
|
||||||
lua.load(
|
lua.load(
|
||||||
|
@ -293,10 +287,7 @@ fn test_to_value_with_options() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
// serialize_unit_to_null
|
// serialize_unit_to_null
|
||||||
let data3 = lua.to_value_with(
|
let data3 = lua.to_value_with(
|
||||||
&mydata,
|
&mydata,
|
||||||
SerializeOptions {
|
SerializeOptions::new().serialize_unit_to_null(false),
|
||||||
serialize_unit_to_null: false,
|
|
||||||
..SerializeOptions::default()
|
|
||||||
},
|
|
||||||
)?;
|
)?;
|
||||||
globals.set("data3", data3)?;
|
globals.set("data3", data3)?;
|
||||||
lua.load(
|
lua.load(
|
||||||
|
|
Loading…
Reference in New Issue