2019-08-19 05:16:39 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-09-05 16:23:42 +00:00
|
|
|
use crate::data::{Primitive, TaggedDictBuilder, Value};
|
2019-07-19 20:11:49 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
use csv::ReaderBuilder;
|
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
pub struct FromCSV;
|
|
|
|
|
2019-08-25 12:59:46 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct FromCSVArgs {
|
|
|
|
headerless: bool,
|
|
|
|
}
|
2019-08-19 05:16:39 +00:00
|
|
|
|
2019-08-25 12:59:46 +00:00
|
|
|
impl WholeStreamCommand for FromCSV {
|
2019-08-19 05:16:39 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"from-csv"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2019-10-28 05:15:35 +00:00
|
|
|
Signature::build("from-csv")
|
|
|
|
.switch("headerless", "don't treat the first row as column names")
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse text as .csv and create table"
|
2019-08-25 12:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
args.process(registry, from_csv)?.run()
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 20:11:49 +00:00
|
|
|
pub fn from_csv_string_to_value(
|
|
|
|
s: String,
|
2019-08-25 12:59:46 +00:00
|
|
|
headerless: bool,
|
2019-08-05 08:54:29 +00:00
|
|
|
tag: impl Into<Tag>,
|
2019-08-21 06:39:57 +00:00
|
|
|
) -> Result<Tagged<Value>, csv::Error> {
|
2019-07-20 02:27:10 +00:00
|
|
|
let mut reader = ReaderBuilder::new()
|
|
|
|
.has_headers(false)
|
|
|
|
.from_reader(s.as_bytes());
|
2019-08-05 08:54:29 +00:00
|
|
|
let tag = tag.into();
|
2019-07-19 20:11:49 +00:00
|
|
|
|
|
|
|
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?;
|
|
|
|
|
2019-08-25 12:59:46 +00:00
|
|
|
for (idx, item) in line.iter().enumerate() {
|
|
|
|
if headerless {
|
|
|
|
fields.push_back(format!("Column{}", idx + 1));
|
|
|
|
} else {
|
|
|
|
fields.push_back(item.to_string());
|
|
|
|
}
|
2019-07-19 20:11:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
if let Some(row_values) = iter.next() {
|
|
|
|
let row_values = row_values?;
|
|
|
|
|
2019-10-13 04:12:43 +00:00
|
|
|
let mut row = TaggedDictBuilder::new(tag.clone());
|
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-10-13 04:12:43 +00:00
|
|
|
Value::Primitive(Primitive::String(String::from(entry))).tagged(&tag),
|
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-10-13 04:12:43 +00:00
|
|
|
Ok(Value::Table(rows).tagged(&tag))
|
2019-07-19 20:11:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 12:59:46 +00:00
|
|
|
fn from_csv(
|
|
|
|
FromCSVArgs {
|
|
|
|
headerless: skip_headers,
|
|
|
|
}: FromCSVArgs,
|
|
|
|
RunnableContext { input, name, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-09-14 16:30:24 +00:00
|
|
|
let name_tag = name;
|
2019-07-19 20:11:49 +00:00
|
|
|
|
2019-09-26 00:22:17 +00:00
|
|
|
let stream = async_stream! {
|
2019-08-21 06:39:57 +00: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();
|
2019-10-13 04:12:43 +00:00
|
|
|
latest_tag = Some(value_tag.clone());
|
2019-08-21 06:39:57 +00:00
|
|
|
match value.item {
|
2019-08-01 01:58:42 +00:00
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
2019-08-21 06:39:57 +00:00
|
|
|
concat_string.push_str(&s);
|
|
|
|
concat_string.push_str("\n");
|
2019-08-01 01:58:42 +00:00
|
|
|
}
|
2019-08-21 06:39:57 +00:00
|
|
|
_ => yield Err(ShellError::labeled_error_with_secondary(
|
2019-08-05 08:54:29 +00:00
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-10-13 04:12:43 +00:00
|
|
|
name_tag.clone(),
|
2019-08-05 08:54:29 +00:00
|
|
|
"value originates from here",
|
2019-10-13 04:12:43 +00:00
|
|
|
value_tag.clone(),
|
2019-07-19 20:11:49 +00:00
|
|
|
)),
|
2019-08-21 06:39:57 +00:00
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
}
|
2019-08-21 06:39:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 04:12:43 +00:00
|
|
|
match from_csv_string_to_value(concat_string, skip_headers, name_tag.clone()) {
|
2019-08-24 07:38:38 +00:00
|
|
|
Ok(x) => match x {
|
2019-09-05 16:23:42 +00:00
|
|
|
Tagged { item: Value::Table(list), .. } => {
|
2019-08-24 07:38:38 +00:00
|
|
|
for l in list {
|
|
|
|
yield ReturnSuccess::value(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x => yield ReturnSuccess::value(x),
|
|
|
|
},
|
2019-08-21 06:39:57 +00:00
|
|
|
Err(_) => if let Some(last_tag) = latest_tag {
|
|
|
|
yield Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Could not parse as CSV",
|
|
|
|
"input cannot be parsed as CSV",
|
2019-10-13 04:12:43 +00:00
|
|
|
name_tag.clone(),
|
2019-08-21 06:39:57 +00:00
|
|
|
"value originates from here",
|
2019-10-13 04:12:43 +00:00
|
|
|
last_tag.clone(),
|
2019-08-21 06:39:57 +00:00
|
|
|
))
|
|
|
|
} ,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
2019-07-19 20:11:49 +00:00
|
|
|
}
|