2019-08-19 05:16:39 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-08-01 01:58:42 +00:00
|
|
|
use crate::object::{Primitive, TaggedDictBuilder, Value};
|
2019-06-01 07:05:57 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
pub struct FromTOML;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for FromTOML {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"from-toml"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("from-toml")
|
|
|
|
}
|
2019-08-29 22:52:32 +00:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse text as .toml and create table."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
from_toml(args, registry)
|
|
|
|
}
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 04:51:21 +00:00
|
|
|
pub fn convert_toml_value_to_nu_value(v: &toml::Value, tag: impl Into<Tag>) -> Tagged<Value> {
|
2019-08-05 08:54:29 +00:00
|
|
|
let tag = tag.into();
|
2019-07-09 04:31:26 +00:00
|
|
|
|
2019-06-01 07:05:57 +00:00
|
|
|
match v {
|
2019-09-01 16:20:31 +00:00
|
|
|
toml::Value::Boolean(b) => Value::boolean(*b).tagged(tag),
|
|
|
|
toml::Value::Integer(n) => Value::number(n).tagged(tag),
|
|
|
|
toml::Value::Float(n) => Value::number(n).tagged(tag),
|
2019-08-05 08:54:29 +00:00
|
|
|
toml::Value::String(s) => Value::Primitive(Primitive::String(String::from(s))).tagged(tag),
|
2019-06-15 23:03:49 +00:00
|
|
|
toml::Value::Array(a) => Value::List(
|
|
|
|
a.iter()
|
2019-08-05 08:54:29 +00:00
|
|
|
.map(|x| convert_toml_value_to_nu_value(x, tag))
|
2019-06-15 23:03:49 +00:00
|
|
|
.collect(),
|
2019-07-09 04:31:26 +00:00
|
|
|
)
|
2019-08-05 08:54:29 +00:00
|
|
|
.tagged(tag),
|
2019-07-09 04:31:26 +00:00
|
|
|
toml::Value::Datetime(dt) => {
|
2019-08-05 08:54:29 +00:00
|
|
|
Value::Primitive(Primitive::String(dt.to_string())).tagged(tag)
|
2019-07-09 04:31:26 +00:00
|
|
|
}
|
2019-06-01 07:05:57 +00:00
|
|
|
toml::Value::Table(t) => {
|
2019-08-05 08:54:29 +00:00
|
|
|
let mut collected = TaggedDictBuilder::new(tag);
|
2019-07-09 04:31:26 +00:00
|
|
|
|
2019-06-01 07:05:57 +00:00
|
|
|
for (k, v) in t.iter() {
|
2019-08-05 08:54:29 +00:00
|
|
|
collected.insert_tagged(k.clone(), convert_toml_value_to_nu_value(v, tag));
|
2019-06-01 07:05:57 +00:00
|
|
|
}
|
2019-07-09 04:31:26 +00:00
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
collected.into_tagged_value()
|
2019-06-01 07:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 04:31:26 +00:00
|
|
|
pub fn from_toml_string_to_value(
|
|
|
|
s: String,
|
2019-08-05 08:54:29 +00:00
|
|
|
tag: impl Into<Tag>,
|
2019-08-21 06:39:57 +00:00
|
|
|
) -> Result<Tagged<Value>, toml::de::Error> {
|
2019-06-15 23:03:49 +00:00
|
|
|
let v: toml::Value = s.parse::<toml::Value>()?;
|
2019-08-05 08:54:29 +00:00
|
|
|
Ok(convert_toml_value_to_nu_value(&v, tag))
|
2019-06-01 19:20:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 22:22:11 +00:00
|
|
|
pub fn from_toml(
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once(registry)?;
|
|
|
|
let span = args.name_span();
|
2019-08-21 06:39:57 +00:00
|
|
|
let input = args.input;
|
2019-07-23 22:22:11 +00:00
|
|
|
|
2019-08-21 06:39:57 +00:00
|
|
|
let stream = async_stream_block! {
|
|
|
|
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();
|
|
|
|
latest_tag = Some(value_tag);
|
|
|
|
match value.item {
|
2019-08-01 01:58:42 +00:00
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
2019-08-21 06:39:57 +00:00
|
|
|
concat_string.push_str(&s);
|
|
|
|
concat_string.push_str("\n");
|
2019-08-01 01:58:42 +00:00
|
|
|
}
|
2019-08-21 06:39:57 +00:00
|
|
|
_ => yield Err(ShellError::labeled_error_with_secondary(
|
2019-08-05 08:54:29 +00:00
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-07-03 20:31:15 +00:00
|
|
|
span,
|
2019-08-21 06:39:57 +00:00
|
|
|
"value originates from here",
|
2019-08-06 03:03:13 +00:00
|
|
|
value_tag.span,
|
2019-07-03 20:31:15 +00:00
|
|
|
)),
|
2019-08-21 06:39:57 +00:00
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
}
|
2019-08-21 06:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match from_toml_string_to_value(concat_string, span) {
|
2019-08-24 07:38:38 +00:00
|
|
|
Ok(x) => match x {
|
|
|
|
Tagged { item: Value::List(list), .. } => {
|
|
|
|
for l in list {
|
|
|
|
yield ReturnSuccess::value(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x => yield ReturnSuccess::value(x),
|
|
|
|
},
|
2019-08-21 06:39:57 +00:00
|
|
|
Err(_) => if let Some(last_tag) = latest_tag {
|
|
|
|
yield Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Could not parse as TOML",
|
|
|
|
"input cannot be parsed as TOML",
|
|
|
|
span,
|
|
|
|
"value originates from here",
|
|
|
|
last_tag.span,
|
|
|
|
))
|
|
|
|
} ,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
2019-06-01 07:05:57 +00:00
|
|
|
}
|