2019-06-03 09:41:28 +02:00
|
|
|
use crate::object::base::OF64;
|
2019-08-01 03:58:42 +02:00
|
|
|
use crate::object::{Primitive, TaggedDictBuilder, Value};
|
2019-06-03 09:41:28 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
fn convert_yaml_value_to_nu_value(v: &serde_yaml::Value, span: impl Into<Span>) -> Tagged<Value> {
|
2019-07-09 06:31:26 +02:00
|
|
|
let span = span.into();
|
|
|
|
|
2019-06-03 09:41:28 +02:00
|
|
|
match v {
|
2019-08-01 03:58:42 +02:00
|
|
|
serde_yaml::Value::Bool(b) => Value::Primitive(Primitive::Boolean(*b)).tagged(span),
|
2019-06-03 09:41:28 +02:00
|
|
|
serde_yaml::Value::Number(n) if n.is_i64() => {
|
2019-08-01 03:58:42 +02:00
|
|
|
Value::Primitive(Primitive::Int(n.as_i64().unwrap())).tagged(span)
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
|
|
|
serde_yaml::Value::Number(n) if n.is_f64() => {
|
2019-08-01 03:58:42 +02:00
|
|
|
Value::Primitive(Primitive::Float(OF64::from(n.as_f64().unwrap()))).tagged(span)
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
2019-08-01 03:58:42 +02:00
|
|
|
serde_yaml::Value::String(s) => Value::string(s).tagged(span),
|
2019-06-03 09:41:28 +02:00
|
|
|
serde_yaml::Value::Sequence(a) => Value::List(
|
|
|
|
a.iter()
|
2019-07-09 06:31:26 +02:00
|
|
|
.map(|x| convert_yaml_value_to_nu_value(x, span))
|
2019-06-03 09:41:28 +02:00
|
|
|
.collect(),
|
2019-07-09 06:31:26 +02:00
|
|
|
)
|
2019-08-01 03:58:42 +02:00
|
|
|
.tagged(span),
|
2019-06-03 09:41:28 +02:00
|
|
|
serde_yaml::Value::Mapping(t) => {
|
2019-08-01 03:58:42 +02:00
|
|
|
let mut collected = TaggedDictBuilder::new(span);
|
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-08-01 03:58:42 +02:00
|
|
|
collected.insert_tagged(k.clone(), convert_yaml_value_to_nu_value(v, span));
|
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-01 03:58:42 +02:00
|
|
|
serde_yaml::Value::Null => Value::Primitive(Primitive::Nothing).tagged(span),
|
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,
|
|
|
|
span: impl Into<Span>,
|
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-07-09 06:31:26 +02:00
|
|
|
Ok(convert_yaml_value_to_nu_value(&v, span))
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_yaml(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
let out = args.input;
|
2019-07-20 04:27:10 +02:00
|
|
|
let span = args.call_info.name_span;
|
2019-06-03 09:41:28 +02:00
|
|
|
Ok(out
|
2019-07-03 22:31:15 +02:00
|
|
|
.values
|
2019-08-01 03:58:42 +02:00
|
|
|
.map(move |a| {
|
|
|
|
let value_span = a.span();
|
|
|
|
match a.item {
|
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
|
|
|
match from_yaml_string_to_value(s, value_span) {
|
|
|
|
Ok(x) => ReturnSuccess::value(x),
|
|
|
|
Err(_) => Err(ShellError::maybe_labeled_error(
|
|
|
|
"Could not parse as YAML",
|
|
|
|
"piped data failed YAML parse",
|
|
|
|
span,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Err(ShellError::maybe_labeled_error(
|
|
|
|
"Expected string values from pipeline",
|
|
|
|
"expects strings from pipeline",
|
2019-07-03 22:31:15 +02:00
|
|
|
span,
|
|
|
|
)),
|
2019-08-01 03:58:42 +02:00
|
|
|
}
|
2019-06-03 09:41:28 +02:00
|
|
|
})
|
2019-07-03 22:31:15 +02:00
|
|
|
.to_output_stream())
|
2019-06-03 09:41:28 +02:00
|
|
|
}
|