lib/generators: use the explicit public interface pattern

This enables further refactoring without accidentally changing the public interface.
This commit is contained in:
Philip Taron 2024-03-06 16:34:08 -08:00
parent 3a01525ae7
commit 8422fe83b9
No known key found for this signature in database

View File

@ -37,6 +37,7 @@ let
replaceStrings replaceStrings
split split
tail tail
toJSON
typeOf typeOf
; ;
@ -77,9 +78,6 @@ let
assertMsg assertMsg
gvariant gvariant
; ;
in
rec {
## -- HELPER FUNCTIONS & DEFAULTS -- ## -- HELPER FUNCTIONS & DEFAULTS --
@ -302,19 +300,6 @@ rec {
# for details. # for details.
toDconfINI = toINI { mkKeyValue = mkDconfKeyValue; }; toDconfINI = toINI { mkKeyValue = mkDconfKeyValue; };
/* Generates JSON from an arbitrary (non-function) value.
* For more information see the documentation of the builtin.
*/
toJSON = {}: builtins.toJSON;
/* YAML has been a strict superset of JSON since 1.2, so we
* use toJSON. Before it only had a few differences referring
* to implicit typing rules, so it should work with older
* parsers as well.
*/
toYAML = toJSON;
withRecursion = withRecursion =
{ {
/* If this option is not null, the given value will stop evaluating at a certain depth */ /* If this option is not null, the given value will stop evaluating at a certain depth */
@ -496,7 +481,7 @@ ${expr "" v}
else if v == null then else if v == null then
abort "generators.toDhall: cannot convert a null to Dhall" abort "generators.toDhall: cannot convert a null to Dhall"
else else
builtins.toJSON v; toJSON v;
/* /*
Translate a simple Nix expression to Lua representation with occasional Translate a simple Nix expression to Lua representation with occasional
@ -566,7 +551,7 @@ ${expr "" v}
else if v == null then else if v == null then
"nil" "nil"
else if isInt v || isFloat v || isString v || isBool v then else if isInt v || isFloat v || isString v || isBool v then
builtins.toJSON v toJSON v
else if isList v then else if isList v then
(if v == [ ] then "{}" else (if v == [ ] then "{}" else
"{${introSpace}${concatItems (map (value: "${toLua innerArgs value}") v)}${outroSpace}}") "{${introSpace}${concatItems (map (value: "${toLua innerArgs value}") v)}${outroSpace}}")
@ -580,7 +565,7 @@ ${expr "" v}
''"${toString v}"'' ''"${toString v}"''
else else
"{${introSpace}${concatItems ( "{${introSpace}${concatItems (
mapAttrsToList (key: value: "[${builtins.toJSON key}] = ${toLua innerArgs value}") v mapAttrsToList (key: value: "[${toJSON key}] = ${toLua innerArgs value}") v
)}${outroSpace}}" )}${outroSpace}}"
) )
else else
@ -593,4 +578,37 @@ ${expr "" v}
mkLuaInline :: String -> AttrSet mkLuaInline :: String -> AttrSet
*/ */
mkLuaInline = expr: { _type = "lua-inline"; inherit expr; }; mkLuaInline = expr: { _type = "lua-inline"; inherit expr; };
in
# Everything in this attrset is the public interface of the file.
{
inherit
mkDconfKeyValue
mkKeyValueDefault
mkLuaInline
mkValueStringDefault
toDconfINI
toDhall
toGitINI
toINI
toINIWithGlobalSection
toKeyValue
toLua
toPlist
toPretty
withRecursion
;
/* Generates JSON from an arbitrary (non-function) value.
* For more information see the documentation of the builtin.
*/
toJSON = {}: toJSON;
/* YAML has been a strict superset of JSON since 1.2, so we
* use toJSON. Before it only had a few differences referring
* to implicit typing rules, so it should work with older
* parsers as well.
*/
toYAML = {}: toJSON;
} }