some cleanup

This commit is contained in:
Dmitriy Pleshevskiy 2024-02-28 23:40:47 +03:00
parent f152767145
commit e42d73e3ff
Signed by: pleshevskiy
GPG Key ID: 17041163DA10A9A2
1 changed files with 30 additions and 24 deletions

54
lib.nix
View File

@ -18,10 +18,23 @@ let
inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath; inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath;
inherit (builtins) concatStringsSep filter mapAttrs attrValues; inherit (builtins) concatStringsSep filter mapAttrs attrValues;
error = message: throw "[nix2lua] ${message}"; ################################################################################
# Utils
################################################################################
error = message: throw "[nix2lua] ${message}";
warn = msg: builtins.trace "[nix2lua] warning: ${msg}"; warn = msg: builtins.trace "[nix2lua] warning: ${msg}";
getType = expr: if isAttrs expr && expr ? _type then expr._type else null;
validString = expr:
if isString expr || isPath expr then toString expr
else error "Value '${toString expr}' is not a valid string";
################################################################################
# Low-Level
################################################################################
isJoin = expr: getType expr == "_join"; isJoin = expr: getType expr == "_join";
join = sep: expr: join = sep: expr:
if isList expr then { _type = "_join"; sep = validString sep; parts = expr; } if isList expr then { _type = "_join"; sep = validString sep; parts = expr; }
@ -31,7 +44,7 @@ let
mkLuaRaw = expr: mkLuaRaw = expr:
if isLuaRaw expr then if isLuaRaw expr then
{ _type = "raw"; raw = expr.raw; } { _type = "raw"; raw = expr.raw; }
else if isString expr || isLuaRaw expr then else if isString expr then
{ _type = "raw"; raw = expr; } { _type = "raw"; raw = expr; }
else else
error "Value '${toString expr}' is not supported for a raw type"; error "Value '${toString expr}' is not supported for a raw type";
@ -48,12 +61,23 @@ let
++ [ (mkLuaRaw ")") ] ++ [ (mkLuaRaw ")") ]
); );
toLuaStr = expr: "\"${validString expr}\"";
toLuaBool = expr: if expr then "true" else "false";
isLuaNil = expr: getType expr == "nil"; isLuaNil = expr: getType expr == "nil";
LuaNil = { _type = "nil"; }; LuaNil = { _type = "nil"; };
# DEPRECATED # DEPRECATED
mkLuaNil = warn "`mkLuaNil` is deprecated. Use `LuaNil` instead" LuaNil; mkLuaNil = warn "`mkLuaNil` is deprecated. Use `LuaNil` instead" LuaNil;
toLuaList = onValue: expr:
let
wrapObj = expr: "{ ${concatStringsSep ", " expr} }";
excludeNull = expr: filter (v: !(isNull v)) expr;
in
wrapObj (excludeNull (map onValue expr));
toLuaTable = onValue: expr: onValue (attrValues (mapAttrs mkNamedField expr));
mkNamedField = name: expr: { mkNamedField = name: expr: {
_type = "table_field"; _type = "table_field";
name = validString name; name = validString name;
@ -64,8 +88,6 @@ let
if isNull expr then null if isNull expr then null
else "[${toLuaStr name}] = ${expr}"; else "[${toLuaStr name}] = ${expr}";
toLua = val: toLuaInternal 0 val;
toLuaInternal = depth: expr: toLuaInternal = depth: expr:
let nextDepth = depth + 1; in let nextDepth = depth + 1; in
if isJoin expr then concatStringsSep expr.sep (map (toLuaInternal depth) expr.parts) if isJoin expr then concatStringsSep expr.sep (map (toLuaInternal depth) expr.parts)
@ -74,32 +96,16 @@ let
else if isNamedField expr then else if isNamedField expr then
if depth > 0 then toLuaNamedField expr.name expr.value if depth > 0 then toLuaNamedField expr.name expr.value
else error "You cannot render table field at the top level" else error "You cannot render table field at the top level"
else if isAttrs expr then toLuaTable nextDepth expr else if isAttrs expr then toLuaTable (toLuaInternal nextDepth) expr
else if isList expr then toLuaList nextDepth expr else if isList expr then toLuaList (toLuaInternal nextDepth) expr
else if isString expr || isPath expr then toLuaStr expr else if isString expr || isPath expr then toLuaStr expr
else if isFloat expr || isInt expr then toString expr else if isFloat expr || isInt expr then toString expr
else if isBool expr then toLuaBool expr else if isBool expr then toLuaBool expr
else if isNull expr then null else if isNull expr then null
else error "Value '${toString expr}' is not supported yet"; else error "Value '${toString expr}' is not supported yet";
toLuaList = depth: expr: toLua = val: toLuaInternal 0 val;
wrapObj (excludeNull (map (toLuaInternal depth) expr));
toLuaTable = depth: expr: toLuaInternal depth (attrValues (mapAttrs mkNamedField expr));
excludeNull = expr: filter (v: !(isNull v)) expr;
wrapObj = expr: "{ ${concatStringsSep ", " expr} }";
toLuaStr = expr: "\"${validString expr}\"";
toLuaBool = expr: if expr then "true" else "false";
getType = expr: if isAttrs expr && expr ? _type then expr._type else null;
validString = expr:
if isString expr || isPath expr then toString expr
else error "Value '${toString expr}' is not a valid string";
in in
{ {
# low-level # low-level