2019-08-29 11:02:16 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-09-05 18:23:42 +02:00
|
|
|
use crate::data::{Primitive, Value};
|
2019-08-29 11:02:16 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
use csv::WriterBuilder;
|
|
|
|
|
|
|
|
pub struct ToTSV;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct ToTSVArgs {
|
|
|
|
headerless: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WholeStreamCommand for ToTSV {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"to-tsv"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2019-09-04 08:48:40 +02:00
|
|
|
Signature::build("to-tsv").switch("headerless")
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Convert table into .tsv text"
|
2019-08-29 11:02:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
args.process(registry, to_tsv)?.run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn value_to_tsv_value(v: &Value) -> Value {
|
|
|
|
match v {
|
|
|
|
Value::Primitive(Primitive::String(s)) => Value::Primitive(Primitive::String(s.clone())),
|
|
|
|
Value::Primitive(Primitive::Nothing) => Value::Primitive(Primitive::Nothing),
|
|
|
|
Value::Primitive(Primitive::Boolean(b)) => Value::Primitive(Primitive::Boolean(b.clone())),
|
2019-09-10 14:00:25 +02:00
|
|
|
Value::Primitive(Primitive::Decimal(f)) => Value::Primitive(Primitive::Decimal(f.clone())),
|
|
|
|
Value::Primitive(Primitive::Int(i)) => Value::Primitive(Primitive::Int(i.clone())),
|
|
|
|
Value::Primitive(Primitive::Path(x)) => Value::Primitive(Primitive::Path(x.clone())),
|
2019-08-29 11:02:16 +02:00
|
|
|
Value::Primitive(Primitive::Bytes(b)) => Value::Primitive(Primitive::Bytes(b.clone())),
|
|
|
|
Value::Primitive(Primitive::Date(d)) => Value::Primitive(Primitive::Date(d.clone())),
|
2019-09-05 18:23:42 +02:00
|
|
|
Value::Row(o) => Value::Row(o.clone()),
|
|
|
|
Value::Table(l) => Value::Table(l.clone()),
|
2019-08-29 11:02:16 +02:00
|
|
|
Value::Block(_) => Value::Primitive(Primitive::Nothing),
|
|
|
|
_ => Value::Primitive(Primitive::Nothing),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 08:48:40 +02:00
|
|
|
fn to_string_helper(v: &Value) -> Result<String, ShellError> {
|
2019-08-29 11:02:16 +02:00
|
|
|
match v {
|
|
|
|
Value::Primitive(Primitive::Date(d)) => Ok(d.to_string()),
|
2019-08-30 19:29:04 +02:00
|
|
|
Value::Primitive(Primitive::Bytes(b)) => Ok(format!("{}", b)),
|
2019-08-29 11:02:16 +02:00
|
|
|
Value::Primitive(Primitive::Boolean(_)) => Ok(v.as_string()?),
|
2019-09-10 14:00:25 +02:00
|
|
|
Value::Primitive(Primitive::Decimal(_)) => Ok(v.as_string()?),
|
|
|
|
Value::Primitive(Primitive::Int(_)) => Ok(v.as_string()?),
|
|
|
|
Value::Primitive(Primitive::Path(_)) => Ok(v.as_string()?),
|
2019-09-05 18:23:42 +02:00
|
|
|
Value::Table(_) => return Ok(String::from("[table]")),
|
|
|
|
Value::Row(_) => return Ok(String::from("[row]")),
|
2019-08-29 11:02:16 +02:00
|
|
|
Value::Primitive(Primitive::String(s)) => return Ok(s.to_string()),
|
2019-09-10 14:00:25 +02:00
|
|
|
_ => return Err(ShellError::string("Unexpected value")),
|
2019-08-29 11:02:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 08:48:40 +02:00
|
|
|
fn merge_descriptors(values: &[Tagged<Value>]) -> Vec<String> {
|
|
|
|
let mut ret = vec![];
|
|
|
|
for value in values {
|
|
|
|
for desc in value.data_descriptors() {
|
|
|
|
if !ret.contains(&desc) {
|
|
|
|
ret.push(desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_string(v: &Value) -> Result<String, ShellError> {
|
2019-08-29 11:02:16 +02:00
|
|
|
match v {
|
2019-09-05 18:23:42 +02:00
|
|
|
Value::Row(o) => {
|
2019-08-29 11:02:16 +02:00
|
|
|
let mut wtr = WriterBuilder::new().delimiter(b'\t').from_writer(vec![]);
|
|
|
|
let mut fields: VecDeque<String> = VecDeque::new();
|
|
|
|
let mut values: VecDeque<String> = VecDeque::new();
|
|
|
|
|
|
|
|
for (k, v) in o.entries.iter() {
|
|
|
|
fields.push_back(k.clone());
|
|
|
|
values.push_back(to_string_helper(&v)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
wtr.write_record(fields).expect("can not write.");
|
|
|
|
wtr.write_record(values).expect("can not write.");
|
|
|
|
|
2019-09-04 08:48:40 +02:00
|
|
|
return Ok(String::from_utf8(
|
|
|
|
wtr.into_inner()
|
|
|
|
.map_err(|_| ShellError::string("Could not convert record"))?,
|
|
|
|
)
|
|
|
|
.map_err(|_| ShellError::string("Could not convert record"))?);
|
|
|
|
}
|
2019-09-05 18:23:42 +02:00
|
|
|
Value::Table(list) => {
|
2019-09-04 08:48:40 +02:00
|
|
|
let mut wtr = WriterBuilder::new().delimiter(b'\t').from_writer(vec![]);
|
|
|
|
|
|
|
|
let merged_descriptors = merge_descriptors(&list);
|
|
|
|
wtr.write_record(&merged_descriptors)
|
|
|
|
.expect("can not write.");
|
|
|
|
|
|
|
|
for l in list {
|
|
|
|
let mut row = vec![];
|
|
|
|
for desc in &merged_descriptors {
|
|
|
|
match l.item.get_data_by_key(&desc) {
|
|
|
|
Some(s) => {
|
|
|
|
row.push(to_string_helper(s)?);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
row.push(String::new());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wtr.write_record(&row).expect("can not write");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(String::from_utf8(
|
|
|
|
wtr.into_inner()
|
|
|
|
.map_err(|_| ShellError::string("Could not convert record"))?,
|
|
|
|
)
|
|
|
|
.map_err(|_| ShellError::string("Could not convert record"))?);
|
2019-08-29 11:02:16 +02:00
|
|
|
}
|
|
|
|
_ => return to_string_helper(&v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_tsv(
|
|
|
|
ToTSVArgs { headerless }: ToTSVArgs,
|
|
|
|
RunnableContext { input, name, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-09-14 18:30:24 +02:00
|
|
|
let name_tag = name;
|
2019-09-26 02:22:17 +02:00
|
|
|
let stream = async_stream! {
|
2019-09-04 08:48:40 +02:00
|
|
|
let input: Vec<Tagged<Value>> = input.values.collect().await;
|
|
|
|
|
|
|
|
let to_process_input = if input.len() > 1 {
|
|
|
|
let tag = input[0].tag;
|
2019-09-05 18:23:42 +02:00
|
|
|
vec![Tagged { item: Value::Table(input), tag } ]
|
2019-09-04 08:48:40 +02:00
|
|
|
} else if input.len() == 1 {
|
|
|
|
input
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
|
|
|
|
for value in to_process_input {
|
|
|
|
match to_string(&value_to_tsv_value(&value.item)) {
|
|
|
|
Ok(x) => {
|
|
|
|
let converted = if headerless {
|
|
|
|
x.lines().skip(1).collect()
|
|
|
|
} else {
|
|
|
|
x
|
|
|
|
};
|
2019-09-14 18:30:24 +02:00
|
|
|
yield ReturnSuccess::value(Value::Primitive(Primitive::String(converted)).tagged(name_tag))
|
2019-09-04 08:48:40 +02:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
yield Err(ShellError::labeled_error_with_secondary(
|
2019-09-14 18:30:24 +02:00
|
|
|
"Expected a table with TSV-compatible structure.tag() from pipeline",
|
2019-09-04 08:48:40 +02:00
|
|
|
"requires TSV-compatible input",
|
2019-09-14 18:30:24 +02:00
|
|
|
name_tag,
|
2019-09-04 08:48:40 +02:00
|
|
|
"originates from here".to_string(),
|
2019-09-14 18:30:24 +02:00
|
|
|
value.tag(),
|
2019-09-04 08:48:40 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
2019-08-29 11:02:16 +02:00
|
|
|
}
|