Refactor CFrame.new constructors to use a match on arg len (#102)

This commit is contained in:
Kenneth Loeffler 2023-09-16 19:28:44 -07:00 committed by GitHub
parent 210b64a86d
commit 67fe1d3e4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -104,41 +104,55 @@ impl LuaExportsTable<'_> for CFrame {
type ArgsPosXYZQuat = (f32, f32, f32, f32, f32, f32, f32); type ArgsPosXYZQuat = (f32, f32, f32, f32, f32, f32, f32);
type ArgsMatrix = (f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32); type ArgsMatrix = (f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32);
let cframe_new = |lua, args: LuaMultiValue| { let cframe_new = |lua, args: LuaMultiValue| match args.len() {
if args.clone().into_vec().is_empty() { 0 => Ok(CFrame(Mat4::IDENTITY)),
Ok(CFrame(Mat4::IDENTITY))
} else if let Ok(pos) = ArgsPos::from_lua_multi(args.clone(), lua) { 1 => match ArgsPos::from_lua_multi(args, lua) {
Ok(CFrame(Mat4::from_translation(pos.0))) Ok(pos) => Ok(CFrame(Mat4::from_translation(pos.0))),
} else if let Ok((from, to, up)) = ArgsLook::from_lua_multi(args.clone(), lua) { Err(err) => Err(err),
Ok(CFrame(look_at( },
from.0,
to.0, 3 => {
up.as_deref().unwrap_or(&Vector3(Vec3::Y)).0, if let Ok((from, to, up)) = ArgsLook::from_lua_multi(args.clone(), lua) {
))) Ok(CFrame(look_at(
} else if let Ok((x, y, z)) = ArgsPosXYZ::from_lua_multi(args.clone(), lua) { from.0,
Ok(CFrame(Mat4::from_translation(Vec3::new(x, y, z)))) to.0,
} else if let Ok((x, y, z, qx, qy, qz, qw)) = up.as_deref().unwrap_or(&Vector3(Vec3::Y)).0,
ArgsPosXYZQuat::from_lua_multi(args.clone(), lua) )))
{ } else if let Ok((x, y, z)) = ArgsPosXYZ::from_lua_multi(args, lua) {
Ok(CFrame(Mat4::from_rotation_translation( Ok(CFrame(Mat4::from_translation(Vec3::new(x, y, z))))
} else {
// TODO: Make this error message better
Err(LuaError::RuntimeError(
"Invalid arguments to constructor".to_string(),
))
}
}
7 => match ArgsPosXYZQuat::from_lua_multi(args, lua) {
Ok((x, y, z, qx, qy, qz, qw)) => Ok(CFrame(Mat4::from_rotation_translation(
Quat::from_array([qx, qy, qz, qw]), Quat::from_array([qx, qy, qz, qw]),
Vec3::new(x, y, z), Vec3::new(x, y, z),
))) ))),
} else if let Ok((x, y, z, r00, r01, r02, r10, r11, r12, r20, r21, r22)) = Err(err) => Err(err),
ArgsMatrix::from_lua_multi(args, lua) },
{
Ok(CFrame(Mat4::from_cols_array_2d(&[ 12 => match ArgsMatrix::from_lua_multi(args, lua) {
[r00, r01, r02, 0.0], Ok((x, y, z, r00, r01, r02, r10, r11, r12, r20, r21, r22)) => {
[r10, r11, r12, 0.0], Ok(CFrame(Mat4::from_cols_array_2d(&[
[r20, r21, r22, 0.0], [r00, r01, r02, 0.0],
[x, y, z, 1.0], [r10, r11, r12, 0.0],
]))) [r20, r21, r22, 0.0],
} else { [x, y, z, 1.0],
// FUTURE: Better error message here using given arg types ])))
Err(LuaError::RuntimeError( }
"Invalid arguments to constructor".to_string(), Err(err) => Err(err),
)) },
}
_ => Err(LuaError::RuntimeError(format!(
"Invalid number of arguments: expected 0, 1, 3, 7, or 12, got {}",
args.len()
))),
}; };
TableBuilder::new(lua)? TableBuilder::new(lua)?