feat: accept more lua types for Vec3 and Direction

This commit is contained in:
Ryan 2025-02-17 23:09:44 -05:00
parent 0303244897
commit 93a2dda8c6
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
2 changed files with 26 additions and 12 deletions

View File

@ -18,9 +18,13 @@ impl IntoLua for Direction {
impl FromLua for Direction { impl FromLua for Direction {
fn from_lua(value: Value, _lua: &Lua) -> Result<Self> { fn from_lua(value: Value, _lua: &Lua) -> Result<Self> {
if let Value::Table(table) = value { if let Value::Table(table) = value {
Ok(Self { Ok(if let (Ok(x), Ok(y)) = (table.get(1), table.get(2)) {
x: table.get("x")?, Self { x, y }
y: table.get("y")?, } else {
Self {
x: table.get("x")?,
y: table.get("y")?,
}
}) })
} else { } else {
Err(mlua::Error::FromLuaConversionError { Err(mlua::Error::FromLuaConversionError {

View File

@ -19,18 +19,28 @@ impl IntoLua for Vec3 {
impl FromLua for Vec3 { impl FromLua for Vec3 {
fn from_lua(value: Value, _lua: &Lua) -> Result<Self> { fn from_lua(value: Value, _lua: &Lua) -> Result<Self> {
if let Value::Table(table) = value { match value {
Ok(Self { Value::Table(table) => Ok(
x: table.get("x")?, if let (Ok(x), Ok(y), Ok(z)) = (table.get(1), table.get(2), table.get(3)) {
y: table.get("y")?, Self { x, y, z }
z: table.get("z")?, } else {
}) Self {
} else { x: table.get("x")?,
Err(mlua::Error::FromLuaConversionError { y: table.get("y")?,
z: table.get("z")?,
}
},
),
Value::Vector(vector) => Ok(Self {
x: vector.x().into(),
y: vector.y().into(),
z: vector.z().into(),
}),
_ => Err(mlua::Error::FromLuaConversionError {
from: value.type_name(), from: value.type_name(),
to: "Vec3".to_string(), to: "Vec3".to_string(),
message: None, message: None,
}) }),
} }
} }
} }