2019-08-19 05:16:39 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-05-25 01:20:03 +00:00
|
|
|
use crate::errors::ShellError;
|
2019-08-01 01:58:42 +00:00
|
|
|
use crate::object::{Primitive, TaggedDictBuilder, Value};
|
2019-05-25 01:20:03 +00:00
|
|
|
use crate::prelude::*;
|
2019-06-01 17:00:42 +00:00
|
|
|
use log::trace;
|
2019-05-25 01:20:03 +00:00
|
|
|
|
2019-08-20 03:15:05 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SplitColumnArgs {
|
2019-08-20 06:11:11 +00:00
|
|
|
separator: Tagged<String>,
|
2019-08-20 03:15:05 +00:00
|
|
|
rest: Vec<Tagged<String>>,
|
|
|
|
}
|
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
pub struct SplitColumn;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for SplitColumn {
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-08-20 03:15:05 +00:00
|
|
|
args.process(registry, split_column)?.run()
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"split-column"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2019-08-20 06:11:11 +00:00
|
|
|
Signature::build("split-column")
|
|
|
|
.required("separator", SyntaxType::Any)
|
|
|
|
.rest()
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 03:15:05 +00:00
|
|
|
fn split_column(
|
2019-08-20 06:11:11 +00:00
|
|
|
SplitColumnArgs { separator, rest }: SplitColumnArgs,
|
2019-08-20 03:15:05 +00:00
|
|
|
RunnableContext { input, name, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-05-25 01:20:03 +00:00
|
|
|
Ok(input
|
2019-07-03 20:31:15 +00:00
|
|
|
.values
|
2019-07-08 16:44:53 +00:00
|
|
|
.map(move |v| match v.item {
|
2019-08-01 01:58:42 +00:00
|
|
|
Value::Primitive(Primitive::String(ref s)) => {
|
2019-08-20 06:11:11 +00:00
|
|
|
let splitter = separator.replace("\\n", "\n");
|
2019-06-01 17:00:42 +00:00
|
|
|
trace!("splitting with {:?}", splitter);
|
2019-05-25 01:20:03 +00:00
|
|
|
|
2019-08-26 11:45:29 +00:00
|
|
|
let split_result: Vec<_> = if splitter.chars().all(|c| c.is_whitespace()) {
|
|
|
|
s.split(&splitter).filter(|s| *s != "").collect()
|
|
|
|
} else {
|
|
|
|
s.split(&splitter).collect()
|
|
|
|
};
|
|
|
|
|
2019-06-01 17:00:42 +00:00
|
|
|
trace!("split result = {:?}", split_result);
|
2019-05-26 06:54:41 +00:00
|
|
|
|
2019-08-20 06:11:11 +00:00
|
|
|
let positional: Vec<_> = rest.iter().map(|f| f.item.clone()).collect();
|
|
|
|
|
2019-05-28 02:01:37 +00:00
|
|
|
// If they didn't provide column names, make up our own
|
2019-08-20 06:11:11 +00:00
|
|
|
if positional.len() == 0 {
|
2019-05-28 02:01:37 +00:00
|
|
|
let mut gen_columns = vec![];
|
|
|
|
for i in 0..split_result.len() {
|
|
|
|
gen_columns.push(format!("Column{}", i + 1));
|
|
|
|
}
|
|
|
|
|
2019-08-05 08:54:29 +00:00
|
|
|
let mut dict = TaggedDictBuilder::new(v.tag());
|
2019-06-22 20:46:16 +00:00
|
|
|
for (&k, v) in split_result.iter().zip(gen_columns.iter()) {
|
2019-07-09 04:31:26 +00:00
|
|
|
dict.insert(v.clone(), Primitive::String(k.into()));
|
2019-05-28 02:01:37 +00:00
|
|
|
}
|
2019-07-09 04:31:26 +00:00
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
ReturnSuccess::value(dict.into_tagged_value())
|
2019-08-20 06:11:11 +00:00
|
|
|
} else if split_result.len() == positional.len() {
|
2019-08-05 08:54:29 +00:00
|
|
|
let mut dict = TaggedDictBuilder::new(v.tag());
|
2019-08-20 06:11:11 +00:00
|
|
|
for (&k, v) in split_result.iter().zip(positional.iter()) {
|
2019-08-20 03:15:05 +00:00
|
|
|
dict.insert(v, Value::Primitive(Primitive::String(k.into())));
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
2019-08-01 01:58:42 +00:00
|
|
|
ReturnSuccess::value(dict.into_tagged_value())
|
2019-05-25 01:20:03 +00:00
|
|
|
} else {
|
2019-08-05 08:54:29 +00:00
|
|
|
let mut dict = TaggedDictBuilder::new(v.tag());
|
2019-08-20 06:11:11 +00:00
|
|
|
for (&k, v) in split_result.iter().zip(positional.iter()) {
|
|
|
|
dict.insert(v, Value::Primitive(Primitive::String(k.into())));
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
2019-08-01 01:58:42 +00:00
|
|
|
ReturnSuccess::value(dict.into_tagged_value())
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-05 08:54:29 +00:00
|
|
|
_ => Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-08-20 03:15:05 +00:00
|
|
|
name,
|
2019-08-05 08:54:29 +00:00
|
|
|
"value originates from here",
|
|
|
|
v.span(),
|
2019-07-03 20:31:15 +00:00
|
|
|
)),
|
2019-05-25 01:20:03 +00:00
|
|
|
})
|
2019-07-03 20:31:15 +00:00
|
|
|
.to_output_stream())
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|