2024-01-25 15:43:06 +01:00
|
|
|
{ lib }:
|
|
|
|
with lib;
|
|
|
|
rec {
|
2024-06-26 11:38:54 +01:00
|
|
|
# Whether the string is a reserved keyword in lua
|
|
|
|
isKeyword =
|
|
|
|
s:
|
|
|
|
elem s [
|
|
|
|
"and"
|
|
|
|
"break"
|
|
|
|
"do"
|
|
|
|
"else"
|
|
|
|
"elseif"
|
|
|
|
"end"
|
|
|
|
"false"
|
|
|
|
"for"
|
|
|
|
"function"
|
|
|
|
"if"
|
|
|
|
"in"
|
|
|
|
"local"
|
|
|
|
"nil"
|
|
|
|
"not"
|
|
|
|
"or"
|
|
|
|
"repeat"
|
|
|
|
"return"
|
|
|
|
"then"
|
|
|
|
"true"
|
|
|
|
"until"
|
|
|
|
"while"
|
|
|
|
];
|
|
|
|
|
|
|
|
# Valid lua identifiers are not reserved keywords, do not start with a digit,
|
|
|
|
# and contain only letters, digits, and underscores.
|
|
|
|
isIdentifier = s: !(isKeyword s) && (match "[A-Za-z_][0-9A-Za-z_]*" s) == [ ];
|
|
|
|
|
2024-01-25 15:43:06 +01:00
|
|
|
# Black functional magic that converts a bunch of different Nix types to their
|
|
|
|
# lua equivalents!
|
|
|
|
toLuaObject =
|
|
|
|
args:
|
|
|
|
if builtins.isAttrs args then
|
|
|
|
if hasAttr "__raw" args then
|
|
|
|
args.__raw
|
|
|
|
else if hasAttr "__empty" args then
|
|
|
|
"{ }"
|
|
|
|
else
|
|
|
|
"{"
|
|
|
|
+ (concatStringsSep "," (
|
|
|
|
mapAttrsToList (
|
|
|
|
n: v:
|
2024-05-31 10:00:53 +02:00
|
|
|
let
|
|
|
|
valueString = toLuaObject v;
|
|
|
|
in
|
|
|
|
if hasPrefix "__unkeyed" n then
|
|
|
|
valueString
|
|
|
|
else if hasPrefix "__rawKey__" n then
|
2024-05-31 23:13:03 +02:00
|
|
|
''[${removePrefix "__rawKey__" n}] = '' + valueString
|
2024-01-25 15:43:06 +01:00
|
|
|
else if n == "__emptyString" then
|
2024-05-31 10:00:53 +02:00
|
|
|
"[''] = " + valueString
|
2024-01-25 15:43:06 +01:00
|
|
|
else
|
2024-05-31 10:00:53 +02:00
|
|
|
"[${toLuaObject n}] = " + valueString
|
2024-01-25 15:43:06 +01:00
|
|
|
) (filterAttrs (n: v: v != null && (toLuaObject v != "{}")) args)
|
|
|
|
))
|
|
|
|
+ "}"
|
|
|
|
else if builtins.isList args then
|
|
|
|
"{" + concatMapStringsSep "," toLuaObject args + "}"
|
|
|
|
else if builtins.isString args then
|
|
|
|
# This should be enough!
|
|
|
|
builtins.toJSON args
|
|
|
|
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
|
|
|
|
"";
|
|
|
|
}
|