From b2c1379bb3f3d7387eb400508a1bf2c094347483 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Sun, 30 Oct 2022 12:27:56 +0400 Subject: [PATCH] add checks module --- lib/checks.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/checks.lua diff --git a/lib/checks.lua b/lib/checks.lua new file mode 100644 index 0000000..0e6daed --- /dev/null +++ b/lib/checks.lua @@ -0,0 +1,42 @@ +-- Copyright (c) 2006-2013 Fabien Fleutot and others. +-- +-- All rights reserved. + +checkers = { } + +local function check_one(expected, val) + if type(val)==expected then return true end + local mt = getmetatable(val) + if mt and mt.__type==expected then return true end + local f = checkers[expected] + if f and f(val) then return true end + return false +end + +local function check_many(_, expected, val) + if expected=='?' then return true + elseif expected=='!' then return (val~=nil) + elseif type(expected) ~= 'string' then + error 'strings expected by checks()' + elseif val==nil and expected :sub(1,1) == '?' then return true end + for one in expected :gmatch "[^|?]+" do + if check_one(one, val) then return true end + end + return false +end + +local function checks(...) + for i, arg in ipairs{...} do + local name, val = debug.getlocal(2, i) + local success = check_many(name, arg, val) + if not success then + local fname = debug.getinfo(2, 'n').name + local fmt = "bad argument #%d to '%s' (%s expected, got %s)" + local msg = string.format(fmt, i, fname or "?", arg, type(val)) + error(msg, 3) + end + end +end + +return checks +