2019-08-01 01:58:42 +00:00
|
|
|
use crate::object::{Primitive, TaggedDictBuilder, Value};
|
2019-07-19 20:11:49 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
use csv::ReaderBuilder;
|
|
|
|
|
|
|
|
pub fn from_csv_string_to_value(
|
|
|
|
s: String,
|
|
|
|
span: impl Into<Span>,
|
2019-08-01 01:58:42 +00:00
|
|
|
) -> Result<Tagged<Value>, Box<dyn std::error::Error>> {
|
2019-07-20 02:27:10 +00:00
|
|
|
let mut reader = ReaderBuilder::new()
|
|
|
|
.has_headers(false)
|
|
|
|
.from_reader(s.as_bytes());
|
2019-07-19 20:11:49 +00:00
|
|
|
let span = span.into();
|
|
|
|
|
|
|
|
let mut fields: VecDeque<String> = VecDeque::new();
|
|
|
|
let mut iter = reader.records();
|
2019-07-20 06:44:21 +00:00
|
|
|
let mut rows = vec![];
|
2019-07-19 20:11:49 +00: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-01 01:58:42 +00:00
|
|
|
let mut row = TaggedDictBuilder::new(span);
|
2019-07-20 02:27:10 +00:00
|
|
|
|
2019-07-19 20:11:49 +00:00
|
|
|
for (idx, entry) in row_values.iter().enumerate() {
|
2019-08-01 01:58:42 +00:00
|
|
|
row.insert_tagged(
|
2019-07-20 02:27:10 +00:00
|
|
|
fields.get(idx).unwrap(),
|
2019-08-01 01:58:42 +00:00
|
|
|
Value::Primitive(Primitive::String(String::from(entry))).tagged(span),
|
2019-07-20 02:27:10 +00:00
|
|
|
);
|
2019-07-19 20:11:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
rows.push(row.into_tagged_value());
|
2019-07-19 20:11:49 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
Ok(Tagged::from_item(Value::List(rows), span))
|
2019-07-19 20:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_csv(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
let out = args.input;
|
2019-07-20 02:27:10 +00:00
|
|
|
let span = args.call_info.name_span;
|
2019-07-19 20:11:49 +00:00
|
|
|
|
|
|
|
Ok(out
|
|
|
|
.values
|
2019-08-01 01:58:42 +00:00
|
|
|
.map(move |a| {
|
|
|
|
let value_span = a.span();
|
|
|
|
match a.item {
|
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
|
|
|
match from_csv_string_to_value(s, value_span) {
|
|
|
|
Ok(x) => ReturnSuccess::value(x),
|
|
|
|
Err(_) => Err(ShellError::maybe_labeled_error(
|
|
|
|
"Could not parse as CSV",
|
|
|
|
"piped data failed CSV parse",
|
|
|
|
span,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Err(ShellError::maybe_labeled_error(
|
|
|
|
"Expected string values from pipeline",
|
|
|
|
"expects strings from pipeline",
|
2019-07-19 20:11:49 +00:00
|
|
|
span,
|
|
|
|
)),
|
2019-08-01 01:58:42 +00:00
|
|
|
}
|
2019-07-19 20:11:49 +00:00
|
|
|
})
|
|
|
|
.to_output_stream())
|
|
|
|
}
|