2019-06-01 07:05:57 +00:00
|
|
|
use crate::prelude::*;
|
2021-01-10 02:50:49 +00:00
|
|
|
use nu_engine::WholeStreamCommand;
|
Extract core stuff into own crates
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`).
2019-11-26 02:30:48 +00:00
|
|
|
use nu_errors::ShellError;
|
2019-12-04 19:52:31 +00:00
|
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
2019-06-01 07:05:57 +00:00
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
pub struct FromTOML;
|
|
|
|
|
2020-05-29 08:22:52 +00:00
|
|
|
#[async_trait]
|
2019-08-19 05:16:39 +00:00
|
|
|
impl WholeStreamCommand for FromTOML {
|
|
|
|
fn name(&self) -> &str {
|
2020-05-04 08:44:33 +00:00
|
|
|
"from toml"
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-05-04 08:44:33 +00:00
|
|
|
Signature::build("from toml")
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
2019-08-29 22:52:32 +00:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse text as .toml and create table."
|
|
|
|
}
|
|
|
|
|
2020-12-18 07:53:49 +00:00
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
from_toml(args).await
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
pub fn convert_toml_value_to_nu_value(v: &toml::Value, tag: impl Into<Tag>) -> Value {
|
2019-08-05 08:54:29 +00:00
|
|
|
let tag = tag.into();
|
2020-09-21 17:28:31 +00:00
|
|
|
let span = tag.span;
|
2019-07-09 04:31:26 +00:00
|
|
|
|
2019-06-01 07:05:57 +00:00
|
|
|
match v {
|
2019-12-04 19:52:31 +00:00
|
|
|
toml::Value::Boolean(b) => UntaggedValue::boolean(*b).into_value(tag),
|
|
|
|
toml::Value::Integer(n) => UntaggedValue::int(*n).into_value(tag),
|
2020-09-21 17:28:31 +00:00
|
|
|
toml::Value::Float(n) => UntaggedValue::decimal_from_float(*n, span).into_value(tag),
|
2019-11-21 14:33:14 +00:00
|
|
|
toml::Value::String(s) => {
|
|
|
|
UntaggedValue::Primitive(Primitive::String(String::from(s))).into_value(tag)
|
|
|
|
}
|
|
|
|
toml::Value::Array(a) => UntaggedValue::Table(
|
2019-06-15 23:03:49 +00:00
|
|
|
a.iter()
|
2019-10-13 04:12:43 +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-11-21 14:33:14 +00:00
|
|
|
.into_value(tag),
|
2019-07-09 04:31:26 +00:00
|
|
|
toml::Value::Datetime(dt) => {
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::String(dt.to_string())).into_value(tag)
|
2019-07-09 04:31:26 +00:00
|
|
|
}
|
2019-06-01 07:05:57 +00:00
|
|
|
toml::Value::Table(t) => {
|
2019-10-13 04:12:43 +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-11-21 14:33:14 +00:00
|
|
|
collected.insert_value(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-11-21 14:33:14 +00:00
|
|
|
collected.into_value()
|
2019-06-01 07:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
pub fn from_toml_string_to_value(s: String, tag: impl Into<Tag>) -> Result<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
|
|
|
}
|
|
|
|
|
2020-12-18 07:53:49 +00:00
|
|
|
pub async fn from_toml(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once().await?;
|
2020-06-13 04:03:39 +00:00
|
|
|
let tag = args.name_tag();
|
|
|
|
let input = args.input;
|
2020-05-16 03:18:24 +00:00
|
|
|
|
2020-06-13 04:03:39 +00:00
|
|
|
let concat_string = input.collect_string(tag.clone()).await?;
|
|
|
|
Ok(
|
2020-03-06 16:06:39 +00:00
|
|
|
match from_toml_string_to_value(concat_string.item, tag.clone()) {
|
2019-08-24 07:38:38 +00:00
|
|
|
Ok(x) => match x {
|
2020-06-13 04:03:39 +00:00
|
|
|
Value {
|
|
|
|
value: UntaggedValue::Table(list),
|
|
|
|
..
|
|
|
|
} => futures::stream::iter(list.into_iter().map(ReturnSuccess::value))
|
|
|
|
.to_output_stream(),
|
|
|
|
x => OutputStream::one(ReturnSuccess::value(x)),
|
2019-08-24 07:38:38 +00:00
|
|
|
},
|
2020-03-06 16:06:39 +00:00
|
|
|
Err(_) => {
|
2020-06-13 04:03:39 +00:00
|
|
|
return Err(ShellError::labeled_error_with_secondary(
|
2019-08-21 06:39:57 +00:00
|
|
|
"Could not parse as TOML",
|
|
|
|
"input cannot be parsed as TOML",
|
2019-10-13 04:12:43 +00:00
|
|
|
&tag,
|
2019-08-21 06:39:57 +00:00
|
|
|
"value originates from here",
|
2020-03-06 16:06:39 +00:00
|
|
|
concat_string.tag,
|
2019-08-21 06:39:57 +00:00
|
|
|
))
|
2020-03-06 16:06:39 +00:00
|
|
|
}
|
2020-06-13 04:03:39 +00:00
|
|
|
},
|
|
|
|
)
|
2019-06-01 07:05:57 +00:00
|
|
|
}
|
2020-05-18 12:56:01 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::FromTOML;
|
2020-10-03 14:06:02 +00:00
|
|
|
use super::ShellError;
|
2020-05-18 12:56:01 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-10-03 14:06:02 +00:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-05-18 12:56:01 +00:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2021-02-12 10:13:14 +00:00
|
|
|
test_examples(FromTOML {})
|
2020-05-18 12:56:01 +00:00
|
|
|
}
|
|
|
|
}
|