2019-08-19 07:16:39 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-06-16 08:43:40 +02:00
|
|
|
use crate::prelude::*;
|
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 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2019-12-04 20:52:31 +01:00
|
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
2019-06-16 08:43:40 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
pub struct FromINI;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for FromINI {
|
|
|
|
fn name(&self) -> &str {
|
2020-05-04 10:44:33 +02:00
|
|
|
"from ini"
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-05-04 10:44:33 +02:00
|
|
|
Signature::build("from ini")
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
2019-08-30 00:52:32 +02:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse text as .ini and create table"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
from_ini(args, registry)
|
|
|
|
}
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
fn convert_ini_second_to_nu_value(v: &HashMap<String, String>, tag: impl Into<Tag>) -> Value {
|
2019-08-05 10:54:29 +02:00
|
|
|
let mut second = TaggedDictBuilder::new(tag);
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-12-06 16:28:26 +01:00
|
|
|
for (key, value) in v.iter() {
|
2019-11-21 15:33:14 +01:00
|
|
|
second.insert_untagged(key.clone(), Primitive::String(value.clone()));
|
2019-06-16 08:43:40 +02:00
|
|
|
}
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
second.into_value()
|
2019-06-16 08:43:40 +02:00
|
|
|
}
|
2019-07-09 06:31:26 +02:00
|
|
|
|
|
|
|
fn convert_ini_top_to_nu_value(
|
|
|
|
v: &HashMap<String, HashMap<String, String>>,
|
2019-08-05 10:54:29 +02:00
|
|
|
tag: impl Into<Tag>,
|
2019-11-21 15:33:14 +01:00
|
|
|
) -> Value {
|
2019-08-05 10:54:29 +02:00
|
|
|
let tag = tag.into();
|
2019-10-13 06:12:43 +02:00
|
|
|
let mut top_level = TaggedDictBuilder::new(tag.clone());
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-06-16 08:43:40 +02:00
|
|
|
for (key, value) in v.iter() {
|
2019-11-21 15:33:14 +01:00
|
|
|
top_level.insert_value(
|
2019-10-13 06:12:43 +02:00
|
|
|
key.clone(),
|
|
|
|
convert_ini_second_to_nu_value(value, tag.clone()),
|
|
|
|
);
|
2019-06-16 08:43:40 +02:00
|
|
|
}
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
top_level.into_value()
|
2019-06-16 08:43:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 06:31:26 +02:00
|
|
|
pub fn from_ini_string_to_value(
|
|
|
|
s: String,
|
2019-08-05 10:54:29 +02:00
|
|
|
tag: impl Into<Tag>,
|
2019-11-21 15:33:14 +01:00
|
|
|
) -> Result<Value, serde_ini::de::Error> {
|
2019-06-16 08:43:40 +02:00
|
|
|
let v: HashMap<String, HashMap<String, String>> = serde_ini::from_str(&s)?;
|
2019-08-05 10:54:29 +02:00
|
|
|
Ok(convert_ini_top_to_nu_value(&v, tag))
|
2019-06-16 08:43:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
fn from_ini(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
let registry = registry.clone();
|
2019-09-26 02:22:17 +02:00
|
|
|
let stream = async_stream! {
|
2020-05-16 05:18:24 +02:00
|
|
|
let args = args.evaluate_once(®istry).await?;
|
|
|
|
let tag = args.name_tag();
|
|
|
|
let input = args.input;
|
2020-03-06 17:06:39 +01:00
|
|
|
let concat_string = input.collect_string(tag.clone()).await?;
|
2019-08-21 08:39:57 +02:00
|
|
|
|
2020-03-06 17:06:39 +01:00
|
|
|
match from_ini_string_to_value(concat_string.item, tag.clone()) {
|
2019-08-24 09:38:38 +02:00
|
|
|
Ok(x) => match x {
|
2019-11-21 15:33:14 +01:00
|
|
|
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),
|
|
|
|
},
|
2020-03-06 17:06:39 +01:00
|
|
|
Err(_) => {
|
2019-08-21 08:39:57 +02:00
|
|
|
yield Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Could not parse as INI",
|
|
|
|
"input cannot be parsed as INI",
|
2019-10-13 06:12:43 +02:00
|
|
|
&tag,
|
2019-08-21 08:39:57 +02:00
|
|
|
"value originates from here",
|
2020-03-06 17:06:39 +01:00
|
|
|
concat_string.tag,
|
2019-08-21 08:39:57 +02:00
|
|
|
))
|
2020-03-06 17:06:39 +01:00
|
|
|
}
|
2019-08-21 08:39:57 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
2019-06-16 08:43:40 +02:00
|
|
|
}
|