-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathenum.lua
More file actions
61 lines (50 loc) · 1.3 KB
/
enum.lua
File metadata and controls
61 lines (50 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
--
-- Each enum "value" is a table containing both a string label and an integer
-- value. When two enums are compared these labels and values are individually
-- compared.
--
local enum = {}
function enum.Enum(t)
local e = { _enums = {} }
for k, v in pairs(t) do
e._enums[k] = {
label = k,
value = v,
}
end
return setmetatable(e, {
-- Retrieve an enum entry by its string label
__index = function(table, key)
return rawget(table._enums, key)
end,
-- Retrieve an enum entry by integer value, or return the raw value if
-- it wasn't found in the enum
__call = function(table, value)
for k, v in pairs(table._enums) do
if v.value == value then
return v
end
end
return value
end,
__eq = function(lhs, rhs)
for k, v in pairs(lhs._enums) do
if v ~= rhs._enums[k] then
return false
end
end
return true
end
})
end
function enum.is_defined(v)
return type(v) == "table"
end
function enum.to_int(v)
if type(v) == "table" then
return v.value
else
return v
end
end
return enum