to-edn/to-edn.nix

26 lines
651 B
Nix

{ lib }:
with lib;
let
isSymbol = str: isNull (builtins.match "^'.+" str);
stripQuote = str: head (builtins.match "^'(.+)" str);
parseStr = str: if (isSymbol str) then (stripQuote str) else ''"${str}"'';
join-str = concatStringsSep " ";
toEDN = ds:
if isString ds then
parseStr ds
else
(if isInt ds then
ds
else
(if isList ds then
"[ ${join-str (map toEDN ds)} ]"
else
(if isAttrs ds then
"{ ${join-str (mapAttrsToList (k: v: ":${k} ${toEDN v}") ds)} }"
else
throw "unsupported type: ${builtins.typeOf ds}: ${ds}")));
in ds: toEDN ds