From 4a4f90b613dcf6015a067eb4455675ff6939329f Mon Sep 17 00:00:00 2001 From: Snorre Date: Thu, 3 Apr 2025 10:14:10 +0200 Subject: [PATCH] fixed issue with toXMLGeneric and also made it indented --- lib/default.nix | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index d2bd15e..45563de 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -4,13 +4,24 @@ nixpkgs.lib.extend ( toXMLGeneric = let toXMLRecursive = toXmlRecursive' - ""; + "\n" + 0; - toXmlRecursive' = str: xml: let - parseTag = str: xml: (builtins.concatStringsSep "" [ + indent = depth: ( + if (depth <= 0) + then "" + else (" " + (indent (depth - 1))) + ); + + toXmlRecursive' = str: depth: xml: let + # depth = if (builtins.isInt depth) then depth else (throw "DEPTH ISNT AN INT??? WTFFF"); + parseTag = str: depth: xml: (builtins.concatStringsSep "" [ str - "\n" - "<${xml.name}" + "${indent depth}<${xml.name}${ + if (builtins.hasAttr "content" xml) + then ">" + else " " + }" ( if (builtins.hasAttr "properties" xml) @@ -24,20 +35,20 @@ nixpkgs.lib.extend ( ( if builtins.hasAttr "content" xml - then ((toXmlRecursive' "" xml.content) + "\n") + then ((toXmlRecursive' "\n" (depth + 1) xml.content) + "") else "/>" ) ]); - output = - if (builtins.isAttrs xml) - then (parseTag str xml) - else if (builtins.isList xml) - then (builtins.concatStringsSep "\n" (builtins.map (x: (toXmlRecursive' "" x)) xml)) - else if ((builtins.isBool xml) || (builtins.isInt xml) || (builtins.isNull xml) || (builtins.isFloat xml)) - then (builtins.toString xml) - else nixpkgs.lib.abort "Cannot convert a ${builtins.typeOf xml} to XML"; in - output; + if (builtins.isAttrs xml) + then "${parseTag str depth xml}\n${indent (depth - 1)}" + else if (builtins.isList xml) + then "\n${(builtins.concatStringsSep "" (builtins.map (x: (toXmlRecursive' "" depth x)) xml))}${indent (depth - 1)}" + else if ((builtins.isBool xml) || (builtins.isInt xml) || (builtins.isNull xml) || (builtins.isFloat xml)) + then (builtins.toString xml) + else if (builtins.isString xml) + then xml + else throw "Cannot convert a ${builtins.typeOf xml} to XML"; in toXMLRecursive; }