1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::{
	lua::{LuaError, LuaTable},
	prelude::*,
};

pub trait ToLuaTable {
	fn to_lua_variant(self) -> Result<Option<LuaTable<'static>>, LuaError>;
}

impl ToLuaTable for () {
	fn to_lua_variant(self) -> Result<Option<LuaTable<'static>>, LuaError> {
		Ok(None)
	}
}

impl<T: TryInto<LuaTable<'static>>> ToLuaTable for T
where
	T::Error: Into<LuaError>,
{
	fn to_lua_variant(self) -> Result<Option<LuaTable<'static>>, LuaError> {
		self.try_into().map(Some).map_err(Into::into)
	}
}

impl<'a, T: TryInto<LuaTable<'a>>> ToLuaTable for Option<T>
where
	T::Error: Into<LuaError>,
{
	fn to_lua_variant(self) -> Result<Option<LuaTable<'static>>, LuaError> {
		self
			.map(TryInto::try_into)
			.transpose()
			.map_err(Into::into)
			.map(|v| v.map(|v| v.owned()))
	}
}

#[test]
fn to_lua_variant() {
	fn assert_impl<T: ToLuaTable>() {}

	assert_impl::<LuaTable>();
	assert_impl::<Option<LuaTable>>();
	assert_impl::<Option<Variant>>();
	assert_impl::<Variant>();
	assert_impl::<&'static Variant>();
	assert_impl::<Option<&'static Variant>>();
}