2019-08-19 05:16:39 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-09-05 16:23:42 +00:00
|
|
|
use crate::data::{Primitive, Value};
|
2019-09-11 14:36:50 +00:00
|
|
|
use crate::errors::ShellError;
|
2019-05-30 05:08:42 +00:00
|
|
|
use crate::prelude::*;
|
2019-06-01 17:00:42 +00:00
|
|
|
use log::trace;
|
2019-05-30 05:08:42 +00:00
|
|
|
|
2019-08-20 03:15:05 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SplitRowArgs {
|
2019-08-20 06:11:11 +00:00
|
|
|
separator: Tagged<String>,
|
2019-08-20 03:15:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
pub struct SplitRow;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for SplitRow {
|
2019-08-29 22:52:32 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"split-row"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2019-09-11 14:36:50 +00:00
|
|
|
Signature::build("split-row").required("separator", SyntaxType::Any)
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Split row contents over multiple rows via the separator."
|
|
|
|
}
|
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-08-20 03:15:05 +00:00
|
|
|
args.process(registry, split_row)?.run()
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 03:15:05 +00:00
|
|
|
fn split_row(
|
2019-08-20 06:11:11 +00:00
|
|
|
SplitRowArgs { separator }: SplitRowArgs,
|
2019-08-20 03:15:05 +00:00
|
|
|
RunnableContext { input, name, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-05-30 05:08:42 +00:00
|
|
|
let stream = 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.item.replace("\\n", "\n");
|
2019-06-01 17:00:42 +00:00
|
|
|
trace!("splitting with {:?}", splitter);
|
2019-05-30 05:08:42 +00:00
|
|
|
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
|
|
|
|
|
2019-06-01 17:00:42 +00:00
|
|
|
trace!("split result = {:?}", split_result);
|
2019-05-30 05:08:42 +00:00
|
|
|
|
|
|
|
let mut result = VecDeque::new();
|
|
|
|
for s in split_result {
|
2019-07-08 16:44:53 +00:00
|
|
|
result.push_back(ReturnSuccess::value(
|
2019-08-06 03:03:13 +00:00
|
|
|
Value::Primitive(Primitive::String(s.into())).tagged(v.tag()),
|
2019-07-08 16:44:53 +00:00
|
|
|
));
|
2019-05-30 05:08:42 +00:00
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
_ => {
|
2019-06-15 23:03:49 +00:00
|
|
|
let mut result = VecDeque::new();
|
2019-08-05 08:54:29 +00:00
|
|
|
result.push_back(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-30 05:08:42 +00:00
|
|
|
result
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.flatten();
|
|
|
|
|
2019-07-03 20:31:15 +00:00
|
|
|
Ok(stream.to_output_stream())
|
2019-05-30 05:08:42 +00:00
|
|
|
}
|