diff options
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/clever_f/sequence_coordinator.lua | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/lua/clever_f/sequence_coordinator.lua b/lua/clever_f/sequence_coordinator.lua new file mode 100644 index 0000000..0da3ef3 --- /dev/null +++ b/lua/clever_f/sequence_coordinator.lua @@ -0,0 +1,65 @@ +local domain = require("clever_f.domain") + +local M = {} +local SequenceCoordinator = {} +SequenceCoordinator.__index = SequenceCoordinator +M.SequenceCoordinator = SequenceCoordinator + +local coordinator_records = setmetatable({}, { __mode = "k" }) + +local function fail(message, level) + error(message, (level or 1) + 1) +end + +local function descriptor_text(value) + if domain.Descriptor.is(value) then + return value.value + end + return tostring(value) +end + +function M.validate_primary_descriptor(value) + local descriptor = domain.Descriptor.try_from_string(value) + if descriptor == nil then + fail("clever-f: Invalid mapping '" .. descriptor_text(value) .. "'", 2) + end + return descriptor +end + +function SequenceCoordinator.new(options) + if SequenceCoordinator.is(options) then + return options + end + if type(options) ~= "table" then + fail("SequenceCoordinator options must be a table", 2) + end + local coordinator = setmetatable({}, SequenceCoordinator) + coordinator_records[coordinator] = { + host = options.host or options, + } + return coordinator +end + +function SequenceCoordinator.is(value) + return type(value) == "table" and coordinator_records[value] ~= nil +end + +function SequenceCoordinator:validate_primary_descriptor(value) + return M.validate_primary_descriptor(value) +end + +function SequenceCoordinator:primary(value) + return self:validate_primary_descriptor(value) +end + +function M.new(options) + return SequenceCoordinator.new(options) +end + +setmetatable(M, { + __call = function(_, options) + return SequenceCoordinator.new(options) + end, +}) + +return M |
