use crate::object::{Primitive, TaggedDictBuilder, Value}; use crate::prelude::*; fn from_node_to_value<'a, 'd>(n: &roxmltree::Node<'a, 'd>, tag: impl Into) -> Tagged { let tag = tag.into(); if n.is_element() { let name = n.tag_name().name().trim().to_string(); let mut children_values = vec![]; for c in n.children() { children_values.push(from_node_to_value(&c, tag)); } let children_values: Vec> = children_values .into_iter() .filter(|x| match x { Tagged { item: Value::Primitive(Primitive::String(f)), .. } => { if f.trim() == "" { false } else { true } } _ => true, }) .collect(); let mut collected = TaggedDictBuilder::new(tag); collected.insert(name.clone(), Value::List(children_values)); collected.into_tagged_value() } else if n.is_comment() { Value::string("").tagged(tag) } else if n.is_pi() { Value::string("").tagged(tag) } else if n.is_text() { Value::string(n.text().unwrap()).tagged(tag) } else { Value::string("").tagged(tag) } } fn from_document_to_value(d: &roxmltree::Document, tag: impl Into) -> Tagged { from_node_to_value(&d.root_element(), tag) } pub fn from_xml_string_to_value( s: String, tag: impl Into, ) -> Result, Box> { let parsed = roxmltree::Document::parse(&s)?; Ok(from_document_to_value(&parsed, tag)) } pub fn from_xml(args: CommandArgs) -> Result { let out = args.input; let span = args.call_info.name_span; Ok(out .values .map(move |a| { let value_tag = a.tag(); match a.item { Value::Primitive(Primitive::String(s)) => { match from_xml_string_to_value(s, value_tag) { Ok(x) => ReturnSuccess::value(x), Err(_) => Err(ShellError::labeled_error_with_secondary( "Could not parse as XML", "input cannot be parsed as XML", span, "value originates from here", value_tag.span, )), } } _ => Err(ShellError::labeled_error_with_secondary( "Expected a string from pipeline", "requires string input", span, "value originates from here", a.span(), )), } }) .to_output_stream()) }