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(Select));
working_set.add_decl(Box::new(Split)); working_set.add_decl(Box::new(Split));
working_set.add_decl(Box::new(SplitChars)); 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(Sys));
working_set.add_decl(Box::new(Table)); working_set.add_decl(Box::new(Table));
working_set.add_decl(Box::new(Touch)); 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 { Value::List { vals, span } => Value::List {
vals: vals vals: vals
.iter() .iter()
.flat_map(move |v| { .flat_map(|x| split_chars_helper(x, name))
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(),
},
}]
}
})
.collect(), .collect(),
span, span,
}, },
Value::Stream { stream, span } => Value::Stream { Value::Stream { stream, span } => Value::Stream {
stream: stream stream: stream
.flat_map(move |v| { .flat_map(move |x| split_chars_helper(&x, name))
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(),
},
}]
}
})
.into_value_stream(), .into_value_stream(),
span, span,
}, },
v => { v => {
let v_span = v.span(); let v_span = v.span();
if let Ok(s) = v.as_string() { if v.as_string().is_ok() {
Value::List { Value::List {
vals: s vals: split_chars_helper(&v, name),
.chars()
.collect::<Vec<_>>()
.into_iter()
.map(move |x| Value::String {
val: x.to_string(),
span: v_span,
})
.collect(),
span: v_span, span: v_span,
} }
} else { } 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)] // #[cfg(test)]
// mod tests { // mod tests {
// use super::ShellError; // use super::ShellError;

View File

@ -1,9 +1,9 @@
pub mod chars; pub mod chars;
// pub mod column; pub mod column;
pub mod command; pub mod command;
// pub mod row; pub mod row;
pub use chars::SubCommand as SplitChars; 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 command::SplitCommand as Split;
// pub use row::SubCommand as SplitRow; pub use row::SubCommand as SplitRow;