nushell/crates/nu-cli/src/commands/from_xml.rs

326 lines
9.7 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
2019-06-11 08:26:03 +02:00
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
2019-06-11 08:26:03 +02:00
pub struct FromXML;
impl WholeStreamCommand for FromXML {
fn name(&self) -> &str {
"from-xml"
}
fn signature(&self) -> Signature {
Signature::build("from-xml")
}
fn usage(&self) -> &str {
"Parse text as .xml and create table."
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_xml(args, registry)
}
}
fn from_attributes_to_value(attributes: &[roxmltree::Attribute], tag: impl Into<Tag>) -> Value {
let tag = tag.into();
let mut collected = TaggedDictBuilder::new(tag);
for a in attributes {
collected.insert_untagged(String::from(a.name()), UntaggedValue::string(a.value()));
}
collected.into_value()
}
fn from_node_to_value<'a, 'd>(n: &roxmltree::Node<'a, 'd>, tag: impl Into<Tag>) -> 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() {
children_values.push(from_node_to_value(&c, &tag));
2019-06-11 08:26:03 +02:00
}
let children_values: Vec<Value> = children_values
2019-06-11 08:26:03 +02:00
.into_iter()
.filter(|x| match x {
Value {
value: UntaggedValue::Primitive(Primitive::String(f)),
2019-07-09 06:31:26 +02:00
..
} => {
!f.trim().is_empty() // non-whitespace characters?
2019-06-11 08:26:03 +02:00
}
_ => true,
})
.collect();
let mut collected = TaggedDictBuilder::new(&tag);
let attribute_value: Value = from_attributes_to_value(&n.attributes(), &tag);
let mut row = TaggedDictBuilder::new(&tag);
row.insert_untagged(
String::from("children"),
UntaggedValue::Table(children_values),
);
row.insert_untagged(String::from("attributes"), attribute_value);
collected.insert_untagged(name, row.into_value());
2019-06-11 08:26:03 +02:00
collected.into_value()
2019-06-11 08:26:03 +02:00
} else if n.is_comment() {
UntaggedValue::string("<comment>").into_value(tag)
2019-06-11 08:26:03 +02:00
} else if n.is_pi() {
UntaggedValue::string("<processing_instruction>").into_value(tag)
2019-06-11 08:26:03 +02:00
} else if n.is_text() {
match n.text() {
Some(text) => UntaggedValue::string(text).into_value(tag),
None => UntaggedValue::string("<error>").into_value(tag),
}
2019-06-11 08:26:03 +02:00
} else {
UntaggedValue::string("<unknown>").into_value(tag)
2019-06-11 08:26:03 +02:00
}
}
fn from_document_to_value(d: &roxmltree::Document, tag: impl Into<Tag>) -> Value {
from_node_to_value(&d.root_element(), tag)
2019-06-11 08:26:03 +02:00
}
pub fn from_xml_string_to_value(s: String, tag: impl Into<Tag>) -> Result<Value, roxmltree::Error> {
2019-06-16 01:03:49 +02:00
let parsed = roxmltree::Document::parse(&s)?;
Ok(from_document_to_value(&parsed, tag))
2019-06-11 08:26:03 +02:00
}
fn from_xml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
2019-07-24 00:22:11 +02:00
let args = args.evaluate_once(registry)?;
let tag = args.name_tag();
let name_span = tag.span;
2019-08-21 08:39:57 +02:00
let input = args.input;
let stream = async_stream! {
let values: Vec<Value> = input.values.collect().await;
2019-08-21 08:39:57 +02:00
let mut concat_string = String::new();
let mut latest_tag: Option<Tag> = None;
for value in values {
latest_tag = Some(value.tag.clone());
let value_span = value.tag.span;
if let Ok(s) = value.as_string() {
concat_string.push_str(&s);
}
else {
yield Err(ShellError::labeled_error_with_secondary(
"Expected a string from pipeline",
"requires string input",
name_span,
"value originates from here",
value_span,
))
}
2019-08-21 08:39:57 +02:00
}
match from_xml_string_to_value(concat_string, tag.clone()) {
2019-08-24 09:38:38 +02:00
Ok(x) => match x {
Value { value: UntaggedValue::Table(list), .. } => {
2019-08-24 09:38:38 +02:00
for l in list {
yield ReturnSuccess::value(l);
}
}
x => yield ReturnSuccess::value(x),
},
2019-08-21 08:39:57 +02:00
Err(_) => if let Some(last_tag) = latest_tag {
yield Err(ShellError::labeled_error_with_secondary(
"Could not parse as XML",
"input cannot be parsed as XML",
&tag,
2019-08-21 08:39:57 +02:00
"value originates from here",
&last_tag,
2019-08-21 08:39:57 +02:00
))
} ,
}
};
Ok(stream.to_output_stream())
2019-06-11 08:26:03 +02:00
}
2019-10-22 10:43:39 +02:00
#[cfg(test)]
mod tests {
use crate::commands::from_xml;
use indexmap::IndexMap;
use nu_protocol::{UntaggedValue, Value};
use nu_source::*;
2019-10-22 10:43:39 +02:00
fn string(input: impl Into<String>) -> Value {
UntaggedValue::string(input.into()).into_untagged_value()
2019-10-22 10:43:39 +02:00
}
fn row(entries: IndexMap<String, Value>) -> Value {
UntaggedValue::row(entries).into_untagged_value()
2019-10-22 10:43:39 +02:00
}
fn table(list: &[Value]) -> Value {
UntaggedValue::table(list).into_untagged_value()
2019-10-22 10:43:39 +02:00
}
fn parse(xml: &str) -> Result<Value, roxmltree::Error> {
from_xml::from_xml_string_to_value(xml.to_string(), Tag::unknown())
2019-10-22 10:43:39 +02:00
}
#[test]
fn parses_empty_element() -> Result<(), roxmltree::Error> {
2019-10-22 10:43:39 +02:00
let source = "<nu></nu>";
assert_eq!(
parse(source)?,
2019-10-22 10:43:39 +02:00
row(indexmap! {
"nu".into() => row(indexmap! {
"children".into() => table(&[]),
"attributes".into() => row(indexmap! {})
})
2019-10-22 10:43:39 +02:00
})
);
Ok(())
2019-10-22 10:43:39 +02:00
}
#[test]
fn parses_element_with_text() -> Result<(), roxmltree::Error> {
2019-10-22 10:43:39 +02:00
let source = "<nu>La era de los tres caballeros</nu>";
assert_eq!(
parse(source)?,
2019-10-22 10:43:39 +02:00
row(indexmap! {
"nu".into() => row(indexmap! {
"children".into() => table(&[string("La era de los tres caballeros")]),
"attributes".into() => row(indexmap! {})
})
2019-10-22 10:43:39 +02:00
})
);
Ok(())
2019-10-22 10:43:39 +02:00
}
#[test]
fn parses_element_with_elements() -> Result<(), roxmltree::Error> {
2019-10-22 10:43:39 +02:00
let source = "\
<nu>
<dev>Andrés</dev>
<dev>Jonathan</dev>
<dev>Yehuda</dev>
</nu>";
assert_eq!(
parse(source)?,
2019-10-22 10:43:39 +02:00
row(indexmap! {
"nu".into() => row(indexmap! {
"children".into() => table(&[
row(indexmap! {
"dev".into() => row(indexmap! {
"children".into() => table(&[string("Andrés")]),
"attributes".into() => row(indexmap! {})
})
}),
row(indexmap! {
"dev".into() => row(indexmap! {
"children".into() => table(&[string("Jonathan")]),
"attributes".into() => row(indexmap! {})
})
}),
row(indexmap! {
"dev".into() => row(indexmap! {
"children".into() => table(&[string("Yehuda")]),
"attributes".into() => row(indexmap! {})
})
})
]),
"attributes".into() => row(indexmap! {})
})
})
);
Ok(())
}
#[test]
fn parses_element_with_attribute() -> Result<(), roxmltree::Error> {
let source = "\
<nu version=\"2.0\">
</nu>";
assert_eq!(
parse(source)?,
row(indexmap! {
"nu".into() => row(indexmap! {
"children".into() => table(&[]),
"attributes".into() => row(indexmap! {
"version".into() => string("2.0")
})
})
})
);
Ok(())
}
#[test]
fn parses_element_with_attribute_and_element() -> Result<(), roxmltree::Error> {
let source = "\
<nu version=\"2.0\">
<version>2.0</version>
</nu>";
assert_eq!(
parse(source)?,
row(indexmap! {
"nu".into() => row(indexmap! {
"children".into() => table(&[
row(indexmap! {
"version".into() => row(indexmap! {
"children".into() => table(&[string("2.0")]),
"attributes".into() => row(indexmap! {})
})
})
]),
"attributes".into() => row(indexmap! {
"version".into() => string("2.0")
})
})
})
);
Ok(())
}
#[test]
fn parses_element_with_multiple_attributes() -> Result<(), roxmltree::Error> {
let source = "\
<nu version=\"2.0\" age=\"25\">
</nu>";
assert_eq!(
parse(source)?,
row(indexmap! {
"nu".into() => row(indexmap! {
"children".into() => table(&[]),
"attributes".into() => row(indexmap! {
"version".into() => string("2.0"),
"age".into() => string("25")
})
})
2019-10-22 10:43:39 +02:00
})
);
Ok(())
2019-10-22 10:43:39 +02:00
}
}