2019-08-19 07:16:39 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-06-03 09:41:28 +02:00
|
|
|
use crate::prelude::*;
|
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;
|
2020-06-06 09:42:06 +02:00
|
|
|
use nu_protocol::{Primitive, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
2019-06-03 09:41:28 +02:00
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
pub struct FromYAML;
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2019-08-19 07:16:39 +02:00
|
|
|
impl WholeStreamCommand for FromYAML {
|
|
|
|
fn name(&self) -> &str {
|
2020-05-04 10:44:33 +02:00
|
|
|
"from yaml"
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-05-04 10:44:33 +02:00
|
|
|
Signature::build("from yaml")
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse text as .yaml/.yml and create table."
|
|
|
|
}
|
2019-08-29 05:53:45 +02:00
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
from_yaml(args).await
|
2019-08-29 05:53:45 +02:00
|
|
|
}
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
2019-08-29 05:53:45 +02:00
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
pub struct FromYML;
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2019-08-30 00:52:32 +02:00
|
|
|
impl WholeStreamCommand for FromYML {
|
2019-08-29 05:53:45 +02:00
|
|
|
fn name(&self) -> &str {
|
2020-05-04 10:44:33 +02:00
|
|
|
"from yml"
|
2019-08-29 05:53:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-05-04 10:44:33 +02:00
|
|
|
Signature::build("from yml")
|
2019-08-29 05:53:45 +02:00
|
|
|
}
|
2019-08-30 00:52:32 +02:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse text as .yaml/.yml and create table."
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
from_yaml(args).await
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
2019-08-29 05:53:45 +02:00
|
|
|
}
|
|
|
|
|
2020-01-04 07:44:17 +01:00
|
|
|
fn convert_yaml_value_to_nu_value(
|
|
|
|
v: &serde_yaml::Value,
|
|
|
|
tag: impl Into<Tag>,
|
|
|
|
) -> Result<Value, ShellError> {
|
2019-08-05 10:54:29 +02:00
|
|
|
let tag = tag.into();
|
2020-09-21 19:28:31 +02:00
|
|
|
let span = tag.span;
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2020-06-16 21:12:04 +02:00
|
|
|
let err_not_compatible_number = ShellError::labeled_error(
|
|
|
|
"Expected a compatible number",
|
|
|
|
"expected a compatible number",
|
|
|
|
&tag,
|
|
|
|
);
|
2020-01-04 07:44:17 +01:00
|
|
|
Ok(match v {
|
2019-12-04 20:52:31 +01:00
|
|
|
serde_yaml::Value::Bool(b) => UntaggedValue::boolean(*b).into_value(tag),
|
2019-06-03 09:41:28 +02:00
|
|
|
serde_yaml::Value::Number(n) if n.is_i64() => {
|
2020-11-22 01:37:16 +01:00
|
|
|
UntaggedValue::int(n.as_i64().ok_or(err_not_compatible_number)?).into_value(tag)
|
|
|
|
}
|
|
|
|
serde_yaml::Value::Number(n) if n.is_f64() => {
|
|
|
|
UntaggedValue::decimal_from_float(n.as_f64().ok_or(err_not_compatible_number)?, span)
|
|
|
|
.into_value(tag)
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
2019-12-04 20:52:31 +01:00
|
|
|
serde_yaml::Value::String(s) => UntaggedValue::string(s).into_value(tag),
|
2020-01-04 07:44:17 +01:00
|
|
|
serde_yaml::Value::Sequence(a) => {
|
|
|
|
let result: Result<Vec<Value>, ShellError> = a
|
|
|
|
.iter()
|
2019-10-13 06:12:43 +02:00
|
|
|
.map(|x| convert_yaml_value_to_nu_value(x, &tag))
|
2020-01-04 07:44:17 +01:00
|
|
|
.collect();
|
|
|
|
UntaggedValue::Table(result?).into_value(tag)
|
|
|
|
}
|
2019-06-03 09:41:28 +02:00
|
|
|
serde_yaml::Value::Mapping(t) => {
|
2019-10-13 06:12:43 +02:00
|
|
|
let mut collected = TaggedDictBuilder::new(&tag);
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-06-03 09:41:28 +02:00
|
|
|
for (k, v) in t.iter() {
|
2020-06-16 21:12:04 +02:00
|
|
|
// A ShellError that we re-use multiple times in the Mapping scenario
|
|
|
|
let err_unexpected_map = ShellError::labeled_error(
|
|
|
|
format!("Unexpected YAML:\nKey: {:?}\nValue: {:?}", k, v),
|
|
|
|
"unexpected",
|
|
|
|
tag.clone(),
|
|
|
|
);
|
|
|
|
match (k, v) {
|
|
|
|
(serde_yaml::Value::String(k), _) => {
|
2020-01-04 07:44:17 +01:00
|
|
|
collected.insert_value(k.clone(), convert_yaml_value_to_nu_value(v, &tag)?);
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
2020-06-16 21:12:04 +02:00
|
|
|
// Hard-code fix for cases where "v" is a string without quotations with double curly braces
|
|
|
|
// e.g. k = value
|
|
|
|
// value: {{ something }}
|
|
|
|
// Strangely, serde_yaml returns
|
|
|
|
// "value" -> Mapping(Mapping { map: {Mapping(Mapping { map: {String("something"): Null} }): Null} })
|
|
|
|
(serde_yaml::Value::Mapping(m), serde_yaml::Value::Null) => {
|
|
|
|
return m
|
|
|
|
.iter()
|
|
|
|
.take(1)
|
|
|
|
.collect_vec()
|
|
|
|
.first()
|
|
|
|
.and_then(|e| match e {
|
|
|
|
(serde_yaml::Value::String(s), serde_yaml::Value::Null) => Some(
|
|
|
|
UntaggedValue::string("{{ ".to_owned() + &s + " }}")
|
|
|
|
.into_value(tag),
|
|
|
|
),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.ok_or(err_unexpected_map);
|
|
|
|
}
|
|
|
|
(_, _) => {
|
|
|
|
return Err(err_unexpected_map);
|
|
|
|
}
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
collected.into_value()
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
2019-11-21 15:33:14 +01:00
|
|
|
serde_yaml::Value::Null => UntaggedValue::Primitive(Primitive::Nothing).into_value(tag),
|
2019-06-26 09:40:43 +02:00
|
|
|
x => unimplemented!("Unsupported yaml case: {:?}", x),
|
2020-01-04 07:44:17 +01:00
|
|
|
})
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
|
|
|
|
2020-01-04 07:44:17 +01:00
|
|
|
pub fn from_yaml_string_to_value(s: String, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
|
|
|
let tag = tag.into();
|
|
|
|
let v: serde_yaml::Value = serde_yaml::from_str(&s).map_err(|x| {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
format!("Could not load yaml: {}", x),
|
|
|
|
"could not load yaml from text",
|
|
|
|
&tag,
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Ok(convert_yaml_value_to_nu_value(&v, tag)?)
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn from_yaml(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once().await?;
|
2020-06-06 09:42:06 +02:00
|
|
|
let tag = args.name_tag();
|
|
|
|
let input = args.input;
|
|
|
|
|
|
|
|
let concat_string = input.collect_string(tag.clone()).await?;
|
|
|
|
|
|
|
|
match from_yaml_string_to_value(concat_string.item, tag.clone()) {
|
|
|
|
Ok(x) => match x {
|
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Table(list),
|
|
|
|
..
|
|
|
|
} => Ok(futures::stream::iter(list).to_output_stream()),
|
|
|
|
x => Ok(OutputStream::one(x)),
|
|
|
|
},
|
|
|
|
Err(_) => Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Could not parse as YAML",
|
|
|
|
"input cannot be parsed as YAML",
|
|
|
|
&tag,
|
|
|
|
"value originates from here",
|
|
|
|
&concat_string.tag,
|
|
|
|
)),
|
|
|
|
}
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2020-06-16 21:12:04 +02:00
|
|
|
use super::*;
|
2020-10-20 11:07:13 +02:00
|
|
|
use nu_protocol::row;
|
|
|
|
use nu_test_support::value::string;
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-05-18 14:56:01 +02:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2020-10-03 16:06:02 +02:00
|
|
|
Ok(test_examples(FromYAML {})?)
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
2020-06-16 21:12:04 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_problematic_yaml() {
|
|
|
|
struct TestCase {
|
|
|
|
description: &'static str,
|
|
|
|
input: &'static str,
|
|
|
|
expected: Result<Value, ShellError>,
|
|
|
|
}
|
|
|
|
let tt: Vec<TestCase> = vec![
|
|
|
|
TestCase {
|
|
|
|
description: "Double Curly Braces With Quotes",
|
|
|
|
input: r#"value: "{{ something }}""#,
|
|
|
|
expected: Ok(row!["value".to_owned() => string("{{ something }}")]),
|
|
|
|
},
|
|
|
|
TestCase {
|
|
|
|
description: "Double Curly Braces Without Quotes",
|
|
|
|
input: r#"value: {{ something }}"#,
|
|
|
|
expected: Ok(row!["value".to_owned() => string("{{ something }}")]),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
for tc in tt.into_iter() {
|
|
|
|
let actual = from_yaml_string_to_value(tc.input.to_owned(), Tag::default());
|
|
|
|
if actual.is_err() {
|
|
|
|
assert!(
|
|
|
|
tc.expected.is_err(),
|
|
|
|
"actual is Err for test:\nTest Description {}\nErr: {:?}",
|
|
|
|
tc.description,
|
|
|
|
actual
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
assert_eq!(actual, tc.expected, "{}", tc.description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|