Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stefano-m/lua-enum
Enumerated Types for Lua
https://github.com/stefano-m/lua-enum
enum enumerated-types enumeration lua
Last synced: 1 day ago
JSON representation
Enumerated Types for Lua
- Host: GitHub
- URL: https://github.com/stefano-m/lua-enum
- Owner: stefano-m
- License: apache-2.0
- Created: 2017-10-31T20:55:56.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-09T12:43:41.000Z (almost 2 years ago)
- Last Synced: 2024-08-02T08:06:55.651Z (4 months ago)
- Topics: enum, enumerated-types, enumeration, lua
- Language: Lua
- Homepage: https://stefano-m.github.io/lua-enum/
- Size: 35.2 KB
- Stars: 22
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/stefano-m/lua-enum.svg?branch=master)](https://travis-ci.org/stefano-m/lua-enum) [![codecov](https://codecov.io/gh/stefano-m/lua-enum/branch/master/graph/badge.svg)](https://codecov.io/gh/stefano-m/lua-enum)
# Enum @VERSION@ - Simulate Enums in Lua
This is a little module that simulates [enumerated
types](https://en.wikipedia.org/wiki/Enumerated_type) in Lua.Its API is very similar to the [Python3 Enum
API](https://docs.python.org/3/library/enum.html), although much more limited.## Example Usage
``` lua
enum = require("enum")sizes = {"SMALL", "MEDIUM", "BIG"}
Size = enum.new("Size", sizes)
print(Size) -- ""
print(Size.SMALL) -- ""
print(Size.SMALL.name) -- "SMALL"
print(Size.SMALL.value) -- 1
assert(Size.SMALL ~= Size.BIG) -- true
assert(Size.SMALL < Size.BIG) -- error "Unsupported operation"
assert(Size[1] == Size.SMALL) -- true
Size[5] -- error "Invalid enum member: 5"-- Enums cannot be modified
Size.MINI -- error "Invalid enum: MINI"
assert(Size.BIG.something == nil) -- true
Size.MEDIUM.other = 1 -- error "Cannot set fields in enum value"-- Keys cannot be reused
Color = enum.new("Color", {"RED", "RED"}) -- error "Attempted to reuse key: 'RED'"
```# Installing
## Using "classic" nix
If you are on NixOS, you can install this package from
[nix-stefano-m-overlays](https://github.com/stefano-m/nix-stefano-m-nix-overlays).## Using nix flakes
This package can be installed as a [nix
flake](https://nixos.wiki/wiki/Flakes). It provides packages for the various
versions of Lua shipped with nix as well as an
[overlay](https://nixos.wiki/wiki/Overlays) that adds the `enum` attribute to
the Lua package sets.----------
**NOTE**
To ensure that the flake overlays are composable, `enum` is added *directly* to
the top-level `luaPackages`. An unfortunate consequence is that `enum` will
**not** be present in `lua.pkgs`. To use `enum` in `lua.withPackages`, one must
refer to the top-level `luaPackages`.For example, say that you want a Lua environment with `enum` and `http`, you
need to do something like:``` nix
let
# assume that enumOverlay is the overlay provided by the enum flake.
pkgs = import {overlays = [enumOverlay];};
in
# http is present in lua.pkgs
# enum is not present in lua.pkgs
myLuaEnv = pkgs.lua.withPackages(ps: [ps.http pkgs.luaPackages.enum])
```----------
## Using Luarocks
This package is [published to Luarocks as
`enum`](http://luarocks.org/modules/stefano-m/enum) and can be installed using```shell
luarocks install enum
```