Add record support to lib.generators.toPreserves
This commit is contained in:
parent
079be99183
commit
bf64c36c22
|
@ -12,7 +12,7 @@
|
|||
|
||||
overlay = final: prev:
|
||||
with final; {
|
||||
lib = lib.extend libOverlay;
|
||||
lib = prev.lib.extend libOverlay;
|
||||
syndicate-rs = callPackage ./syndicate-rs {
|
||||
rust = rust-bin.nightly.latest.default;
|
||||
};
|
||||
|
|
24
lib.nix
24
lib.nix
|
@ -4,20 +4,32 @@ in with lib; {
|
|||
|
||||
generators = with final.generators;
|
||||
prev.generators // {
|
||||
|
||||
# Generates Preserves from an arbitrary value.
|
||||
# Records are generated for functions that take
|
||||
# a single formal argument `toRecord` and return
|
||||
# a list of values.
|
||||
toPreserves = { }@args:
|
||||
v:
|
||||
let concatItems = lib.strings.concatStringsSep " ";
|
||||
in if isAttrs v then
|
||||
let
|
||||
toPreserves' = toPreserves args;
|
||||
concatItems = toString;
|
||||
in v:
|
||||
if isAttrs v then
|
||||
"{ ${
|
||||
concatItems (lib.attrsets.mapAttrsToList
|
||||
(key: val: "${key}: ${toPreserves args val}") v)
|
||||
(key: val: "${key}: ${toPreserves' val}") v)
|
||||
} }"
|
||||
else if isList v then
|
||||
"[ ${concatItems (map (toPreserves args) v)} ]"
|
||||
"[ ${concatItems (map toPreserves' v)} ]"
|
||||
else if isBool v then
|
||||
(if v then "#t" else "#f")
|
||||
else if isFunction v then
|
||||
abort "generators.toPreserves: cannot convert a function to Preserves"
|
||||
(if (lib.functionArgs v) == { toRecord = false; } then
|
||||
let items = v { toRecord = null; };
|
||||
in "<${head items} ${toString (map toPreserves' (tail items))}>"
|
||||
else
|
||||
abort
|
||||
"generators.toPreserves: cannot convert a function to Preserves")
|
||||
else if isNull v then
|
||||
"null"
|
||||
else
|
||||
|
|
Loading…
Reference in New Issue