2019-06-01 09:05:57 +02:00
|
|
|
use crate::object::base::OF64;
|
2019-07-13 04:18:02 +02:00
|
|
|
use crate::object::{Primitive, SpannedDictBuilder, Value};
|
2019-06-01 09:05:57 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-07-09 06:31:26 +02:00
|
|
|
fn convert_toml_value_to_nu_value(v: &toml::Value, span: impl Into<Span>) -> Spanned<Value> {
|
|
|
|
let span = span.into();
|
|
|
|
|
2019-06-01 09:05:57 +02:00
|
|
|
match v {
|
2019-07-09 06:31:26 +02:00
|
|
|
toml::Value::Boolean(b) => Value::Primitive(Primitive::Boolean(*b)).spanned(span),
|
|
|
|
toml::Value::Integer(n) => Value::Primitive(Primitive::Int(*n)).spanned(span),
|
|
|
|
toml::Value::Float(n) => Value::Primitive(Primitive::Float(OF64::from(*n))).spanned(span),
|
|
|
|
toml::Value::String(s) => {
|
|
|
|
Value::Primitive(Primitive::String(String::from(s))).spanned(span)
|
|
|
|
}
|
2019-06-16 01:03:49 +02:00
|
|
|
toml::Value::Array(a) => Value::List(
|
|
|
|
a.iter()
|
2019-07-09 06:31:26 +02:00
|
|
|
.map(|x| convert_toml_value_to_nu_value(x, span))
|
2019-06-16 01:03:49 +02:00
|
|
|
.collect(),
|
2019-07-09 06:31:26 +02:00
|
|
|
)
|
|
|
|
.spanned(span),
|
|
|
|
toml::Value::Datetime(dt) => {
|
|
|
|
Value::Primitive(Primitive::String(dt.to_string())).spanned(span)
|
|
|
|
}
|
2019-06-01 09:05:57 +02:00
|
|
|
toml::Value::Table(t) => {
|
2019-07-09 06:31:26 +02:00
|
|
|
let mut collected = SpannedDictBuilder::new(span);
|
|
|
|
|
2019-06-01 09:05:57 +02:00
|
|
|
for (k, v) in t.iter() {
|
2019-07-13 04:07:06 +02:00
|
|
|
collected.insert_spanned(k.clone(), convert_toml_value_to_nu_value(v, span));
|
2019-06-01 09:05:57 +02:00
|
|
|
}
|
2019-07-09 06:31:26 +02:00
|
|
|
|
|
|
|
collected.into_spanned_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,
|
|
|
|
span: impl Into<Span>,
|
|
|
|
) -> Result<Spanned<Value>, Box<dyn std::error::Error>> {
|
2019-06-16 01:03:49 +02:00
|
|
|
let v: toml::Value = s.parse::<toml::Value>()?;
|
2019-07-09 06:31:26 +02:00
|
|
|
Ok(convert_toml_value_to_nu_value(&v, span))
|
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-07-08 18:44:53 +02:00
|
|
|
.map(move |a| match a.item {
|
2019-07-09 06:31:26 +02:00
|
|
|
Value::Primitive(Primitive::String(s)) => match from_toml_string_to_value(s, span) {
|
2019-07-08 18:44:53 +02:00
|
|
|
Ok(x) => ReturnSuccess::value(x.spanned(a.span)),
|
2019-07-03 22:31:15 +02:00
|
|
|
Err(_) => Err(ShellError::maybe_labeled_error(
|
|
|
|
"Could not parse as TOML",
|
|
|
|
"piped data failed TOML parse",
|
|
|
|
span,
|
|
|
|
)),
|
2019-06-16 01:03:49 +02:00
|
|
|
},
|
2019-07-03 22:31:15 +02:00
|
|
|
_ => Err(ShellError::maybe_labeled_error(
|
2019-06-16 01:03:49 +02:00
|
|
|
"Expected string values from pipeline",
|
|
|
|
"expects strings from pipeline",
|
|
|
|
span,
|
2019-07-03 22:31:15 +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
|
|
|
}
|