Detect writing invalid sections

And display a proper error, instead of some random pairs-error
This commit is contained in:
Bart van Strien 2021-07-26 14:04:19 +02:00
parent 274bb1e0ce
commit 2acf400942

View File

@ -140,12 +140,22 @@ function inifile.save(name, t, backend)
table.insert(contents, ("%s=%s"):format(key, tostring(value))) table.insert(contents, ("%s=%s"):format(key, tostring(value)))
end end
local function tableLike(value)
local function index()
return value[1]
end
return pcall(index) and pcall(next, value)
end
local function writesection(section, order) local function writesection(section, order)
local s = t[section] local s = t[section]
-- Discard if it doesn't exist (anymore) -- Discard if it doesn't exist (anymore)
if not s then return end if not s then return end
table.insert(contents, ("[%s]"):format(section)) table.insert(contents, ("[%s]"):format(section))
assert(tableLike(s), ("Invalid section %s: not table-like"):format(section))
-- Write our comments out again, sadly we have only achieved -- Write our comments out again, sadly we have only achieved
-- section-accuracy so far -- section-accuracy so far
if comments and comments[section] then if comments and comments[section] then