mirror of
https://github.com/unixtensor/Roblox-Elevator-Game.git
synced 2025-12-14 14:51:55 +00:00
new switch statement for Enums like Rust
This commit is contained in:
88
src/shared/Enum.lua
Normal file
88
src/shared/Enum.lua
Normal file
@@ -0,0 +1,88 @@
|
||||
--!optimize 2
|
||||
--!native
|
||||
--!strict
|
||||
|
||||
type EnumValue = any
|
||||
type EnumName = string
|
||||
type Enums = {[EnumName]: EnumValue}
|
||||
type EnumsMetadata = {
|
||||
__index: (self: EnumsMetadata, i: EnumName) -> EnumValue?,
|
||||
__newindex: (self: EnumsMetadata, i: EnumName, v: EnumValue) -> ()
|
||||
}
|
||||
|
||||
export type CustomEnums = {
|
||||
Enums: typeof(setmetatable({} :: Enums, {} :: EnumsMetadata))
|
||||
} & CustonEnumsFunctions
|
||||
|
||||
type CustonEnumsFunctions = {
|
||||
Create: <T>(Name: EnumName, EnumValue: T) -> T,
|
||||
Remove: (Name: EnumName) -> ()
|
||||
}
|
||||
|
||||
--type MatchList = {
|
||||
-- [EnumValue]
|
||||
--}
|
||||
|
||||
local CustomEnum = {} :: CustomEnums
|
||||
local EnumMeta = {} :: EnumsMetadata
|
||||
|
||||
function EnumMeta.__index(self, i)
|
||||
local get = rawget(self, i)
|
||||
if type(i) == "string" then
|
||||
return get
|
||||
end
|
||||
warn(`Enum: attempt to retrieve an unknown Enum "{i}".`, debug.traceback())
|
||||
return nil
|
||||
end
|
||||
|
||||
function EnumMeta.__newindex(self, i, v)
|
||||
local PossibleEnum = rawget(self, i)
|
||||
if not PossibleEnum then
|
||||
if type(i) == "string" then
|
||||
rawset(self, i, v)
|
||||
else
|
||||
error(`Enum: attempt to set an Enum but the requested name was not a string "{i}", type="{type(i)}".`, 2)
|
||||
end
|
||||
else
|
||||
error(`Enum: attempt to set an Enum but the name "{i}" is already registered.`, 2)
|
||||
end
|
||||
end
|
||||
|
||||
CustomEnum.Enums = setmetatable({}, EnumMeta)
|
||||
|
||||
local function EnumMethods(Enum)
|
||||
function Enum:Match(Result: string, MatchList) --Branch this out later
|
||||
local Return: any? = nil
|
||||
local b: boolean = false
|
||||
|
||||
for MatchEnumName, EnumFunc in MatchList do
|
||||
if MatchEnumName == Result then
|
||||
Return = EnumFunc()
|
||||
break
|
||||
end
|
||||
end
|
||||
if not Return and MatchList["_"] then
|
||||
Return = MatchList["_"]()
|
||||
end
|
||||
return Return
|
||||
end
|
||||
end
|
||||
|
||||
function CustomEnum.Create(Name, EnumValue)
|
||||
CustomEnum.Enums[Name] = EnumValue
|
||||
|
||||
local Enum = CustomEnum.Enums[Name]
|
||||
EnumMethods(Enum)
|
||||
|
||||
return Enum
|
||||
end
|
||||
|
||||
function CustomEnum.Remove(Name)
|
||||
if CustomEnum.Enums[Name] then
|
||||
CustomEnum.Enums[Name] = nil
|
||||
else
|
||||
warn(`Enum: attempt to remove an Enum that does not exist "{Name}".`, debug.traceback())
|
||||
end
|
||||
end
|
||||
|
||||
return CustomEnum
|
||||
@@ -42,7 +42,7 @@ end
|
||||
function Tags:Nuke()
|
||||
local Exports = self.Exports
|
||||
|
||||
for i,v in Exports do
|
||||
for i: string, v: Instance | {Instance} in Exports do
|
||||
if type(v) == "table" then
|
||||
for n: number = 1, #v do
|
||||
CS:RemoveTag(v[n], i)
|
||||
|
||||
Reference in New Issue
Block a user