From da9dd31f504d30f33922cdf362a7c01673a6b927 Mon Sep 17 00:00:00 2001 From: Alexander M Pickering Date: Sun, 19 Jan 2025 15:23:06 -0600 Subject: Last commit before theme release --- src/util.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/util.lua') diff --git a/src/util.lua b/src/util.lua index 52597b2..f0192dc 100644 --- a/src/util.lua +++ b/src/util.lua @@ -90,4 +90,16 @@ function util.reverse(tbl, val) return ret end +function util.typecheck(tbl, ...) + local args = {...} + assert(#args % 2 == 0,"Typecheck should have an odd number of arguments, found " .. tostring(#args + 1) .. ".") + for i = 1, #args, 2 do + assert(args[i] and type(args[i]) == "string", "Cannot check a field of type " .. type(args[i]) .. " at position " .. tostring(i + 1) .. ".") + assert(tbl[args[i]], "Failed to find a field: " .. args[i]) + assert(args[i+1] and type(args[i + 1]) == "string", "Cannot check for a type " .. type(args[i + 1]) .. " at position " .. tostring(i + 2) .. ".") + assert(type(tbl[args[i]]) == args[i+1], "Expected a " .. args[i+1] .. " at position " .. tostring(i+2) .. " but found a " .. type(tbl[args[i]])) + end + return true +end + return util -- cgit v1.2.3-70-g09d2