2019-05-25 01:20:03 +00:00
|
|
|
use crate::errors::ShellError;
|
2019-07-09 04:31:26 +00:00
|
|
|
use crate::object::{Primitive, SpannedDictBuilder, 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-05-31 20:34:15 +00:00
|
|
|
pub fn split_column(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2019-06-22 03:43:37 +00:00
|
|
|
let positional: Vec<_> = args.positional_iter().cloned().collect();
|
|
|
|
let span = args.name_span;
|
2019-07-03 20:31:15 +00:00
|
|
|
|
2019-06-22 03:43:37 +00:00
|
|
|
if positional.len() == 0 {
|
2019-06-18 00:39:09 +00:00
|
|
|
return Err(ShellError::maybe_labeled_error(
|
|
|
|
"Split-column needs more information",
|
|
|
|
"needs parameter (eg split-column \",\")",
|
|
|
|
args.name_span,
|
|
|
|
));
|
|
|
|
}
|
2019-06-22 03:43:37 +00:00
|
|
|
|
2019-05-25 01:20:03 +00:00
|
|
|
let input = args.input;
|
|
|
|
|
|
|
|
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-05-25 01:20:03 +00:00
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
2019-06-22 03:43:37 +00:00
|
|
|
let splitter = positional[0].as_string().unwrap().replace("\\n", "\n");
|
2019-06-01 17:00:42 +00:00
|
|
|
trace!("splitting with {:?}", splitter);
|
2019-05-25 01:20:03 +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-26 06:54:41 +00:00
|
|
|
|
2019-05-28 02:01:37 +00:00
|
|
|
// If they didn't provide column names, make up our own
|
2019-06-22 03:43:37 +00:00
|
|
|
if (positional.len() - 1) == 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-07-09 04:31:26 +00:00
|
|
|
let mut dict = SpannedDictBuilder::new(v.span);
|
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
|
|
|
|
|
|
|
ReturnSuccess::value(dict.into_spanned_value())
|
2019-06-22 03:43:37 +00:00
|
|
|
} else if split_result.len() == (positional.len() - 1) {
|
2019-07-09 04:31:26 +00:00
|
|
|
let mut dict = SpannedDictBuilder::new(v.span);
|
2019-06-22 20:46:16 +00:00
|
|
|
for (&k, v) in split_result.iter().zip(positional.iter().skip(1)) {
|
2019-07-09 04:31:26 +00:00
|
|
|
dict.insert(
|
2019-05-26 06:54:41 +00:00
|
|
|
v.as_string().unwrap(),
|
2019-06-22 20:46:16 +00:00
|
|
|
Value::Primitive(Primitive::String(k.into())),
|
2019-05-26 06:54:41 +00:00
|
|
|
);
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
2019-07-09 04:31:26 +00:00
|
|
|
ReturnSuccess::value(dict.into_spanned_value())
|
2019-05-25 01:20:03 +00:00
|
|
|
} else {
|
2019-07-09 04:31:26 +00:00
|
|
|
let mut dict = SpannedDictBuilder::new(v.span);
|
2019-06-22 03:43:37 +00:00
|
|
|
for k in positional.iter().skip(1) {
|
2019-07-09 04:31:26 +00:00
|
|
|
dict.insert(k.as_string().unwrap().trim(), Primitive::String("".into()));
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
2019-07-09 04:31:26 +00:00
|
|
|
ReturnSuccess::value(dict.into_spanned_value())
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-03 20:31:15 +00:00
|
|
|
_ => Err(ShellError::maybe_labeled_error(
|
2019-06-15 23:03:49 +00:00
|
|
|
"Expected string values from pipeline",
|
|
|
|
"expects strings from pipeline",
|
|
|
|
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
|
|
|
}
|