forked from extern/nushell
e4226def16
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`).
152 lines
4.6 KiB
Rust
152 lines
4.6 KiB
Rust
use crate::commands::WholeStreamCommand;
|
|
use crate::data::{value, TaggedDictBuilder};
|
|
use crate::prelude::*;
|
|
use nu_errors::ShellError;
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
|
|
|
|
pub struct FromYAML;
|
|
|
|
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."
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
args: CommandArgs,
|
|
registry: &CommandRegistry,
|
|
) -> Result<OutputStream, ShellError> {
|
|
from_yaml(args, registry)
|
|
}
|
|
}
|
|
|
|
pub struct FromYML;
|
|
|
|
impl WholeStreamCommand for FromYML {
|
|
fn name(&self) -> &str {
|
|
"from-yml"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("from-yml")
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
fn convert_yaml_value_to_nu_value(v: &serde_yaml::Value, tag: impl Into<Tag>) -> Value {
|
|
let tag = tag.into();
|
|
|
|
match v {
|
|
serde_yaml::Value::Bool(b) => value::boolean(*b).into_value(tag),
|
|
serde_yaml::Value::Number(n) if n.is_i64() => {
|
|
value::number(n.as_i64().unwrap()).into_value(tag)
|
|
}
|
|
serde_yaml::Value::Number(n) if n.is_f64() => {
|
|
UntaggedValue::Primitive(Primitive::from(n.as_f64().unwrap())).into_value(tag)
|
|
}
|
|
serde_yaml::Value::String(s) => value::string(s).into_value(tag),
|
|
serde_yaml::Value::Sequence(a) => UntaggedValue::Table(
|
|
a.iter()
|
|
.map(|x| convert_yaml_value_to_nu_value(x, &tag))
|
|
.collect(),
|
|
)
|
|
.into_value(tag),
|
|
serde_yaml::Value::Mapping(t) => {
|
|
let mut collected = TaggedDictBuilder::new(&tag);
|
|
|
|
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));
|
|
}
|
|
_ => unimplemented!("Unknown key type"),
|
|
}
|
|
}
|
|
|
|
collected.into_value()
|
|
}
|
|
serde_yaml::Value::Null => UntaggedValue::Primitive(Primitive::Nothing).into_value(tag),
|
|
x => unimplemented!("Unsupported yaml case: {:?}", x),
|
|
}
|
|
}
|
|
|
|
pub fn from_yaml_string_to_value(s: String, tag: impl Into<Tag>) -> serde_yaml::Result<Value> {
|
|
let v: serde_yaml::Value = serde_yaml::from_str(&s)?;
|
|
Ok(convert_yaml_value_to_nu_value(&v, tag))
|
|
}
|
|
|
|
fn from_yaml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
let args = args.evaluate_once(registry)?;
|
|
let tag = args.name_tag();
|
|
let name_span = tag.span;
|
|
let input = args.input;
|
|
|
|
let stream = async_stream! {
|
|
let values: Vec<Value> = input.values.collect().await;
|
|
|
|
let mut concat_string = String::new();
|
|
let mut latest_tag: Option<Tag> = None;
|
|
|
|
for value in values {
|
|
latest_tag = Some(value.tag.clone());
|
|
let value_span = value.tag.span;
|
|
|
|
match &value.value {
|
|
UntaggedValue::Primitive(Primitive::String(s)) => {
|
|
concat_string.push_str(&s);
|
|
concat_string.push_str("\n");
|
|
}
|
|
_ => yield Err(ShellError::labeled_error_with_secondary(
|
|
"Expected a string from pipeline",
|
|
"requires string input",
|
|
name_span,
|
|
"value originates from here",
|
|
value_span,
|
|
)),
|
|
|
|
}
|
|
}
|
|
|
|
match from_yaml_string_to_value(concat_string, tag.clone()) {
|
|
Ok(x) => match x {
|
|
Value { value: UntaggedValue::Table(list), .. } => {
|
|
for l in list {
|
|
yield ReturnSuccess::value(l);
|
|
}
|
|
}
|
|
x => yield ReturnSuccess::value(x),
|
|
},
|
|
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",
|
|
&tag,
|
|
"value originates from here",
|
|
&last_tag,
|
|
))
|
|
} ,
|
|
}
|
|
};
|
|
|
|
Ok(stream.to_output_stream())
|
|
}
|