mirror of
https://github.com/MezzalunaWM/Mezzaluna.git
synced 2026-03-07 19:49:53 -05:00
26 lines
857 B
Zig
26 lines
857 B
Zig
const LuaUtils = @This();
|
|
|
|
const std = @import("std");
|
|
const zlua = @import("zlua");
|
|
|
|
pub fn coerceNumber(comptime x: type, number: zlua.Number) error{InvalidNumber}!x {
|
|
if (number < std.math.minInt(x) or number > std.math.maxInt(x) or std.math.isNan(number)) {
|
|
return error.InvalidNumber;
|
|
}
|
|
switch (@typeInfo(x)) {
|
|
.int => return @as(x, @intFromFloat(number)),
|
|
.float => return @floatCast(number),
|
|
else => @compileError("unsupported type"),
|
|
}
|
|
}
|
|
|
|
pub fn coerceInteger(comptime x: type, number: zlua.Integer) error{InvalidInteger}!x {
|
|
if (number < std.math.minInt(x) or number > std.math.maxInt(x) or std.math.isNan(number)) {
|
|
return error.InvalidInteger;
|
|
}
|
|
switch (@typeInfo(x)) {
|
|
.int => return @intCast(number),
|
|
.float => return @as(x, @floatFromInt(number)),
|
|
else => @compileError("unsupported type"),
|
|
}
|
|
}
|