add support of deep raw

This commit is contained in:
Dmitriy Pleshevskiy 2024-02-28 15:25:21 +03:00
parent a45e0eee22
commit 828f2a3b8b
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
2 changed files with 13 additions and 1 deletions

10
lib.nix
View file

@ -22,8 +22,16 @@ let
warn = msg: builtins.trace "[nix2lua] warning: ${msg}"; warn = msg: builtins.trace "[nix2lua] warning: ${msg}";
mkLuaRaw = raw: { _type = "raw"; inherit raw; };
isLuaRaw = expr: getType expr == "raw"; isLuaRaw = expr: getType expr == "raw";
mkLuaRaw = expr:
if isLuaRaw expr then
{ _type = "raw"; raw = expr.raw; }
else if isString expr || isLuaRaw expr then
{ _type = "raw"; raw = expr; }
else
error "Value '${toString expr}' is not supported for a raw type";
# mkCall = fnName: args: mkLuaRaw
LuaNil = { _type = "nil"; }; LuaNil = { _type = "nil"; };
isLuaNil = expr: getType expr == "nil"; isLuaNil = expr: getType expr == "nil";

View file

@ -108,6 +108,10 @@ pkgs.lib.runTests {
expr = toLua (mkLuaRaw "hello"); expr = toLua (mkLuaRaw "hello");
expected = "hello"; expected = "hello";
}; };
"test returns deep raw string" = {
expr = toLua (mkLuaRaw (mkLuaRaw (mkLuaRaw "hello")));
expected = "hello";
};
"test returns path as string" = { "test returns path as string" = {
expr = toLua /foo/bar; expr = toLua /foo/bar;
expected = "\"/foo/bar\""; expected = "\"/foo/bar\"";