2019-08-19 07:16:39 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-08-01 03:58:42 +02:00
|
|
|
use crate::object::{Primitive, TaggedDictBuilder, Value};
|
2019-07-19 22:11:49 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
use csv::ReaderBuilder;
|
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
pub struct FromCSV;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for FromCSV {
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
from_csv(args, registry)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"from-csv"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("from-csv")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 22:11:49 +02:00
|
|
|
pub fn from_csv_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-07-20 04:27:10 +02:00
|
|
|
let mut reader = ReaderBuilder::new()
|
|
|
|
.has_headers(false)
|
|
|
|
.from_reader(s.as_bytes());
|
2019-08-05 10:54:29 +02:00
|
|
|
let tag = tag.into();
|
2019-07-19 22:11:49 +02:00
|
|
|
|
|
|
|
let mut fields: VecDeque<String> = VecDeque::new();
|
|
|
|
let mut iter = reader.records();
|
2019-07-20 08:44:21 +02:00
|
|
|
let mut rows = vec![];
|
2019-07-19 22:11:49 +02:00
|
|
|
|
|
|
|
if let Some(result) = iter.next() {
|
|
|
|
let line = result?;
|
|
|
|
|
|
|
|
for item in line.iter() {
|
|
|
|
fields.push_back(item.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
if let Some(row_values) = iter.next() {
|
|
|
|
let row_values = row_values?;
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
let mut row = TaggedDictBuilder::new(tag);
|
2019-07-20 04:27:10 +02:00
|
|
|
|
2019-07-19 22:11:49 +02:00
|
|
|
for (idx, entry) in row_values.iter().enumerate() {
|
2019-08-01 03:58:42 +02:00
|
|
|
row.insert_tagged(
|
2019-07-20 04:27:10 +02:00
|
|
|
fields.get(idx).unwrap(),
|
2019-08-05 10:54:29 +02:00
|
|
|
Value::Primitive(Primitive::String(String::from(entry))).tagged(tag),
|
2019-07-20 04:27:10 +02:00
|
|
|
);
|
2019-07-19 22:11:49 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
rows.push(row.into_tagged_value());
|
2019-07-19 22:11:49 +02:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
Ok(Tagged::from_item(Value::List(rows), tag))
|
2019-07-19 22:11:49 +02:00
|
|
|
}
|
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
fn from_csv(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
2019-07-24 00:22:11 +02:00
|
|
|
let args = args.evaluate_once(registry)?;
|
|
|
|
let span = args.name_span();
|
2019-07-19 22:11:49 +02:00
|
|
|
let out = args.input;
|
|
|
|
|
|
|
|
Ok(out
|
|
|
|
.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_csv_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 CSV",
|
2019-08-05 10:54:29 +02:00
|
|
|
"input cannot be parsed as CSV",
|
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-19 22:11:49 +02:00
|
|
|
span,
|
2019-08-05 10:54:29 +02:00
|
|
|
"value originates from here",
|
|
|
|
a.span(),
|
2019-07-19 22:11:49 +02:00
|
|
|
)),
|
2019-08-01 03:58:42 +02:00
|
|
|
}
|
2019-07-19 22:11:49 +02:00
|
|
|
})
|
|
|
|
.to_output_stream())
|
|
|
|
}
|