Port split column and split row

This commit is contained in:
JT 2021-10-09 15:41:39 +13:00
parent 44fbf0fce3
commit 60f9fe1aa4
3 changed files with 32 additions and 56 deletions

View File

@ -43,6 +43,8 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
working_set.add_decl(Box::new(Select));
working_set.add_decl(Box::new(Split));
working_set.add_decl(Box::new(SplitChars));
working_set.add_decl(Box::new(SplitColumn));
working_set.add_decl(Box::new(SplitRow));
working_set.add_decl(Box::new(Sys));
working_set.add_decl(Box::new(Table));
working_set.add_decl(Box::new(Touch));

View File

@ -65,69 +65,21 @@ fn split_chars(call: &Call, input: Value) -> Result<nu_protocol::Value, nu_proto
Value::List { vals, span } => Value::List {
vals: vals
.iter()
.flat_map(move |v| {
if let Ok(s) = v.as_string() {
let v_span = v.span();
s.chars()
.collect::<Vec<_>>()
.into_iter()
.map(move |x| Value::String {
val: x.to_string(),
span: v_span,
})
.collect()
} else {
vec![Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: name,
origin: v.span(),
},
}]
}
})
.flat_map(|x| split_chars_helper(x, name))
.collect(),
span,
},
Value::Stream { stream, span } => Value::Stream {
stream: stream
.flat_map(move |v| {
if let Ok(s) = v.as_string() {
let v_span = v.span();
s.chars()
.collect::<Vec<_>>()
.into_iter()
.map(move |x| Value::String {
val: x.to_string(),
span: v_span,
})
.collect()
} else {
vec![Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: name,
origin: v.span(),
},
}]
}
})
.flat_map(move |x| split_chars_helper(&x, name))
.into_value_stream(),
span,
},
v => {
let v_span = v.span();
if let Ok(s) = v.as_string() {
if v.as_string().is_ok() {
Value::List {
vals: s
.chars()
.collect::<Vec<_>>()
.into_iter()
.map(move |x| Value::String {
val: x.to_string(),
span: v_span,
})
.collect(),
vals: split_chars_helper(&v, name),
span: v_span,
}
} else {
@ -143,6 +95,28 @@ fn split_chars(call: &Call, input: Value) -> Result<nu_protocol::Value, nu_proto
})
}
fn split_chars_helper(v: &Value, name: Span) -> Vec<Value> {
if let Ok(s) = v.as_string() {
let v_span = v.span();
s.chars()
.collect::<Vec<_>>()
.into_iter()
.map(move |x| Value::String {
val: x.to_string(),
span: v_span,
})
.collect()
} else {
vec![Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: name,
origin: v.span(),
},
}]
}
}
// #[cfg(test)]
// mod tests {
// use super::ShellError;

View File

@ -1,9 +1,9 @@
pub mod chars;
// pub mod column;
pub mod column;
pub mod command;
// pub mod row;
pub mod row;
pub use chars::SubCommand as SplitChars;
// pub use column::SubCommand as SplitColumn;
pub use column::SubCommand as SplitColumn;
pub use command::SplitCommand as Split;
// pub use row::SubCommand as SplitRow;
pub use row::SubCommand as SplitRow;