2019-08-19 07:16:39 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-07-21 09:08:05 +02:00
|
|
|
use crate::object::{Primitive, Value};
|
|
|
|
use crate::prelude::*;
|
|
|
|
use csv::WriterBuilder;
|
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
pub struct ToCSV;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for ToCSV {
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
to_csv(args, registry)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"to-csv"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("to-csv")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 09:08:05 +02:00
|
|
|
pub fn value_to_csv_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::Object(o) => Value::Object(o.clone()),
|
|
|
|
Value::List(l) => Value::List(l.clone()),
|
|
|
|
Value::Block(_) => Value::Primitive(Primitive::Nothing),
|
2019-07-24 00:22:11 +02:00
|
|
|
_ => Value::Primitive(Primitive::Nothing),
|
2019-07-21 09:08:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_string(v: &Value) -> Result<String, Box<dyn std::error::Error>> {
|
|
|
|
match v {
|
|
|
|
Value::List(_l) => return Ok(String::from("[list list]")),
|
|
|
|
Value::Object(o) => {
|
|
|
|
let mut wtr = WriterBuilder::new().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(&v)?);
|
|
|
|
}
|
2019-07-24 00:22:11 +02:00
|
|
|
|
2019-07-21 09:08:05 +02:00
|
|
|
wtr.write_record(fields).expect("can not write.");
|
|
|
|
wtr.write_record(values).expect("can not write.");
|
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
return Ok(String::from_utf8(wtr.into_inner()?)?);
|
|
|
|
}
|
2019-07-21 09:08:05 +02:00
|
|
|
Value::Primitive(Primitive::String(s)) => return Ok(s.to_string()),
|
2019-07-24 00:22:11 +02:00
|
|
|
_ => return Err("Bad input".into()),
|
2019-07-21 09:08:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
fn to_csv(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
2019-07-24 00:22:11 +02:00
|
|
|
let args = args.evaluate_once(registry)?;
|
|
|
|
let name_span = args.name_span();
|
2019-07-21 09:08:05 +02:00
|
|
|
let out = args.input;
|
2019-07-24 00:22:11 +02:00
|
|
|
|
2019-07-21 09:08:05 +02:00
|
|
|
Ok(out
|
|
|
|
.values
|
2019-07-24 00:22:11 +02:00
|
|
|
.map(move |a| match to_string(&value_to_csv_value(&a.item)) {
|
2019-08-05 10:54:29 +02:00
|
|
|
Ok(x) => ReturnSuccess::value(
|
|
|
|
Value::Primitive(Primitive::String(x)).simple_spanned(name_span),
|
|
|
|
),
|
2019-08-06 05:03:13 +02:00
|
|
|
_ => Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Expected an object with CSV-compatible structure from pipeline",
|
|
|
|
"requires CSV-compatible input",
|
2019-07-24 00:22:11 +02:00
|
|
|
name_span,
|
2019-08-06 05:03:13 +02:00
|
|
|
format!("{} originates from here", a.item.type_name()),
|
|
|
|
a.span(),
|
2019-07-24 00:22:11 +02:00
|
|
|
)),
|
|
|
|
})
|
2019-07-21 09:08:05 +02:00
|
|
|
.to_output_stream())
|
|
|
|
}
|