mirror of
https://github.com/NotAShelf/nvf.git
synced 2025-09-06 10:21:31 +00:00
treewide: make lib calls explicit
This commit is contained in:
parent
a7531186a8
commit
974bfcc78e
56 changed files with 589 additions and 496 deletions
46
lib/lua.nix
46
lib/lua.nix
|
@ -1,7 +1,9 @@
|
|||
# Helpers for converting values to lua
|
||||
{lib}: let
|
||||
inherit (lib) mapAttrsToList filterAttrs concatStringsSep concatMapStringsSep stringToCharacters boolToString;
|
||||
inherit (builtins) hasAttr head throw typeOf;
|
||||
inherit (builtins) hasAttr head throw typeOf isList isAttrs isBool isInt isString isPath isFloat toJSON;
|
||||
inherit (lib.attrsets) mapAttrsToList filterAttrs;
|
||||
inherit (lib.strings) concatStringsSep concatMapStringsSep stringToCharacters;
|
||||
inherit (lib.trivial) boolToString;
|
||||
in rec {
|
||||
# Convert a null value to lua's nil
|
||||
nullString = value:
|
||||
|
@ -11,29 +13,29 @@ in rec {
|
|||
|
||||
# convert an expression to lua
|
||||
expToLua = exp:
|
||||
if builtins.isList exp
|
||||
if isList exp
|
||||
then listToLuaTable exp # if list, convert to lua table
|
||||
else if builtins.isAttrs exp
|
||||
else if isAttrs exp
|
||||
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 if isBool exp
|
||||
then boolToString exp # if bool, convert to string
|
||||
else if isInt exp
|
||||
then toString exp # if int, convert to string
|
||||
else if exp == null
|
||||
then "nil"
|
||||
else (builtins.toJSON exp); # otherwise jsonify the value and print as is
|
||||
else (toJSON exp); # otherwise jsonify the value and print as is
|
||||
|
||||
# convert list to a lua table
|
||||
listToLuaTable = list:
|
||||
"{ " + (builtins.concatStringsSep ", " (map expToLua list)) + " }";
|
||||
"{ " + (concatStringsSep ", " (map expToLua list)) + " }";
|
||||
|
||||
# convert attrset to a lua table
|
||||
attrsetToLuaTable = attrset:
|
||||
"{ "
|
||||
+ (
|
||||
builtins.concatStringsSep ", "
|
||||
concatStringsSep ", "
|
||||
(
|
||||
lib.mapAttrsToList (
|
||||
mapAttrsToList (
|
||||
name: value:
|
||||
name
|
||||
+ " = "
|
||||
|
@ -44,10 +46,10 @@ in rec {
|
|||
)
|
||||
+ " }";
|
||||
# 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}}'';
|
||||
luaTable = items: ''{${concatStringsSep "," items}}'';
|
||||
|
||||
toLuaObject = args:
|
||||
if builtins.isAttrs args
|
||||
if isAttrs args
|
||||
then
|
||||
if hasAttr "__raw" args
|
||||
then args.__raw
|
||||
|
@ -68,19 +70,19 @@ in rec {
|
|||
)
|
||||
args)))
|
||||
+ "}"
|
||||
else if builtins.isList args
|
||||
else if isList args
|
||||
then "{" + concatMapStringsSep "," toLuaObject args + "}"
|
||||
else if builtins.isString args
|
||||
else if 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
|
||||
toJSON args
|
||||
else if isPath args
|
||||
then toJSON (toString args)
|
||||
else if isBool args
|
||||
then "${boolToString args}"
|
||||
else if builtins.isFloat args
|
||||
else if isFloat args
|
||||
then "${toString args}"
|
||||
else if builtins.isInt args
|
||||
else if isInt args
|
||||
then "${toString args}"
|
||||
else if (args == null)
|
||||
then "nil"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue