2021-09-02 20:12:54 +00:00
|
|
|
final: prev:
|
2022-09-14 20:24:11 +00:00
|
|
|
let lib = prev;
|
2021-09-02 20:12:54 +00:00
|
|
|
in with lib; {
|
|
|
|
|
2022-09-14 20:24:11 +00:00
|
|
|
generators =
|
2021-09-02 20:12:54 +00:00
|
|
|
prev.generators // {
|
2021-09-02 20:12:54 +00:00
|
|
|
|
2022-03-17 02:48:08 +00:00
|
|
|
/* Generates text-encoded Preserves from an arbitrary value.
|
|
|
|
|
|
|
|
Records are generated for lists with a final element in
|
|
|
|
the form of `{ record = «label»; }`.
|
|
|
|
|
|
|
|
Type: toPreserves :: a -> string
|
|
|
|
|
|
|
|
Example:
|
|
|
|
toPreserves { } [{ a = 0; b = 1; } "c" [ true false ] { record = "foo"; }]
|
|
|
|
=> "<foo { a: 0 b: 1 } \"c\" [ #t #f ]>"
|
|
|
|
*/
|
2021-09-02 20:12:54 +00:00
|
|
|
toPreserves = { }@args:
|
2021-09-02 20:12:54 +00:00
|
|
|
let
|
2022-09-14 20:24:11 +00:00
|
|
|
toPreserves' = final.generators.toPreserves args;
|
2021-09-02 20:12:54 +00:00
|
|
|
concatItems = toString;
|
2022-03-17 02:48:08 +00:00
|
|
|
recordLabel = list:
|
|
|
|
with builtins;
|
|
|
|
let len = length list;
|
|
|
|
in if len == 0 then
|
|
|
|
null
|
|
|
|
else
|
|
|
|
let end = elemAt list (len - 1);
|
|
|
|
in if (isAttrs end) && (attrNames end) == [ "record" ] then
|
|
|
|
end
|
|
|
|
else
|
|
|
|
null;
|
2021-09-02 20:12:54 +00:00
|
|
|
in v:
|
|
|
|
if isAttrs v then
|
2021-09-02 20:12:54 +00:00
|
|
|
"{ ${
|
|
|
|
concatItems (lib.attrsets.mapAttrsToList
|
2022-03-17 02:48:08 +00:00
|
|
|
(key: val: "${key}: ${toPreserves' val}") v)
|
2021-09-02 20:12:54 +00:00
|
|
|
} }"
|
|
|
|
else if isList v then
|
2022-03-17 02:48:08 +00:00
|
|
|
let label = recordLabel v;
|
|
|
|
in if label == null then
|
|
|
|
"[ ${concatItems (map toPreserves' v)} ]"
|
|
|
|
else
|
|
|
|
"<${label.record} ${
|
|
|
|
concatItems (map toPreserves' (lib.lists.init v))
|
|
|
|
}>"
|
2021-09-02 20:12:54 +00:00
|
|
|
else if isBool v then
|
|
|
|
(if v then "#t" else "#f")
|
|
|
|
else if isFunction v then
|
2022-03-17 02:48:08 +00:00
|
|
|
abort "generators.toPreserves: cannot convert a function to Preserves"
|
2021-09-02 20:12:54 +00:00
|
|
|
else if isNull v then
|
|
|
|
"null"
|
|
|
|
else
|
|
|
|
builtins.toJSON v;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|