2019-06-11 08:26:03 +02:00
|
|
|
use crate::prelude::*;
|
2021-01-10 03:50:49 +01:00
|
|
|
use nu_engine::WholeStreamCommand;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2019-12-04 20:52:31 +01:00
|
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
2019-06-11 08:26:03 +02:00
|
|
|
|
2021-03-26 09:26:57 +01:00
|
|
|
pub struct FromXml;
|
2019-08-19 07:16:39 +02:00
|
|
|
|
2021-03-26 09:26:57 +01:00
|
|
|
impl WholeStreamCommand for FromXml {
|
2019-08-19 07:16:39 +02:00
|
|
|
fn name(&self) -> &str {
|
2020-05-04 10:44:33 +02:00
|
|
|
"from xml"
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-05-04 10:44:33 +02:00
|
|
|
Signature::build("from xml")
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
2019-08-30 00:52:32 +02:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse text as .xml and create table."
|
|
|
|
}
|
|
|
|
|
2021-04-12 04:35:01 +02:00
|
|
|
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
2021-04-06 18:19:43 +02:00
|
|
|
from_xml(args)
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
|
2020-01-25 17:16:40 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-01-01 03:13:59 +01:00
|
|
|
fn from_node_to_value(n: &roxmltree::Node, tag: impl Into<Tag>) -> Value {
|
2019-08-05 10:54:29 +02:00
|
|
|
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-10-13 06:12:43 +02:00
|
|
|
children_values.push(from_node_to_value(&c, &tag));
|
2019-06-11 08:26:03 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
let children_values: Vec<Value> = children_values
|
2019-06-11 08:26:03 +02:00
|
|
|
.into_iter()
|
|
|
|
.filter(|x| match x {
|
2019-11-21 15:33:14 +01:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Primitive(Primitive::String(f)),
|
2019-07-09 06:31:26 +02:00
|
|
|
..
|
|
|
|
} => {
|
2019-12-06 16:28:26 +01:00
|
|
|
!f.trim().is_empty() // non-whitespace characters?
|
2019-06-11 08:26:03 +02:00
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2020-01-25 17:16:40 +01:00
|
|
|
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
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
collected.into_value()
|
2019-06-11 08:26:03 +02:00
|
|
|
} else if n.is_comment() {
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::string("<comment>").into_value(tag)
|
2019-06-11 08:26:03 +02:00
|
|
|
} else if n.is_pi() {
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::string("<processing_instruction>").into_value(tag)
|
2019-06-11 08:26:03 +02:00
|
|
|
} else if n.is_text() {
|
2020-01-02 05:02:46 +01:00
|
|
|
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 {
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::string("<unknown>").into_value(tag)
|
2019-06-11 08:26:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
fn from_document_to_value(d: &roxmltree::Document, tag: impl Into<Tag>) -> Value {
|
2019-08-05 10:54:29 +02:00
|
|
|
from_node_to_value(&d.root_element(), tag)
|
2019-06-11 08:26:03 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01: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)?;
|
2019-08-05 10:54:29 +02:00
|
|
|
Ok(from_document_to_value(&parsed, tag))
|
2019-06-11 08:26:03 +02:00
|
|
|
}
|
|
|
|
|
2021-04-12 04:35:01 +02:00
|
|
|
fn from_xml(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
2021-04-06 18:19:43 +02:00
|
|
|
let args = args.evaluate_once()?;
|
2020-06-13 06:03:39 +02:00
|
|
|
let tag = args.name_tag();
|
|
|
|
let input = args.input;
|
2020-05-16 05:18:24 +02:00
|
|
|
|
2021-04-06 18:19:43 +02:00
|
|
|
let concat_string = input.collect_string(tag.clone())?;
|
2019-08-21 08:39:57 +02:00
|
|
|
|
2020-06-13 06:03:39 +02:00
|
|
|
Ok(
|
2020-03-06 17:06:39 +01:00
|
|
|
match from_xml_string_to_value(concat_string.item, tag.clone()) {
|
2019-08-24 09:38:38 +02:00
|
|
|
Ok(x) => match x {
|
2020-06-13 06:03:39 +02:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Table(list),
|
|
|
|
..
|
2021-04-06 18:19:43 +02:00
|
|
|
} => list
|
|
|
|
.into_iter()
|
|
|
|
.map(ReturnSuccess::value)
|
2021-04-12 04:35:01 +02:00
|
|
|
.to_action_stream(),
|
|
|
|
x => ActionStream::one(ReturnSuccess::value(x)),
|
2019-08-24 09:38:38 +02:00
|
|
|
},
|
2020-03-06 17:06:39 +01:00
|
|
|
Err(_) => {
|
2020-06-13 06:03:39 +02:00
|
|
|
return Err(ShellError::labeled_error_with_secondary(
|
2019-08-21 08:39:57 +02:00
|
|
|
"Could not parse as XML",
|
|
|
|
"input cannot be parsed as XML",
|
2019-10-13 06:12:43 +02:00
|
|
|
&tag,
|
2019-08-21 08:39:57 +02:00
|
|
|
"value originates from here",
|
2020-03-06 17:06:39 +01:00
|
|
|
&concat_string.tag,
|
2019-08-21 08:39:57 +02:00
|
|
|
))
|
2020-06-13 06:03:39 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-06-11 08:26:03 +02:00
|
|
|
}
|
2019-10-22 10:43:39 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2019-10-22 10:43:39 +02:00
|
|
|
use crate::commands::from_xml;
|
|
|
|
use indexmap::IndexMap;
|
2019-12-04 20:52:31 +01:00
|
|
|
use nu_protocol::{UntaggedValue, Value};
|
2019-11-21 15:33:14 +01:00
|
|
|
use nu_source::*;
|
2019-10-22 10:43:39 +02:00
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
fn string(input: impl Into<String>) -> Value {
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::string(input.into()).into_untagged_value()
|
2019-10-22 10:43:39 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
fn row(entries: IndexMap<String, Value>) -> Value {
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::row(entries).into_untagged_value()
|
2019-10-22 10:43:39 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 08:36:08 +01:00
|
|
|
fn table(list: &[Value]) -> Value {
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::table(list).into_untagged_value()
|
2019-10-22 10:43:39 +02:00
|
|
|
}
|
|
|
|
|
2020-01-02 05:02:46 +01: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]
|
2020-01-02 05:02:46 +01:00
|
|
|
fn parses_empty_element() -> Result<(), roxmltree::Error> {
|
2019-10-22 10:43:39 +02:00
|
|
|
let source = "<nu></nu>";
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-01-02 05:02:46 +01:00
|
|
|
parse(source)?,
|
2019-10-22 10:43:39 +02:00
|
|
|
row(indexmap! {
|
2020-01-25 17:16:40 +01:00
|
|
|
"nu".into() => row(indexmap! {
|
|
|
|
"children".into() => table(&[]),
|
|
|
|
"attributes".into() => row(indexmap! {})
|
|
|
|
})
|
2019-10-22 10:43:39 +02:00
|
|
|
})
|
|
|
|
);
|
2020-01-02 05:02:46 +01:00
|
|
|
|
|
|
|
Ok(())
|
2019-10-22 10:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-01-02 05:02:46 +01:00
|
|
|
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!(
|
2020-01-02 05:02:46 +01:00
|
|
|
parse(source)?,
|
2019-10-22 10:43:39 +02:00
|
|
|
row(indexmap! {
|
2020-01-25 17:16:40 +01:00
|
|
|
"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
|
|
|
})
|
|
|
|
);
|
2020-01-02 05:02:46 +01:00
|
|
|
|
|
|
|
Ok(())
|
2019-10-22 10:43:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-01-02 05:02:46 +01:00
|
|
|
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!(
|
2020-01-02 05:02:46 +01:00
|
|
|
parse(source)?,
|
2019-10-22 10:43:39 +02:00
|
|
|
row(indexmap! {
|
2020-01-25 17:16:40 +01:00
|
|
|
"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
|
|
|
})
|
|
|
|
);
|
2020-01-02 05:02:46 +01:00
|
|
|
|
|
|
|
Ok(())
|
2019-10-22 10:43:39 +02:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2021-03-26 09:26:57 +01:00
|
|
|
use super::FromXml;
|
2020-05-18 14:56:01 +02:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2021-03-26 09:26:57 +01:00
|
|
|
test_examples(FromXml {})
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
2019-10-22 10:43:39 +02:00
|
|
|
}
|