nix-community.nixvim/lib/to-lua.nix

50 lines
1.4 KiB
Nix
Raw Normal View History

2024-05-05 19:39:35 +02:00
{ lib }:
with lib;
rec {
2024-01-25 15:43:06 +01:00
# Black functional magic that converts a bunch of different Nix types to their
# lua equivalents!
2024-05-05 19:39:35 +02:00
toLuaObject =
args:
if builtins.isAttrs args then
if hasAttr "__raw" args then
args.__raw
else if hasAttr "__empty" args then
"{ }"
2024-01-25 15:43:06 +01:00
else
"{"
2024-05-05 19:39:35 +02:00
+ (concatStringsSep "," (
mapAttrsToList (
n: v:
let
valueString = toLuaObject v;
in
if hasPrefix "__unkeyed" n then
valueString
else if hasPrefix "__rawKey__" n then
"[${n}] = " + valueString
2024-05-05 19:39:35 +02:00
else if n == "__emptyString" then
"[''] = " + valueString
2024-05-05 19:39:35 +02:00
else
"[${toLuaObject n}] = " + valueString
2024-05-05 19:39:35 +02:00
) (filterAttrs (n: v: v != null && (toLuaObject v != "{}")) args)
))
2024-01-25 15:43:06 +01:00
+ "}"
2024-05-05 19:39:35 +02:00
else if builtins.isList args then
"{" + concatMapStringsSep "," toLuaObject args + "}"
else if builtins.isString args then
2024-01-25 15:43:06 +01:00
# This should be enough!
builtins.toJSON args
2024-05-05 19:39:35 +02:00
else if builtins.isPath args then
builtins.toJSON (toString args)
else if builtins.isBool args then
"${boolToString args}"
else if builtins.isFloat args then
"${toString args}"
else if builtins.isInt args then
"${toString args}"
else if (args == null) then
"nil"
else
"";
2024-01-25 15:43:06 +01:00
}