25
25
--- In case of name or value clashing, the call will fail. For this reason, it's
26
26
--- best if you define the members in ascending order.
27
27
--- @brief ]]
28
- local Enum = {}
28
+ --- @class PlenaryEnum
29
+ --- @field is_enum fun ( tbl : table ): boolean
30
+ --- @field make_enum fun ( tbl : PlenaryEnumRawTable ): PlenaryEnumEnum
31
+ --- @overload fun ( tbl : PlenaryEnumRawTable ): PlenaryEnumEnum
29
32
30
- --- @class Enum
33
+ --- @alias PlenaryEnumRawTable ( string |{ [1] : string , [2] : integer } ) []
31
34
32
- --- @class Variant
35
+ --- @class PlenaryEnumEnum
36
+ --- @field [ integer] string
37
+ --- @field [ string] PlenaryVariant
38
+ local Enum = {}
33
39
40
+ --- @param name string
41
+ --- @return string
34
42
local function validate_member_name (name )
35
43
if # name > 0 and name :sub (1 , 1 ):match " %u" then
36
44
return name
46
54
--- {'Qux', 10}
47
55
--- }
48
56
--- </pre>
49
- --- @return Enum : A new enum
57
+ --- @param tbl PlenaryEnumRawTable
58
+ --- @return PlenaryEnumEnum : A new enum
50
59
local function make_enum (tbl )
60
+ --- @type PlenaryEnumEnum
51
61
local enum = {}
52
62
63
+ --- @class PlenaryVariant
64
+ --- @field value integer
53
65
local Variant = {}
54
66
Variant .__index = Variant
55
67
68
+ --- @param i integer
69
+ --- @return PlenaryVariant
56
70
local function newVariant (i )
57
71
return setmetatable ({ value = i }, Variant )
58
72
end
59
73
60
74
-- we don't need __eq because the __eq metamethod will only ever be
61
75
-- invoked when they both have the same metatable
62
76
77
+ --- @param o PlenaryVariant
78
+ --- @return boolean
63
79
function Variant :__lt (o )
64
80
return self .value < o .value
65
81
end
66
82
83
+ --- @param o PlenaryVariant
84
+ --- @return boolean
67
85
function Variant :__gt (o )
68
86
return self .value > o .value
69
87
end
70
88
89
+ --- @return string
71
90
function Variant :__tostring ()
72
91
return tostring (self .value )
73
92
end
74
93
94
+ --- @param e PlenaryEnumEnum
95
+ --- @param i integer
96
+ --- @return integer
75
97
local function find_next_idx (e , i )
76
98
local newI = i + 1
77
99
if not e [newI ] then
@@ -112,6 +134,9 @@ local function make_enum(tbl)
112
134
return require (" plenary.tbl" ).freeze (setmetatable (enum , Enum ))
113
135
end
114
136
137
+ --- @param _ PlenaryEnumEnum
138
+ --- @param key string | integer
139
+ --- @return string | PlenaryVariant
115
140
Enum .__index = function (_ , key )
116
141
if Enum [key ] then
117
142
return Enum [key ]
@@ -120,8 +145,8 @@ Enum.__index = function(_, key)
120
145
end
121
146
122
147
--- Checks whether the enum has a member with the given name
123
- --- @param key string : The element to check for
124
- --- @return boolean : True if key is present
148
+ --- @param key string The element to check for
149
+ --- @return boolean has_key True if key is present
125
150
function Enum :has_key (key )
126
151
if rawget (getmetatable (self ).__index , key ) then
127
152
return true
@@ -130,17 +155,17 @@ function Enum:has_key(key)
130
155
end
131
156
132
157
--- If there is a member named 'key', return it, otherwise return nil
133
- --- @param key string : The element to check for
134
- --- @return Variant : The element named by key , or nil if not present
158
+ --- @param key string The element to check for
159
+ --- @return PlenaryVariant ? variant The element named by key, or nil if not present
135
160
function Enum :from_str (key )
136
161
if self :has_key (key ) then
137
162
return self [key ]
138
163
end
139
164
end
140
165
141
166
--- If there is a member of value 'num', return it, otherwise return nil
142
- --- @param num number : The value of the element to check for
143
- --- @return Variant : The element whose value is num
167
+ --- @param num integer The value of the element to check for
168
+ --- @return PlenaryVariant ? variant The element whose value is num
144
169
function Enum :from_num (num )
145
170
local key = self [num ]
146
171
if key then
@@ -149,8 +174,8 @@ function Enum:from_num(num)
149
174
end
150
175
151
176
--- Checks whether the given object corresponds to an instance of Enum
152
- --- @param tbl table : The object to be checked
153
- --- @return boolean : True if tbl is an Enum
177
+ --- @param tbl table The object to be checked
178
+ --- @return boolean is_enum True if tbl is an Enum
154
179
local function is_enum (tbl )
155
180
return getmetatable (getmetatable (tbl ).__index ) == Enum
156
181
end
@@ -159,4 +184,4 @@ return setmetatable({ is_enum = is_enum, make_enum = make_enum }, {
159
184
__call = function (_ , tbl )
160
185
return make_enum (tbl )
161
186
end ,
162
- })
187
+ }) --[[ @as PlenaryEnum ]]
0 commit comments