2022-11-18 02:36:55 +03:00
|
|
|
let
|
2022-11-18 12:40:18 +03:00
|
|
|
inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull;
|
|
|
|
inherit (builtins) concatStringsSep filter mapAttrs attrValues;
|
|
|
|
|
|
|
|
mkLuaNil = { _type = "nil"; };
|
|
|
|
isLuaNil = val: isAttrs val && val ? _type && val._type == "nil";
|
2022-11-18 02:36:55 +03:00
|
|
|
|
|
|
|
toLua = val:
|
2022-11-18 12:40:18 +03:00
|
|
|
if isLuaNil val then "nil"
|
|
|
|
else if isAttrs val then toLuaDict val
|
|
|
|
else if isList val then toLuaList val
|
2022-11-18 02:36:55 +03:00
|
|
|
else if isString val then toLuaStr val
|
|
|
|
else if isFloat val || isInt val then toString val
|
|
|
|
else if isBool val then toLuaBool val
|
2022-11-18 12:40:18 +03:00
|
|
|
else if isNull val then null
|
|
|
|
else null;
|
|
|
|
|
|
|
|
toLuaList = val:
|
|
|
|
wrapObj (excludeNull (map toLua val));
|
|
|
|
|
|
|
|
toLuaDict = val:
|
|
|
|
let
|
|
|
|
toDictItem = name: value:
|
|
|
|
let luaValue = toLua value;
|
|
|
|
in
|
|
|
|
if isNull luaValue then null
|
|
|
|
else "[${toLuaStr name}] = ${luaValue}";
|
|
|
|
|
|
|
|
dictItems = excludeNull (attrValues (mapAttrs toDictItem val));
|
|
|
|
in
|
|
|
|
wrapObj dictItems;
|
2022-11-18 02:36:55 +03:00
|
|
|
|
2022-11-18 12:40:18 +03:00
|
|
|
excludeNull = val: filter (v: !(isNull v)) val;
|
2022-11-18 02:36:55 +03:00
|
|
|
|
2022-11-18 12:40:18 +03:00
|
|
|
wrapObj = val: "{ ${concatStringsSep ", " val} }";
|
|
|
|
|
|
|
|
toLuaStr = val: "\"${val}\"";
|
2022-11-18 02:36:55 +03:00
|
|
|
|
|
|
|
toLuaBool = val: if val then "true" else "false";
|
2022-11-18 12:40:18 +03:00
|
|
|
|
|
|
|
|
2022-11-18 02:36:55 +03:00
|
|
|
in
|
|
|
|
{
|
|
|
|
inherit toLua;
|
2022-11-18 12:40:18 +03:00
|
|
|
inherit mkLuaNil;
|
2022-11-18 02:36:55 +03:00
|
|
|
}
|