neovim-flake/lib/lua.nix

48 lines
1.1 KiB
Nix
Raw Normal View History

# Helpers for converting values to lua
{lib}: rec {
# yes? no.
yesNo = value:
if value
then "yes"
else "no";
# Convert a null value to lua's nil
nullString = value:
if value == null
then "nil"
else "'${value}'";
2023-06-05 20:37:12 +02:00
# convert an expression to lua
2023-06-04 16:36:01 +02:00
expToLua = exp:
if builtins.isList exp
2023-07-30 10:41:52 +02:00
then listToLuaTable exp # if list, convert to lua table
2023-06-04 16:36:01 +02:00
else if builtins.isAttrs exp
2023-07-30 10:41:52 +02:00
then attrsetToLuaTable exp # if attrs, convert to table
else if builtins.isBool exp
then lib.boolToString exp # if bool, convert to string
else if builtins.isInt exp
then builtins.toString exp # if int, convert to string
else (builtins.toJSON exp); # otherwise jsonify the value and print as is
2023-06-04 16:36:01 +02:00
2023-06-05 20:37:12 +02:00
# convert list to a lua table
2023-06-04 16:36:01 +02:00
listToLuaTable = list:
"{ " + (builtins.concatStringsSep ", " (map expToLua list)) + " }";
2023-06-05 20:37:12 +02:00
# convert attrset to a lua table
attrsetToLuaTable = attrset:
"{ "
+ (
builtins.concatStringsSep ", "
2023-06-04 16:36:01 +02:00
(
lib.mapAttrsToList (
name: value:
2023-06-04 16:36:01 +02:00
name
+ " = "
+ (expToLua value)
)
attrset
2023-06-04 16:36:01 +02:00
)
)
+ " }";
}