From 4f05ad2e207496c048e52e14986da83697013cf2 Mon Sep 17 00:00:00 2001 From: "C. Jeremiah Schneider" Date: Thu, 27 Jan 2022 12:31:15 +0100 Subject: [PATCH 1/2] add `xml_str_to_json` function this allows us to prevent unnecessary String allocations for something that is eventually turned into a slice anyway --- src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 8c85780..d00af0b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -376,6 +376,12 @@ pub fn xml_string_to_json(xml: String, config: &Config) -> Result Ok(xml_to_map(&root, config)) } +/// Converts the given XML string into `serde::Value` using settings from `Config` struct. +pub fn xml_str_to_json(xml: &str, config: &Config) -> Result { + let root = Element::from_str(xml)?; + Ok(xml_to_map(&root, config)) +} + /// Returns a tuple for Array and Value enforcements for the current node or /// `(false, JsonArray::Infer(JsonType::Infer)` if the current path is not found /// in the list of paths with custom config. From a95a0d834b01ff4a69f91b74efdb6ed7a840439c Mon Sep 17 00:00:00 2001 From: "C. Jeremiah Schneider" Date: Thu, 27 Jan 2022 20:59:04 +0100 Subject: [PATCH 2/2] implement `xml_string_to_json` with `sml_str_to_json` --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d00af0b..cabf505 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -372,8 +372,7 @@ fn xml_to_map(e: &Element, config: &Config) -> Value { /// Converts the given XML string into `serde::Value` using settings from `Config` struct. pub fn xml_string_to_json(xml: String, config: &Config) -> Result { - let root = Element::from_str(xml.as_str())?; - Ok(xml_to_map(&root, config)) + xml_str_to_json(xml.as_str(), config) } /// Converts the given XML string into `serde::Value` using settings from `Config` struct.