nushell/src/commands/split_column.rs

74 lines
2.8 KiB
Rust
Raw Normal View History

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::*;
use log::trace;
2019-05-25 01:20:03 +00:00
2019-07-23 22:22:11 +00:00
pub fn split_column(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(registry)?;
let span = args.name_span();
let (input, args) = args.parts();
let positional: Vec<_> = args.positional.iter().flatten().cloned().collect();
2019-06-22 03:43:37 +00:00
if positional.len() == 0 {
return Err(ShellError::labeled_error(
2019-06-18 00:39:09 +00:00
"Split-column needs more information",
"needs parameter (eg split-column \",\")",
2019-07-23 22:22:11 +00:00
span,
2019-06-18 00:39:09 +00:00
));
}
2019-06-22 03:43:37 +00:00
2019-05-25 01:20:03 +00:00
Ok(input
.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-06-22 03:43:37 +00:00
let splitter = positional[0].as_string().unwrap().replace("\\n", "\n");
trace!("splitting with {:?}", splitter);
2019-05-25 01:20:03 +00:00
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
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));
}
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-06-22 03:43:37 +00:00
} else if split_result.len() == (positional.len() - 1) {
let mut dict = TaggedDictBuilder::new(v.tag());
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-08-01 01:58:42 +00:00
ReturnSuccess::value(dict.into_tagged_value())
2019-05-25 01:20:03 +00:00
} else {
let mut dict = TaggedDictBuilder::new(v.tag());
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-08-01 01:58:42 +00:00
ReturnSuccess::value(dict.into_tagged_value())
2019-05-25 01:20:03 +00:00
}
}
_ => Err(ShellError::labeled_error_with_secondary(
"Expected a string from pipeline",
"requires string input",
2019-06-15 23:03:49 +00:00
span,
"value originates from here",
v.span(),
)),
2019-05-25 01:20:03 +00:00
})
.to_output_stream())
2019-05-25 01:20:03 +00:00
}