Key Instance type checking

This commit is contained in:
VorTechnix 2022-05-23 17:06:23 -07:00
parent e17b2cce4e
commit 1cef968731

View file

@ -1,16 +1,42 @@
--- A container for transmitting axis, sign pairs within parsing functions. --- A container for transmitting (axis table, sign) or (dir, sign) pairs
-- and other data within parsing functions.
-- @class -- @class
local key_instance = {} local key_instance = {}
key_instance.__index = key_instance key_instance.__index = key_instance
key_instance.__name = "Key Instance" key_instance.__name = "Key Instance"
-- Allowed values for "type" field
local types = {
err = true, rev = true,
axis = true, dir = true,
replace = {
error = "err",
axes = "axis",
},
}
-- Simple function for putting stuff in quotes
local function enquote(take)
if type(take) == "string" then
return '"'..take..'"'
else return tostring(take) end
end
--- Creates a new Key Instance. --- Creates a new Key Instance.
-- @param: type: String: Key type (axis, direction, mirroring or error). -- This is a table with a "type" string, an entry string or table
-- and an optional signed integer (or code number in the case of errors)
-- @param: type: String: Key type (axis, dir(ection), rev (mirroring) or error).
-- @param: entry: String: The main content of the key. -- @param: entry: String: The main content of the key.
-- @param: sign: Int: The signed multiplier of the key (if any). -- @param: sign: Int: The signed multiplier of the key (if any).
-- @return: Key Instance: The new Key Instance. -- @return: Key Instance: The new Key Instance.
function key_instance.new(type,entry,sign) function key_instance.new(type,entry,sign)
if type == "error" then type = "err" end if types.replace[type] then
type = types.replace[type]
elseif not types[type] then
return key_instance.new("err",
"Key Instance internal error: Invalid type "..enquote(type)..".",
500)
end
local tbl = {type = type, entry = entry} local tbl = {type = type, entry = entry}
if sign and sign ~= 0 then if sign and sign ~= 0 then
if type == "err" then tbl.code = sign if type == "err" then tbl.code = sign
@ -46,11 +72,6 @@ function key_instance.is_error(a)
end end
function key_instance.__tostring(a) function key_instance.__tostring(a)
local function enquote(take)
if type(take) == "string" then
return '"'..take..'"'
else return tostring(take) end
end
local ret = "{type = "..enquote(a.type)..", entry = " local ret = "{type = "..enquote(a.type)..", entry = "
if type(a.entry) == "table" and #a.entry <= 3 then if type(a.entry) == "table" and #a.entry <= 3 then
ret = ret.."{" ret = ret.."{"