From d361371d236c90bfb550e827124b5350deeadbda Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Thu, 28 Jun 2018 11:11:19 -0400 Subject: generators: refactor toPLIST --- lib/generators.nix | 86 +++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 43 deletions(-) (limited to 'lib/generators.nix') diff --git a/lib/generators.nix b/lib/generators.nix index aab4498f9c6..14332981583 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -176,51 +176,51 @@ rec { else abort "toPretty: should never happen (v = ${v})"; # PLIST handling - - toPLIST = x: '' + 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})"; + + pprLiteral = ind: x: ind + x; + + pprBool = ind: x: pprLiteral ind (if x then "" else ""); + pprInt = ind: x: pprLiteral ind "${toString x}"; + pprStr = ind: x: pprLiteral ind "${x}"; + pprKey = ind: x: pprLiteral ind "${x}"; + + pprIndent = ind: pprExpr "\t${ind}"; + + pprItem = ind: libStr.concatMapStringsSep "\n" (pprIndent ind); + + pprList = ind: x: libStr.concatStringsSep "\n" [ + (pprLiteral ind "") + (pprItem ind x) + (pprLiteral ind "") + ]; + + pprAttrs = ind: x: libStr.concatStringsSep "\n" [ + (pprLiteral ind "") + (pprAttr ind x) + (pprLiteral ind "") + ]; + + 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 '' - '' + pprExpr "" x - + "\n"; - - 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 "" else ""); - pprInt = ind: x: pprLiteral ind "${toString x}"; - pprStr = ind: x: pprLiteral ind "${x}"; - pprKey = ind: x: pprLiteral ind "${x}"; - - pprIndent = ind: pprExpr "\t${ind}"; - - pprItem = ind: libStr.concatMapStringsSep "\n" (pprIndent ind); - - pprList = ind: x: libStr.concatStringsSep "\n" [ - (pprLiteral ind "") - (pprItem ind x) - (pprLiteral ind "") - ]; - - pprAttrs = ind: x: libStr.concatStringsSep "\n" [ - (pprLiteral ind "") - (pprAttr ind x) - (pprLiteral ind "") - ]; - - 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} + ''; } -- cgit 1.4.1