generators: refactor toPLIST

This commit is contained in:
Matthew Bauer
2018-06-28 11:11:19 -04:00
parent 161414063f
commit d361371d23
4 changed files with 50 additions and 50 deletions

View File

@@ -176,51 +176,51 @@ rec {
else abort "toPretty: should never happen (v = ${v})";
# PLIST handling
toPLIST = {}: v: let
pprExpr = ind: x: with builtins;
if isNull x then "" else
if isBool x then pprBool ind x else
if isInt x then pprInt ind x else
if isString x then pprStr ind x else
if isList x then pprList ind x else
if isAttrs x then pprAttrs ind x else
abort "pprExpr: should never happen (v = ${v})";
toPLIST = x: ''
pprLiteral = ind: x: ind + x;
pprBool = ind: x: pprLiteral ind (if x then "<true/>" else "<false/>");
pprInt = ind: x: pprLiteral ind "<integer>${toString x}</integer>";
pprStr = ind: x: pprLiteral ind "<string>${x}</string>";
pprKey = ind: x: pprLiteral ind "<key>${x}</key>";
pprIndent = ind: pprExpr "\t${ind}";
pprItem = ind: libStr.concatMapStringsSep "\n" (pprIndent ind);
pprList = ind: x: libStr.concatStringsSep "\n" [
(pprLiteral ind "<array>")
(pprItem ind x)
(pprLiteral ind "</array>")
];
pprAttrs = ind: x: libStr.concatStringsSep "\n" [
(pprLiteral ind "<dict>")
(pprAttr ind x)
(pprLiteral ind "</dict>")
];
pprAttr = let attrFilter = name: value: name != "_module" && value != null;
in ind: x: libStr.concatStringsSep "\n" (lib.flatten (lib.mapAttrsToList
(name: value: lib.optional (attrFilter name value) [
(pprKey "\t${ind}" name)
(pprExpr "\t${ind}" value)
]) x));
in ''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
'' + pprExpr "" x
+ "\n</plist>";
pprExpr = ind: x: with builtins;
if isNull x then "" else
if isBool x then pprBool ind x else
if isInt x then pprInt ind x else
if isString x then pprStr ind x else
if isList x then pprList ind x else
if isAttrs x then pprAttrs ind x else
throw "invalid plist type";
pprLiteral = ind: x: ind + x;
pprBool = ind: x: pprLiteral ind (if x then "<true/>" else "<false/>");
pprInt = ind: x: pprLiteral ind "<integer>${toString x}</integer>";
pprStr = ind: x: pprLiteral ind "<string>${x}</string>";
pprKey = ind: x: pprLiteral ind "<key>${x}</key>";
pprIndent = ind: pprExpr "\t${ind}";
pprItem = ind: libStr.concatMapStringsSep "\n" (pprIndent ind);
pprList = ind: x: libStr.concatStringsSep "\n" [
(pprLiteral ind "<array>")
(pprItem ind x)
(pprLiteral ind "</array>")
];
pprAttrs = ind: x: libStr.concatStringsSep "\n" [
(pprLiteral ind "<dict>")
(pprAttr ind x)
(pprLiteral ind "</dict>")
];
attrFilter = name: value: name != "_module" && value != null;
pprAttr = ind: x: libStr.concatStringsSep "\n" (lib.flatten (lib.mapAttrsToList (name: value: lib.optional (attrFilter name value) [
(pprKey "\t${ind}" name)
(pprExpr "\t${ind}" value)
]) x));
${pprExpr "" v}
</plist>'';
}