nushell/src/commands/from_ini.rs

121 lines
3.4 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
use crate::data::{Primitive, TaggedDictBuilder, Value};
2019-06-16 08:43:40 +02:00
use crate::prelude::*;
use std::collections::HashMap;
pub struct FromINI;
impl WholeStreamCommand for FromINI {
fn name(&self) -> &str {
"from-ini"
}
fn signature(&self) -> Signature {
Signature::build("from-ini")
}
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-07-09 06:31:26 +02:00
fn convert_ini_second_to_nu_value(
v: &HashMap<String, String>,
tag: impl Into<Tag>,
2019-08-01 03:58:42 +02:00
) -> Tagged<Value> {
let mut second = TaggedDictBuilder::new(tag);
2019-07-09 06:31:26 +02:00
2019-06-16 08:43:40 +02:00
for (key, value) in v.into_iter() {
2019-07-09 06:31:26 +02:00
second.insert(key.clone(), Primitive::String(value.clone()));
2019-06-16 08:43:40 +02:00
}
2019-07-09 06:31:26 +02:00
2019-08-01 03:58:42 +02:00
second.into_tagged_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>>,
tag: impl Into<Tag>,
2019-08-01 03:58:42 +02:00
) -> Tagged<Value> {
let tag = tag.into();
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() {
top_level.insert_tagged(
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-08-01 03:58:42 +02:00
top_level.into_tagged_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,
tag: impl Into<Tag>,
2019-08-21 08:39:57 +02:00
) -> Result<Tagged<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)?;
Ok(convert_ini_top_to_nu_value(&v, tag))
2019-06-16 08:43:40 +02:00
}
fn from_ini(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
2019-07-24 00:22:11 +02:00
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! {
2019-08-21 08:39:57 +02:00
let values: Vec<Tagged<Value>> = input.values.collect().await;
let mut concat_string = String::new();
let mut latest_tag: Option<Tag> = None;
for value in values {
let value_tag = value.tag();
latest_tag = Some(value_tag.clone());
2019-08-21 08:39:57 +02:00
match value.item {
2019-08-01 03:58:42 +02:00
Value::Primitive(Primitive::String(s)) => {
2019-08-21 08:39:57 +02:00
concat_string.push_str(&s);
concat_string.push_str("\n");
2019-08-01 03:58:42 +02:00
}
2019-08-21 08:39:57 +02:00
_ => yield Err(ShellError::labeled_error_with_secondary(
"Expected a string from pipeline",
"requires string input",
&tag,
"value originates from here",
&value_tag,
)),
2019-08-21 08:39:57 +02:00
2019-08-01 03:58:42 +02:00
}
2019-08-21 08:39:57 +02:00
}
match from_ini_string_to_value(concat_string, tag.clone()) {
2019-08-24 09:38:38 +02:00
Ok(x) => match x {
Tagged { item: Value::Table(list), .. } => {
2019-08-24 09:38:38 +02:00
for l in list {
yield ReturnSuccess::value(l);
}
}
x => yield ReturnSuccess::value(x),
},
2019-08-21 08:39:57 +02:00
Err(_) => if let Some(last_tag) = latest_tag {
yield Err(ShellError::labeled_error_with_secondary(
"Could not parse as INI",
"input cannot be parsed as INI",
&tag,
2019-08-21 08:39:57 +02:00
"value originates from here",
last_tag,
2019-08-21 08:39:57 +02:00
))
} ,
}
};
Ok(stream.to_output_stream())
2019-06-16 08:43:40 +02:00
}