2019-08-01 03:58:42 +02:00
|
|
|
use crate::object::{Primitive, TaggedDictBuilder, Value};
|
2019-06-11 08:26:03 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
fn from_node_to_value<'a, 'd>(n: &roxmltree::Node<'a, 'd>, tag: impl Into<Tag>) -> Tagged<Value> {
|
|
|
|
let tag = tag.into();
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-06-11 08:26:03 +02:00
|
|
|
if n.is_element() {
|
|
|
|
let name = n.tag_name().name().trim().to_string();
|
|
|
|
|
|
|
|
let mut children_values = vec![];
|
|
|
|
for c in n.children() {
|
2019-08-05 10:54:29 +02:00
|
|
|
children_values.push(from_node_to_value(&c, tag));
|
2019-06-11 08:26:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
let children_values: Vec<Tagged<Value>> = children_values
|
2019-06-11 08:26:03 +02:00
|
|
|
.into_iter()
|
|
|
|
.filter(|x| match x {
|
2019-08-01 03:58:42 +02:00
|
|
|
Tagged {
|
2019-07-09 06:31:26 +02:00
|
|
|
item: Value::Primitive(Primitive::String(f)),
|
|
|
|
..
|
|
|
|
} => {
|
2019-06-11 08:26:03 +02:00
|
|
|
if f.trim() == "" {
|
2019-06-15 20:36:17 +02:00
|
|
|
false
|
2019-06-11 08:26:03 +02:00
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
let mut collected = TaggedDictBuilder::new(tag);
|
2019-07-13 04:07:06 +02:00
|
|
|
collected.insert(name.clone(), Value::List(children_values));
|
2019-06-11 08:26:03 +02:00
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
collected.into_tagged_value()
|
2019-06-11 08:26:03 +02:00
|
|
|
} else if n.is_comment() {
|
2019-08-05 10:54:29 +02:00
|
|
|
Value::string("<comment>").tagged(tag)
|
2019-06-11 08:26:03 +02:00
|
|
|
} else if n.is_pi() {
|
2019-08-05 10:54:29 +02:00
|
|
|
Value::string("<processing_instruction>").tagged(tag)
|
2019-06-11 08:26:03 +02:00
|
|
|
} else if n.is_text() {
|
2019-08-05 10:54:29 +02:00
|
|
|
Value::string(n.text().unwrap()).tagged(tag)
|
2019-06-11 08:26:03 +02:00
|
|
|
} else {
|
2019-08-05 10:54:29 +02:00
|
|
|
Value::string("<unknown>").tagged(tag)
|
2019-06-11 08:26:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
fn from_document_to_value(d: &roxmltree::Document, tag: impl Into<Tag>) -> Tagged<Value> {
|
|
|
|
from_node_to_value(&d.root_element(), tag)
|
2019-06-11 08:26:03 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 06:31:26 +02:00
|
|
|
pub fn from_xml_string_to_value(
|
|
|
|
s: String,
|
2019-08-05 10:54:29 +02:00
|
|
|
tag: impl Into<Tag>,
|
2019-08-01 03:58:42 +02:00
|
|
|
) -> Result<Tagged<Value>, Box<dyn std::error::Error>> {
|
2019-06-16 01:03:49 +02:00
|
|
|
let parsed = roxmltree::Document::parse(&s)?;
|
2019-08-05 10:54:29 +02:00
|
|
|
Ok(from_document_to_value(&parsed, tag))
|
2019-06-11 08:26:03 +02:00
|
|
|
}
|
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
pub fn from_xml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once(registry)?;
|
|
|
|
let span = args.name_span();
|
2019-06-11 08:26:03 +02:00
|
|
|
let out = args.input;
|
|
|
|
Ok(out
|
2019-07-03 22:31:15 +02:00
|
|
|
.values
|
2019-08-05 10:54:29 +02:00
|
|
|
.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",
|
2019-07-03 22:31:15 +02:00
|
|
|
span,
|
2019-08-05 10:54:29 +02:00
|
|
|
"value originates from here",
|
|
|
|
a.span(),
|
2019-07-03 22:31:15 +02:00
|
|
|
)),
|
2019-08-05 10:54:29 +02:00
|
|
|
}
|
2019-06-11 08:26:03 +02:00
|
|
|
})
|
2019-07-03 22:31:15 +02:00
|
|
|
.to_output_stream())
|
2019-06-11 08:26:03 +02:00
|
|
|
}
|