2019-08-29 11:02:16 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-09-05 18:23:42 +02:00
|
|
|
use crate::data::{Primitive, TaggedDictBuilder, Value};
|
2019-08-29 11:02:16 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
use csv::ReaderBuilder;
|
|
|
|
|
|
|
|
pub struct FromTSV;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct FromTSVArgs {
|
|
|
|
headerless: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WholeStreamCommand for FromTSV {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"from-tsv"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2019-10-28 06:15:35 +01:00
|
|
|
Signature::build("from-tsv")
|
|
|
|
.switch("headerless", "don't treat the first row as column names")
|
2019-08-29 11:02:16 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Parse text as .tsv and create table."
|
|
|
|
}
|
|
|
|
|
2019-08-29 11:02:16 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
args.process(registry, from_tsv)?.run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_tsv_string_to_value(
|
|
|
|
s: String,
|
|
|
|
headerless: bool,
|
|
|
|
tag: impl Into<Tag>,
|
|
|
|
) -> Result<Tagged<Value>, csv::Error> {
|
|
|
|
let mut reader = ReaderBuilder::new()
|
2019-11-11 12:25:41 +01:00
|
|
|
.has_headers(!headerless)
|
2019-08-29 11:02:16 +02:00
|
|
|
.delimiter(b'\t')
|
|
|
|
.from_reader(s.as_bytes());
|
|
|
|
let tag = tag.into();
|
|
|
|
|
2019-11-11 12:25:41 +01:00
|
|
|
let headers = if headerless {
|
|
|
|
(1..=reader.headers()?.len())
|
|
|
|
.map(|i| format!("Column{}", i))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
} else {
|
|
|
|
reader.headers()?.iter().map(String::from).collect()
|
|
|
|
};
|
2019-08-29 11:02:16 +02:00
|
|
|
|
2019-11-11 12:25:41 +01:00
|
|
|
let mut rows = vec![];
|
|
|
|
for row in reader.records() {
|
|
|
|
let mut tagged_row = TaggedDictBuilder::new(&tag);
|
|
|
|
for (value, header) in row?.iter().zip(headers.iter()) {
|
|
|
|
tagged_row.insert_tagged(
|
|
|
|
header,
|
|
|
|
Value::Primitive(Primitive::String(String::from(value))).tagged(&tag),
|
|
|
|
)
|
2019-08-29 11:02:16 +02:00
|
|
|
}
|
2019-11-11 12:25:41 +01:00
|
|
|
rows.push(tagged_row.into_tagged_value());
|
2019-08-29 11:02:16 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
Ok(Value::Table(rows).tagged(&tag))
|
2019-08-29 11:02:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_tsv(
|
2019-11-11 12:25:41 +01:00
|
|
|
FromTSVArgs { headerless }: FromTSVArgs,
|
2019-08-29 11:02:16 +02:00
|
|
|
RunnableContext { input, name, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-09-14 18:30:24 +02:00
|
|
|
let name_tag = name;
|
2019-08-29 11:02:16 +02:00
|
|
|
|
2019-09-26 02:22:17 +02:00
|
|
|
let stream = async_stream! {
|
2019-08-29 11:02:16 +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();
|
2019-10-13 06:12:43 +02:00
|
|
|
latest_tag = Some(value_tag.clone());
|
2019-08-29 11:02:16 +02:00
|
|
|
match value.item {
|
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
|
|
|
concat_string.push_str(&s);
|
|
|
|
concat_string.push_str("\n");
|
|
|
|
}
|
|
|
|
_ => yield Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-10-13 06:12:43 +02:00
|
|
|
&name_tag,
|
2019-08-29 11:02:16 +02:00
|
|
|
"value originates from here",
|
2019-10-13 06:12:43 +02:00
|
|
|
&value_tag,
|
2019-08-29 11:02:16 +02:00
|
|
|
)),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 12:25:41 +01:00
|
|
|
match from_tsv_string_to_value(concat_string, headerless, name_tag.clone()) {
|
2019-08-29 11:02:16 +02:00
|
|
|
Ok(x) => match x {
|
2019-09-05 18:23:42 +02:00
|
|
|
Tagged { item: Value::Table(list), .. } => {
|
2019-08-29 11:02:16 +02:00
|
|
|
for l in list {
|
|
|
|
yield ReturnSuccess::value(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x => yield ReturnSuccess::value(x),
|
|
|
|
},
|
|
|
|
Err(_) => if let Some(last_tag) = latest_tag {
|
|
|
|
yield Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Could not parse as TSV",
|
|
|
|
"input cannot be parsed as TSV",
|
2019-10-13 06:12:43 +02:00
|
|
|
&name_tag,
|
2019-08-29 11:02:16 +02:00
|
|
|
"value originates from here",
|
2019-10-13 06:12:43 +02:00
|
|
|
&last_tag,
|
2019-08-29 11:02:16 +02:00
|
|
|
))
|
|
|
|
} ,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|