From 32e4e2614ae9211f69fa915188f0523be89559c4 Mon Sep 17 00:00:00 2001 From: Snorre Ettrup Altschul Date: Thu, 3 Apr 2025 01:20:42 +0200 Subject: [PATCH] added toXml function --- lib/default.nix | 44 ++++++++++++++++++++++++++++++++++++++++++++ lib/from_xml.nix | 7 ------- 2 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 lib/default.nix delete mode 100644 lib/from_xml.nix diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..d2bd15e --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,44 @@ +{nixpkgs, ...}: +nixpkgs.lib.extend ( + final: prev: { + toXMLGeneric = let + toXMLRecursive = + toXmlRecursive' + ""; + + toXmlRecursive' = str: xml: let + parseTag = str: xml: (builtins.concatStringsSep "" [ + str + "\n" + "<${xml.name}" + + ( + if (builtins.hasAttr "properties" xml) + then + (" " + + builtins.concatStringsSep " " (nixpkgs.lib.attrsets.mapAttrsToList + (name: value: "${name}=\"${nixpkgs.lib.strings.escapeXML value}\"") + xml.properties)) + else "" + ) + + ( + if builtins.hasAttr "content" xml + then ((toXmlRecursive' "" xml.content) + "\n") + 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; + in + toXMLRecursive; + } +) diff --git a/lib/from_xml.nix b/lib/from_xml.nix deleted file mode 100644 index a2a00f5..0000000 --- a/lib/from_xml.nix +++ /dev/null @@ -1,7 +0,0 @@ -{nixpkgs, ...}: -nixpkgs.lib.extend ( - final: prev: { - fromXml = { - }; - } -)