nix2lua/lib.nix

63 lines
2 KiB
Nix
Raw Normal View History

2022-11-18 02:36:55 +03:00
let
2022-11-19 04:41:15 +03:00
inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath;
2022-11-18 12:40:18 +03:00
inherit (builtins) concatStringsSep filter mapAttrs attrValues;
2022-11-19 00:25:30 +03:00
mkLuaRaw = raw: { _type = "raw"; inherit raw; };
2022-11-19 04:41:15 +03:00
isLuaRaw = expr: getType expr == "raw";
2022-11-19 00:25:30 +03:00
2022-11-18 12:40:18 +03:00
mkLuaNil = { _type = "nil"; };
2022-11-19 04:41:15 +03:00
isLuaNil = expr: getType expr == "nil";
2022-11-18 22:46:17 +03:00
2022-11-19 04:41:15 +03:00
mkNamedField = name: expr: {
2022-11-19 03:43:29 +03:00
_type = "table_field";
2022-11-18 22:46:17 +03:00
name = validString name;
2022-11-19 04:41:15 +03:00
value = toLua expr;
2022-11-18 22:46:17 +03:00
};
2022-11-19 04:41:15 +03:00
isNamedField = expr: getType expr == "table_field";
toLuaNamedField = name: expr:
if isNull expr then null
else "[${toLuaStr name}] = ${expr}";
2022-11-18 02:36:55 +03:00
2022-11-19 03:43:29 +03:00
toLua = val: toLuaInternal 0 val;
2022-11-19 04:41:15 +03:00
toLuaInternal = depth: expr:
2022-11-19 03:43:29 +03:00
let nextDepth = depth + 1; in
2022-11-19 04:41:15 +03:00
if isLuaNil expr then "nil"
else if isLuaRaw expr then expr.raw
else if isNamedField expr then
if depth > 0 then toLuaNamedField expr.name expr.value
2022-11-19 03:43:29 +03:00
else error "You cannot render table field at the top level"
2022-11-19 04:41:15 +03:00
else if isAttrs expr then toLuaTable nextDepth expr
else if isList expr then toLuaList nextDepth expr
else if isString expr || isPath expr then toLuaStr expr
else if isFloat expr || isInt expr then toString expr
else if isBool expr then toLuaBool expr
else if isNull expr then null
else error "Value '${toString expr}' is not supported yet";
2022-11-18 12:40:18 +03:00
2022-11-19 04:41:15 +03:00
toLuaList = depth: expr:
wrapObj (excludeNull (map (toLuaInternal depth) expr));
2022-11-18 12:40:18 +03:00
2022-11-19 04:41:15 +03:00
toLuaTable = depth: expr: toLuaInternal depth (attrValues (mapAttrs mkNamedField expr));
2022-11-18 02:36:55 +03:00
2022-11-19 04:41:15 +03:00
excludeNull = expr: filter (v: !(isNull v)) expr;
2022-11-18 02:36:55 +03:00
2022-11-19 04:41:15 +03:00
wrapObj = expr: "{ ${concatStringsSep ", " expr} }";
2022-11-18 12:40:18 +03:00
2022-11-19 04:41:15 +03:00
toLuaStr = expr: "\"${validString expr}\"";
2022-11-18 02:36:55 +03:00
2022-11-19 04:41:15 +03:00
toLuaBool = expr: if expr then "true" else "false";
2022-11-18 12:40:18 +03:00
2022-11-19 04:41:15 +03:00
getType = expr: if isAttrs expr && expr ? _type then expr._type else null;
2022-11-18 22:46:17 +03:00
2022-11-19 04:41:15 +03:00
validString = expr:
if isString expr || isPath expr then toString expr
else error "Value '${toString expr}' is not a valid string";
2022-11-19 03:43:29 +03:00
error = message: throw "[nix2lua] ${message}";
2022-11-18 02:36:55 +03:00
in
{
inherit toLua;
2022-11-19 03:43:29 +03:00
inherit mkLuaNil mkLuaRaw mkNamedField;
2022-11-18 02:36:55 +03:00
}