2019-06-01 09:05:57 +02:00
|
|
|
use crate::object::base::OF64;
|
2019-08-01 03:58:42 +02:00
|
|
|
use crate::object::{Primitive, TaggedDictBuilder, Value};
|
2019-06-01 09:05:57 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
fn convert_toml_value_to_nu_value(v: &toml::Value, tag: impl Into<Tag>) -> Tagged<Value> {
|
|
|
|
let tag = tag.into();
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-06-01 09:05:57 +02:00
|
|
|
match v {
|
2019-08-05 10:54:29 +02:00
|
|
|
toml::Value::Boolean(b) => Value::Primitive(Primitive::Boolean(*b)).tagged(tag),
|
|
|
|
toml::Value::Integer(n) => Value::Primitive(Primitive::Int(*n)).tagged(tag),
|
|
|
|
toml::Value::Float(n) => Value::Primitive(Primitive::Float(OF64::from(*n))).tagged(tag),
|
|
|
|
toml::Value::String(s) => Value::Primitive(Primitive::String(String::from(s))).tagged(tag),
|
2019-06-16 01:03:49 +02:00
|
|
|
toml::Value::Array(a) => Value::List(
|
|
|
|
a.iter()
|
2019-08-05 10:54:29 +02:00
|
|
|
.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
|
|
|
)
|
2019-08-05 10:54:29 +02:00
|
|
|
.tagged(tag),
|
2019-07-09 06:31:26 +02:00
|
|
|
toml::Value::Datetime(dt) => {
|
2019-08-05 10:54:29 +02:00
|
|
|
Value::Primitive(Primitive::String(dt.to_string())).tagged(tag)
|
2019-07-09 06:31:26 +02:00
|
|
|
}
|
2019-06-01 09:05:57 +02:00
|
|
|
toml::Value::Table(t) => {
|
2019-08-05 10:54:29 +02:00
|
|
|
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() {
|
2019-08-05 10:54:29 +02:00
|
|
|
collected.insert_tagged(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
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
collected.into_tagged_value()
|
2019-06-01 09:05:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 06:31:26 +02:00
|
|
|
pub fn from_toml_string_to_value(
|
|
|
|
s: String,
|
2019-08-05 10:54:29 +02:00
|
|
|
tag: impl Into<Tag>,
|
2019-08-01 03:58:42 +02:00
|
|
|
) -> Result<Tagged<Value>, Box<dyn std::error::Error>> {
|
2019-06-16 01:03:49 +02:00
|
|
|
let v: toml::Value = s.parse::<toml::Value>()?;
|
2019-08-05 10:54:29 +02:00
|
|
|
Ok(convert_toml_value_to_nu_value(&v, tag))
|
2019-06-01 21:20:48 +02:00
|
|
|
}
|
|
|
|
|
2019-06-01 09:05:57 +02:00
|
|
|
pub fn from_toml(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-01 09:05:57 +02:00
|
|
|
Ok(out
|
2019-07-03 22:31:15 +02:00
|
|
|
.values
|
2019-08-01 03:58:42 +02:00
|
|
|
.map(move |a| {
|
2019-08-05 10:54:29 +02:00
|
|
|
let value_tag = a.tag();
|
2019-08-01 03:58:42 +02:00
|
|
|
match a.item {
|
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
2019-08-05 10:54:29 +02:00
|
|
|
match from_toml_string_to_value(s, value_tag) {
|
2019-08-01 03:58:42 +02:00
|
|
|
Ok(x) => ReturnSuccess::value(x),
|
2019-08-05 10:54:29 +02:00
|
|
|
Err(_) => Err(ShellError::labeled_error_with_secondary(
|
2019-08-01 03:58:42 +02:00
|
|
|
"Could not parse as TOML",
|
2019-08-05 10:54:29 +02:00
|
|
|
"input cannot be parsed as TOML",
|
2019-08-01 03:58:42 +02:00
|
|
|
span,
|
2019-08-05 10:54:29 +02:00
|
|
|
"value originates from here",
|
|
|
|
value_tag.span,
|
2019-08-01 03:58:42 +02:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
2019-08-05 10:54:29 +02:00
|
|
|
_ => Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-07-03 22:31:15 +02:00
|
|
|
span,
|
2019-08-05 10:54:29 +02:00
|
|
|
"value originates from here",
|
|
|
|
a.span(),
|
2019-07-03 22:31:15 +02:00
|
|
|
)),
|
2019-08-01 03:58:42 +02:00
|
|
|
}
|
2019-06-01 09:05:57 +02:00
|
|
|
})
|
2019-07-03 22:31:15 +02:00
|
|
|
.to_output_stream())
|
2019-06-01 09:05:57 +02:00
|
|
|
}
|