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

99 lines
3.0 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
2019-06-01 09:05:57 +02:00
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
2019-06-01 09:05:57 +02:00
pub struct FromTOML;
impl WholeStreamCommand for FromTOML {
fn name(&self) -> &str {
"from-toml"
}
fn signature(&self) -> Signature {
Signature::build("from-toml")
}
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)
}
}
pub fn convert_toml_value_to_nu_value(v: &toml::Value, tag: impl Into<Tag>) -> Value {
let tag = tag.into();
2019-07-09 06:31:26 +02:00
2019-06-01 09:05:57 +02:00
match v {
toml::Value::Boolean(b) => UntaggedValue::boolean(*b).into_value(tag),
toml::Value::Integer(n) => UntaggedValue::int(*n).into_value(tag),
toml::Value::Float(n) => UntaggedValue::decimal(*n).into_value(tag),
toml::Value::String(s) => {
UntaggedValue::Primitive(Primitive::String(String::from(s))).into_value(tag)
}
toml::Value::Array(a) => UntaggedValue::Table(
2019-06-16 01:03:49 +02:00
a.iter()
.map(|x| convert_toml_value_to_nu_value(x, &tag))
2019-06-16 01:03:49 +02:00
.collect(),
2019-07-09 06:31:26 +02:00
)
.into_value(tag),
2019-07-09 06:31:26 +02:00
toml::Value::Datetime(dt) => {
UntaggedValue::Primitive(Primitive::String(dt.to_string())).into_value(tag)
2019-07-09 06:31:26 +02:00
}
2019-06-01 09:05:57 +02:00
toml::Value::Table(t) => {
let mut collected = TaggedDictBuilder::new(&tag);
2019-07-09 06:31:26 +02:00
2019-06-01 09:05:57 +02:00
for (k, v) in t.iter() {
collected.insert_value(k.clone(), convert_toml_value_to_nu_value(v, &tag));
2019-06-01 09:05:57 +02:00
}
2019-07-09 06:31:26 +02:00
collected.into_value()
2019-06-01 09:05:57 +02:00
}
}
}
pub fn from_toml_string_to_value(s: String, tag: impl Into<Tag>) -> Result<Value, toml::de::Error> {
2019-06-16 01:03:49 +02:00
let v: toml::Value = s.parse::<toml::Value>()?;
Ok(convert_toml_value_to_nu_value(&v, tag))
2019-06-01 21:20:48 +02:00
}
2019-07-24 00:22:11 +02:00
pub fn from_toml(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(registry)?;
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
let stream = async_stream! {
let concat_string = input.collect_string(tag.clone()).await?;
match from_toml_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 TOML",
"input cannot be parsed as TOML",
&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-01 09:05:57 +02:00
}