2019-05-30 07:08:42 +02:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::object::{Primitive, Value};
|
2019-06-22 05:43:37 +02:00
|
|
|
use crate::parser::Spanned;
|
2019-05-30 07:08:42 +02:00
|
|
|
use crate::prelude::*;
|
2019-06-01 19:00:42 +02:00
|
|
|
use log::trace;
|
2019-05-30 07:08:42 +02:00
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
pub fn split_row(
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once(registry)?;
|
|
|
|
let span = args.name_span();
|
|
|
|
let len = args.len();
|
|
|
|
let (input, args) = args.parts();
|
2019-06-22 05:43:37 +02:00
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
let positional: Vec<Spanned<Value>> = args.positional.iter().flatten().cloned().collect();
|
|
|
|
|
|
|
|
if len == 0 {
|
2019-06-18 02:39:09 +02:00
|
|
|
return Err(ShellError::maybe_labeled_error(
|
|
|
|
"Split-row needs more information",
|
|
|
|
"needs parameter (eg split-row \"\\n\")",
|
2019-07-24 00:22:11 +02:00
|
|
|
span,
|
2019-06-18 02:39:09 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-05-30 07:08:42 +02:00
|
|
|
let stream = input
|
2019-07-03 22:31:15 +02:00
|
|
|
.values
|
2019-07-08 18:44:53 +02:00
|
|
|
.map(move |v| match v.item {
|
2019-05-30 07:08:42 +02:00
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
2019-06-22 05:43:37 +02:00
|
|
|
let splitter = positional[0].as_string().unwrap().replace("\\n", "\n");
|
2019-06-01 19:00:42 +02:00
|
|
|
trace!("splitting with {:?}", splitter);
|
2019-05-30 07:08:42 +02:00
|
|
|
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
|
|
|
|
|
2019-06-01 19:00:42 +02:00
|
|
|
trace!("split result = {:?}", split_result);
|
2019-05-30 07:08:42 +02:00
|
|
|
|
|
|
|
let mut result = VecDeque::new();
|
|
|
|
for s in split_result {
|
2019-07-08 18:44:53 +02:00
|
|
|
result.push_back(ReturnSuccess::value(
|
|
|
|
Value::Primitive(Primitive::String(s.into())).spanned(v.span),
|
|
|
|
));
|
2019-05-30 07:08:42 +02:00
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
_ => {
|
2019-06-16 01:03:49 +02:00
|
|
|
let mut result = VecDeque::new();
|
2019-07-03 22:31:15 +02:00
|
|
|
result.push_back(Err(ShellError::maybe_labeled_error(
|
|
|
|
"Expected string values from pipeline",
|
|
|
|
"expects strings from pipeline",
|
|
|
|
span,
|
|
|
|
)));
|
2019-05-30 07:08:42 +02:00
|
|
|
result
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.flatten();
|
|
|
|
|
2019-07-03 22:31:15 +02:00
|
|
|
Ok(stream.to_output_stream())
|
2019-05-30 07:08:42 +02:00
|
|
|
}
|