Fix naming, codestyle and few errors; Change Ensure; Use Ensure in Table

This commit is contained in:
2025-03-07 03:04:02 +07:00
parent 7fa2442a76
commit a0bc53af4f
3 changed files with 116 additions and 84 deletions

View File

@@ -1,9 +1,26 @@
local tostring = tostring
Ensure = {}
--- Validates a function argument's type and throws a descriptive error on mismatch.
--- @param variable any The argument value to type-check
--- @param expected_type string Expected Lua type name (e.g., "number", "table", "string")
--- @param arg any The argument value to type-check
--- @param expectedType string Expected Lua type name (e.g., "number", "table", "string")
--- @param n number Argument position in function call (1-based index)
--- @param func string Name of the parent function for error reporting
function ensure_arg_type(variable, expected_type, n, func)
local actual_type = type(variable)
assert(actual_type == expected_type, 'bad argument #%s to \'%s\' (%s expected, got %s)', tostring(n), func, expected_type, actual_type)
--- @param function_name string Name of the parent function for error reporting
function Ensure.argType(arg, expectedType, n, function_name)
local actualType = type(arg)
assert(actualType == expectedType, 'bad argument #%s to \'%s\' (%s expected, got %s)', tostring(n), function_name, expectedType, actualType)
end
--- @param arg any The argument value to type-check
--- @param n number Argument position in function call (1-based index)
--- @param function_name string Name of the parent function for error reporting
function Ensure.argNotNil(arg, n, function_name)
assert(arg ~= nil, 'bad argument #%s to \'%s\' (key cannot be nil)', n, function_name)
end
function Ensure.zeroDivision(divider, function_name)
assert(divider ~= 0, 'Division by zero in \'%s\'', function_name)
end