nvf/lib/lua.nix
Gerg-L 3e48f13c3c
lib: rewrite toLuaObject, deprecate everything else (#1178)
* lint: typos

* wrapper/build: passthru mnw stuff

* lib: rewrite toLuaObject, deprecate everything else

* docs: deno fmt

* Emac

---------

Co-authored-by: Ching Pei Yang <59727193+horriblename@users.noreply.github.com>
2025-10-13 18:56:11 +02:00

52 lines
1.3 KiB
Nix

# Helpers for converting values to lua
{lib}: let
isLuaInline = object: (object._type or null) == "lua-inline";
toLuaObject = args:
{
int = toString args;
float = toString args;
# escapes \ and quotes
string = builtins.toJSON args;
path = builtins.toJSON args;
bool = lib.boolToString args;
null = "nil";
list = "{${lib.concatMapStringsSep ",\n" toLuaObject args}}";
set =
if lib.isDerivation args
then ''"${args}"''
else if isLuaInline args
then args.expr
else "{${
lib.pipe args [
(lib.filterAttrs (_: v: v != null))
(builtins.mapAttrs (
n: v:
if lib.hasPrefix "@" n
then toLuaObject v
else "[${toLuaObject n}] = ${toLuaObject v}"
))
builtins.attrValues
(lib.concatStringsSep ",\n")
]
}}";
}
.${
builtins.typeOf args
}
or (builtins.throw "Could not convert object of type `${builtins.typeOf args}` to lua object");
in
{
inherit isLuaInline toLuaObject;
luaTable = x: (toLuaObject (map lib.mkLuaInline x));
}
// lib.genAttrs [
"nullString"
"expToLua"
"listToLuaTable"
"attrsetToLuaTable"
] (name: lib.warn "${name} is deprecated use toLuaObject instead" toLuaObject)