2019-05-25 01:20:03 +00:00
|
|
|
use crate::errors::ShellError;
|
2019-05-26 06:54:41 +00:00
|
|
|
use crate::object::{Primitive, Value};
|
2019-05-25 01:20:03 +00:00
|
|
|
use crate::prelude::*;
|
2019-05-26 06:54:41 +00:00
|
|
|
use log::debug;
|
2019-05-25 01:20:03 +00:00
|
|
|
|
|
|
|
// TODO: "Amount remaining" wrapper
|
|
|
|
|
|
|
|
pub fn split(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
//let splitter = args.args[0].as_string()?;
|
|
|
|
let input = args.input;
|
|
|
|
let args = args.args;
|
|
|
|
|
|
|
|
Ok(input
|
|
|
|
.map(move |v| match v {
|
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
|
|
|
let splitter = args[0].as_string().unwrap();
|
2019-05-26 06:54:41 +00:00
|
|
|
debug!("splitting with {:?}", splitter);
|
2019-05-25 01:20:03 +00:00
|
|
|
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
|
|
|
|
|
2019-05-26 06:54:41 +00:00
|
|
|
debug!("split result = {:?}", split_result);
|
|
|
|
|
2019-05-25 01:20:03 +00:00
|
|
|
if split_result.len() == (args.len() - 1) {
|
|
|
|
let mut dict = crate::object::Dictionary::default();
|
|
|
|
for (k, v) in split_result.iter().zip(args.iter().skip(1)) {
|
2019-05-26 06:54:41 +00:00
|
|
|
dict.add(
|
|
|
|
v.as_string().unwrap(),
|
|
|
|
Value::Primitive(Primitive::String(k.to_string())),
|
|
|
|
);
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
|
|
|
ReturnValue::Value(Value::Object(dict))
|
|
|
|
} else {
|
|
|
|
let mut dict = crate::object::Dictionary::default();
|
|
|
|
for k in args.iter().skip(1) {
|
2019-05-26 06:54:41 +00:00
|
|
|
dict.add(
|
|
|
|
k.as_string().unwrap().trim(),
|
|
|
|
Value::Primitive(Primitive::String("".to_string())),
|
|
|
|
);
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
|
|
|
ReturnValue::Value(Value::Object(dict))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ReturnValue::Value(Value::Object(crate::object::Dictionary::default())),
|
|
|
|
})
|
|
|
|
.boxed())
|
|
|
|
}
|