2023-04-18 02:01:45 +03:00
# Helpers for converting values to lua
2023-06-04 14:12:08 +03:00
{ lib }: rec {
# yes? no.
2023-04-18 02:01:45 +03:00
yesNo = value :
if value
then " y e s "
else " n o " ;
2023-06-04 14:12:08 +03:00
# Convert a null value to lua's nil
2023-04-18 02:01:45 +03:00
nullString = value :
if value = = null
then " n i l "
else " ' ${ value } ' " ;
2023-06-04 14:12:08 +03:00
2023-06-05 21:37:12 +03:00
# convert an expression to lua
2023-06-04 17:36:01 +03:00
expToLua = exp :
if builtins . isList exp
2023-07-30 11:41:52 +03:00
then listToLuaTable exp # if list, convert to lua table
2023-06-04 17:36:01 +03:00
else if builtins . isAttrs exp
2023-07-30 11:41:52 +03: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 17:36:01 +03:00
2023-06-05 21:37:12 +03:00
# convert list to a lua table
2023-06-04 17:36:01 +03:00
listToLuaTable = list :
" { " + ( builtins . concatStringsSep " , " ( map expToLua list ) ) + " } " ;
2023-06-04 14:12:08 +03:00
2023-06-05 21:37:12 +03:00
# convert attrset to a lua table
2023-06-04 14:12:08 +03:00
attrsetToLuaTable = attrset :
" { "
+ (
builtins . concatStringsSep " , "
2023-06-04 17:36:01 +03:00
(
lib . mapAttrsToList (
2023-06-04 14:12:08 +03:00
name : value :
2023-06-04 17:36:01 +03:00
name
+ " = "
+ ( expToLua value )
2023-06-04 14:12:08 +03:00
)
attrset
2023-06-04 17:36:01 +03:00
)
2023-06-04 14:12:08 +03:00
)
+ " } " ;
2023-10-24 09:18:44 +02:00
# Convert a list of lua expressions to a lua table. The difference to listToLuaTable is that the elements here are expected to be lua expressions already, whereas listToLuaTable converts from nix types to lua first
luaTable = items : '' { ${ builtins . concatStringsSep " , " items } } '' ;
2023-04-18 02:01:45 +03:00
}