util/adhoc.lua
author Kim Alvefur <zash@zash.se>
Thu, 20 Jan 2022 10:51:46 +0100
branch0.11
changeset 12206 ebeb4d959fb3
parent 8385 e5d00bf4a4d5
child 10671 49312378ba1d
permissions -rw-r--r--
util.xml: Deduplicate handlers for restricted XML Makes the code more like util.xmppstream, allowing easier comparisons if we ever need to apply fixes in the future.

-- luacheck: ignore 212/self

local function new_simple_form(form, result_handler)
	return function(self, data, state)
		if state then
			if data.action == "cancel" then
				return { status = "canceled" };
			end
			local fields, err = form:data(data.form);
			return result_handler(fields, err, data);
		else
			return { status = "executing", actions = {"next", "complete", default = "complete"}, form = form }, "executing";
		end
	end
end

local function new_initial_data_form(form, initial_data, result_handler)
	return function(self, data, state)
		if state then
			if data.action == "cancel" then
				return { status = "canceled" };
			end
			local fields, err = form:data(data.form);
			return result_handler(fields, err, data);
		else
			return { status = "executing", actions = {"next", "complete", default = "complete"},
				 form = { layout = form, values = initial_data(data) } }, "executing";
		end
	end
end

return { new_simple_form = new_simple_form,
	 new_initial_data_form = new_initial_data_form };