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

167 lines
4.8 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
2019-06-03 09:41:28 +02:00
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
2019-06-03 09:41:28 +02:00
pub struct FromYAML;
2020-05-29 10:22:52 +02:00
#[async_trait]
impl WholeStreamCommand for FromYAML {
fn name(&self) -> &str {
"from yaml"
}
fn signature(&self) -> Signature {
Signature::build("from yaml")
}
fn usage(&self) -> &str {
"Parse text as .yaml/.yml and create table."
}
2019-08-29 05:53:45 +02:00
2020-05-29 10:22:52 +02:00
async fn run(
2019-08-29 05:53:45 +02:00
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_yaml(args, registry)
}
}
2019-08-29 05:53:45 +02:00
pub struct FromYML;
2020-05-29 10:22:52 +02:00
#[async_trait]
impl WholeStreamCommand for FromYML {
2019-08-29 05:53:45 +02:00
fn name(&self) -> &str {
"from yml"
2019-08-29 05:53:45 +02:00
}
fn signature(&self) -> Signature {
Signature::build("from yml")
2019-08-29 05:53:45 +02:00
}
fn usage(&self) -> &str {
"Parse text as .yaml/.yml and create table."
}
2020-05-29 10:22:52 +02:00
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_yaml(args, registry)
}
2019-08-29 05:53:45 +02:00
}
fn convert_yaml_value_to_nu_value(
v: &serde_yaml::Value,
tag: impl Into<Tag>,
) -> Result<Value, ShellError> {
let tag = tag.into();
2019-07-09 06:31:26 +02:00
Ok(match v {
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() => {
UntaggedValue::int(n.as_i64().ok_or_else(|| {
ShellError::labeled_error(
"Expected a compatible number",
"expected a compatible number",
&tag,
)
})?)
.into_value(tag)
2019-06-03 09:41:28 +02:00
}
serde_yaml::Value::Number(n) if n.is_f64() => {
UntaggedValue::decimal(n.as_f64().ok_or_else(|| {
ShellError::labeled_error(
"Expected a compatible number",
"expected a compatible number",
&tag,
)
})?)
.into_value(tag)
2019-06-03 09:41:28 +02:00
}
serde_yaml::Value::String(s) => UntaggedValue::string(s).into_value(tag),
serde_yaml::Value::Sequence(a) => {
let result: Result<Vec<Value>, ShellError> = a
.iter()
.map(|x| convert_yaml_value_to_nu_value(x, &tag))
.collect();
UntaggedValue::Table(result?).into_value(tag)
}
2019-06-03 09:41:28 +02:00
serde_yaml::Value::Mapping(t) => {
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) => {
collected.insert_value(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
collected.into_value()
2019-06-03 09:41:28 +02:00
}
serde_yaml::Value::Null => UntaggedValue::Primitive(Primitive::Nothing).into_value(tag),
x => unimplemented!("Unsupported yaml case: {:?}", x),
})
2019-06-03 09:41:28 +02: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
}
2019-08-21 08:39:57 +02:00
fn from_yaml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let stream = async_stream! {
let args = args.evaluate_once(&registry).await?;
let tag = args.name_tag();
let input = args.input;
let concat_string = input.collect_string(tag.clone()).await?;
2019-08-21 08:39:57 +02:00
match from_yaml_string_to_value(concat_string.item, 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),
},
Err(_) => {
2019-08-21 08:39:57 +02:00
yield Err(ShellError::labeled_error_with_secondary(
"Could not parse as YAML",
"input cannot be parsed as YAML",
&tag,
2019-08-21 08:39:57 +02:00
"value originates from here",
&concat_string.tag,
2019-08-21 08:39:57 +02:00
))
}
2019-08-21 08:39:57 +02:00
}
};
Ok(stream.to_output_stream())
2019-06-03 09:41:28 +02:00
}
#[cfg(test)]
mod tests {
use super::FromYAML;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromYAML {})
}
}