Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/corecii/lua-enum-simple

A super-simple Enum-like object for Roblox Lua
https://github.com/corecii/lua-enum-simple

Last synced: 12 days ago
JSON representation

A super-simple Enum-like object for Roblox Lua

Awesome Lists containing this project

README

        

# LuaEnum

A super-simple enum type and a super-simple enum collection type.

[Documentation](https://corecii.github.io/lua-enum-simple/)

Install with [wally](https://wally.run):
```toml
# wally.toml
[dependencies]
LuaEnum = "corecii/[email protected]"
```

[or use a packaged release model](https://github.com/Corecii/lua-enum-simple/releases/latest)

---

Think Roblox's enums, not Rust's enums.

I couldn't find a good mix between serializability, debugability, and simplicity, so this module lets you choose between:
* "Id" representation, `"Animal.Dog"`
* "ValueId" representation, `"Animal.1"`
* "Name" representation, `"Dog"`
* "Value" representation, `1`
* "Symbol" representation, a userdata with `tostring(symbol) == ""`

It defaults to `Id` representation.

Additionally, this module provides a way to turn a tree of modulescripts and folders into a tree of EnumCollections and Enums.

---

LuaEnum Example:
```lua
local Animals = LuaEnum.new("Animals", {
Dog = 1,
Cat = 2,
Mouse = 3,
})

print(Animals.Dog) --> "Animals.Dog"

print(LuaEnum.is(Animals)) --> true

print(LuaEnum.isItem(Animals, Animals.Dog)) --> true
print(LuaEnum.isItem(Animals, "x")) --> false

local function speak(animal)
assert(LuaEnum.isItem(Animals, animal))

if animal == Animals.Dog then
print("woof")
elseif animal == Animals.Cat then
print("meow")
elseif animal == Animals.Mouse then
print("sque")
end
end

speak(Animals.Cat) --> "meow"

-- Errors:

local x = Animals.Lizard --> Error: Item "Lizard" does not exist in LuaEnum Animals

Animals.Cat = 5 --> Error: Attempt to modify a readonly table
```