2019-08-19 07:16:39 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-09-05 18:23:42 +02:00
|
|
|
use crate::data::{Primitive, TaggedDictBuilder, Value};
|
2019-06-03 09:41:28 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
pub struct FromYAML;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for FromYAML {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"from-yaml"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("from-yaml")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
from_yaml(args, registry)
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for FromYML {
|
2019-08-29 05:53:45 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"from-yml"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("from-yml")
|
|
|
|
}
|
2019-08-30 00:52:32 +02:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse text as .yaml/.yml and create table."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
from_yaml(args, registry)
|
|
|
|
}
|
2019-08-29 05:53:45 +02:00
|
|
|
}
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
fn convert_yaml_value_to_nu_value(v: &serde_yaml::Value, tag: impl Into<Tag>) -> Tagged<Value> {
|
|
|
|
let tag = tag.into();
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-06-03 09:41:28 +02:00
|
|
|
match v {
|
2019-09-01 18:20:31 +02:00
|
|
|
serde_yaml::Value::Bool(b) => Value::boolean(*b).tagged(tag),
|
2019-06-03 09:41:28 +02:00
|
|
|
serde_yaml::Value::Number(n) if n.is_i64() => {
|
2019-09-01 18:20:31 +02:00
|
|
|
Value::number(n.as_i64().unwrap()).tagged(tag)
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
|
|
|
serde_yaml::Value::Number(n) if n.is_f64() => {
|
2019-08-30 19:29:04 +02:00
|
|
|
Value::Primitive(Primitive::from(n.as_f64().unwrap())).tagged(tag)
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
2019-08-05 10:54:29 +02:00
|
|
|
serde_yaml::Value::String(s) => Value::string(s).tagged(tag),
|
2019-09-05 18:23:42 +02:00
|
|
|
serde_yaml::Value::Sequence(a) => Value::Table(
|
2019-06-03 09:41:28 +02:00
|
|
|
a.iter()
|
2019-10-13 06:12:43 +02:00
|
|
|
.map(|x| convert_yaml_value_to_nu_value(x, &tag))
|
2019-06-03 09:41:28 +02:00
|
|
|
.collect(),
|
2019-07-09 06:31:26 +02:00
|
|
|
)
|
2019-08-05 10:54:29 +02:00
|
|
|
.tagged(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() {
|
|
|
|
match k {
|
|
|
|
serde_yaml::Value::String(k) => {
|
2019-10-13 06:12:43 +02:00
|
|
|
collected.insert_tagged(k.clone(), convert_yaml_value_to_nu_value(v, &tag));
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
|
|
|
_ => unimplemented!("Unknown key type"),
|
|
|
|
}
|
|
|
|
}
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
collected.into_tagged_value()
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
2019-08-05 10:54:29 +02:00
|
|
|
serde_yaml::Value::Null => Value::Primitive(Primitive::Nothing).tagged(tag),
|
2019-06-26 09:40:43 +02:00
|
|
|
x => unimplemented!("Unsupported yaml case: {:?}", x),
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 06:31:26 +02:00
|
|
|
pub fn from_yaml_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
|
|
|
) -> serde_yaml::Result<Tagged<Value>> {
|
2019-06-16 01:03:49 +02:00
|
|
|
let v: serde_yaml::Value = serde_yaml::from_str(&s)?;
|
2019-08-05 10:54:29 +02:00
|
|
|
Ok(convert_yaml_value_to_nu_value(&v, tag))
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
|
|
|
|
2019-08-21 08:39:57 +02:00
|
|
|
fn from_yaml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once(registry)?;
|
2019-09-14 18:30:24 +02:00
|
|
|
let tag = args.name_tag();
|
2019-08-21 08:39:57 +02:00
|
|
|
let input = args.input;
|
2019-07-24 00:22:11 +02:00
|
|
|
|
2019-09-26 02:22:17 +02:00
|
|
|
let stream = async_stream! {
|
2019-08-21 08:39:57 +02:00
|
|
|
let values: Vec<Tagged<Value>> = input.values.collect().await;
|
|
|
|
|
|
|
|
let mut concat_string = String::new();
|
|
|
|
let mut latest_tag: Option<Tag> = None;
|
|
|
|
|
|
|
|
for value in values {
|
|
|
|
let value_tag = value.tag();
|
2019-10-13 06:12:43 +02:00
|
|
|
latest_tag = Some(value_tag.clone());
|
2019-08-21 08:39:57 +02:00
|
|
|
match value.item {
|
2019-08-01 03:58:42 +02:00
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
2019-08-21 08:39:57 +02:00
|
|
|
concat_string.push_str(&s);
|
|
|
|
concat_string.push_str("\n");
|
2019-08-01 03:58:42 +02:00
|
|
|
}
|
2019-08-21 08:39:57 +02:00
|
|
|
_ => yield Err(ShellError::labeled_error_with_secondary(
|
2019-08-05 10:54:29 +02:00
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-10-13 06:12:43 +02:00
|
|
|
&tag,
|
2019-08-05 10:54:29 +02:00
|
|
|
"value originates from here",
|
2019-10-13 06:12:43 +02:00
|
|
|
&value_tag,
|
2019-07-03 22:31:15 +02:00
|
|
|
)),
|
2019-08-21 08:39:57 +02:00
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
}
|
2019-08-21 08:39:57 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
match from_yaml_string_to_value(concat_string, tag.clone()) {
|
2019-08-24 09:38:38 +02:00
|
|
|
Ok(x) => match x {
|
2019-09-05 18:23:42 +02:00
|
|
|
Tagged { item: Value::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 YAML",
|
|
|
|
"input cannot be parsed as YAML",
|
2019-10-13 06:12:43 +02:00
|
|
|
&tag,
|
2019-08-21 08:39:57 +02:00
|
|
|
"value originates from here",
|
2019-10-13 06:12:43 +02:00
|
|
|
&last_tag,
|
2019-08-21 08:39:57 +02:00
|
|
|
))
|
|
|
|
} ,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|