123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- local function loadvar(varname)
- local ok, val = pcall(assert(loadstring('return site.' .. varname)))
- if ok then
- return val
- else
- return nil
- end
- end
- local function assert_type(var, t, msg)
- assert(type(var) == t, msg)
- end
- function need_string(varname, required)
- local var = loadvar(varname)
- if required == false and var == nil then
- return nil
- end
- assert_type(var, 'string', "site.conf error: expected `" .. varname .. "' to be a string")
- return var
- end
- function need_string_match(varname, pat, required)
- local var = need_string(varname, required)
- if not var then
- return nil
- end
- assert(var:match(pat), "site.conf error: expected `" .. varname .. "' to match pattern `" .. pat .. "'")
- return var
- end
- function need_number(varname, required)
- local var = loadvar(varname)
- if required == false and var == nil then
- return nil
- end
- assert_type(var, 'number', "site.conf error: expected `" .. varname .. "' to be a number")
- return var
- end
- function need_boolean(varname, required)
- local var = loadvar(varname)
- if required == false and var == nil then
- return nil
- end
- assert_type(var, 'boolean', "site.conf error: expected `" .. varname .. "' to be a boolean")
- return var
- end
- function need_array(varname, subcheck, required)
- local var = loadvar(varname)
- if required == false and var == nil then
- return nil
- end
- assert_type(var, 'table', "site.conf error: expected `" .. varname .. "' to be an array")
- for _, e in ipairs(var) do
- subcheck(e)
- end
- return var
- end
- function need_table(varname, subcheck, required)
- local var = loadvar(varname)
- if required == false and var == nil then
- return nil
- end
- assert_type(var, 'table', "site.conf error: expected `" .. varname .. "' to be a table")
- if subcheck then
- for k, v in pairs(var) do
- subcheck(k, v)
- end
- end
- return var
- end
- function need_string_array(varname, required)
- return assert(pcall(need_array, varname, function(e) assert_type(e, 'string') end, required),
- "site.conf error: expected `" .. varname .. "' to be a string array")
- end
|