forked from extern/nushell
Merge branch 'main' of https://github.com/nushell/engine-q into unit-test
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
mod build_string;
|
||||
mod split;
|
||||
|
||||
pub use build_string::BuildString;
|
||||
pub use split::*;
|
||||
|
100
crates/nu-command/src/strings/split/chars.rs
Normal file
100
crates/nu-command/src/strings/split/chars.rs
Normal file
@ -0,0 +1,100 @@
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EvaluationContext},
|
||||
Example, ShellError, Signature, Span, Type, Value,
|
||||
};
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
impl Command for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"split chars"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("split chars")
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"splits a string's characters into separate rows"
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_context: &EvaluationContext,
|
||||
call: &Call,
|
||||
input: Value,
|
||||
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
|
||||
split_chars(call, input)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Split the string's characters into separate rows",
|
||||
example: "echo 'hello' | split chars",
|
||||
result: Some(vec![
|
||||
Value::String {
|
||||
val: "h".into(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::String {
|
||||
val: "e".into(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::String {
|
||||
val: "l".into(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::String {
|
||||
val: "l".into(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::String {
|
||||
val: "o".into(),
|
||||
span: Span::unknown(),
|
||||
},
|
||||
]),
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
fn split_chars(call: &Call, input: Value) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
|
||||
let span = call.head;
|
||||
|
||||
Ok(input.flat_map(span, move |x| split_chars_helper(&x, span)))
|
||||
}
|
||||
|
||||
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;
|
||||
// use super::SubCommand;
|
||||
|
||||
// #[test]
|
||||
// fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||
// use crate::examples::test as test_examples;
|
||||
|
||||
// test_examples(SubCommand {})
|
||||
// }
|
||||
// }
|
130
crates/nu-command/src/strings/split/column.rs
Normal file
130
crates/nu-command/src/strings/split/column.rs
Normal file
@ -0,0 +1,130 @@
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EvaluationContext},
|
||||
ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
impl Command for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"split column"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("split column")
|
||||
.required(
|
||||
"separator",
|
||||
SyntaxShape::String,
|
||||
"the character that denotes what separates columns",
|
||||
)
|
||||
.switch("collapse-empty", "remove empty columns", Some('c'))
|
||||
.rest(
|
||||
"rest",
|
||||
SyntaxShape::String,
|
||||
"column names to give the new columns",
|
||||
)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"splits contents across multiple columns via the separator."
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
context: &EvaluationContext,
|
||||
call: &Call,
|
||||
input: Value,
|
||||
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
|
||||
split_column(context, call, input)
|
||||
}
|
||||
}
|
||||
|
||||
fn split_column(
|
||||
context: &EvaluationContext,
|
||||
call: &Call,
|
||||
input: Value,
|
||||
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
|
||||
let name_span = call.head;
|
||||
let separator: Spanned<String> = call.req(context, 0)?;
|
||||
let rest: Vec<Spanned<String>> = call.rest(context, 1)?;
|
||||
let collapse_empty = call.has_flag("collapse-empty");
|
||||
|
||||
Ok(input.map(name_span, move |x| {
|
||||
split_column_helper(&x, &separator, &rest, collapse_empty, name_span)
|
||||
}))
|
||||
}
|
||||
|
||||
fn split_column_helper(
|
||||
v: &Value,
|
||||
separator: &Spanned<String>,
|
||||
rest: &[Spanned<String>],
|
||||
collapse_empty: bool,
|
||||
head: Span,
|
||||
) -> Value {
|
||||
if let Ok(s) = v.as_string() {
|
||||
let splitter = separator.item.replace("\\n", "\n");
|
||||
|
||||
let split_result: Vec<_> = if collapse_empty {
|
||||
s.split(&splitter).filter(|s| !s.is_empty()).collect()
|
||||
} else {
|
||||
s.split(&splitter).collect()
|
||||
};
|
||||
|
||||
let positional: Vec<_> = rest.iter().map(|f| f.item.clone()).collect();
|
||||
|
||||
// If they didn't provide column names, make up our own
|
||||
|
||||
let mut cols = vec![];
|
||||
let mut vals = vec![];
|
||||
if positional.is_empty() {
|
||||
let mut gen_columns = vec![];
|
||||
for i in 0..split_result.len() {
|
||||
gen_columns.push(format!("Column{}", i + 1));
|
||||
}
|
||||
|
||||
for (&k, v) in split_result.iter().zip(&gen_columns) {
|
||||
cols.push(v.to_string());
|
||||
vals.push(Value::String {
|
||||
val: k.into(),
|
||||
span: head,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
for (&k, v) in split_result.iter().zip(&positional) {
|
||||
cols.push(v.into());
|
||||
vals.push(Value::String {
|
||||
val: k.into(),
|
||||
span: head,
|
||||
})
|
||||
}
|
||||
}
|
||||
Value::Record {
|
||||
cols,
|
||||
vals,
|
||||
span: head,
|
||||
}
|
||||
} else {
|
||||
Value::Error {
|
||||
error: ShellError::PipelineMismatch {
|
||||
expected: Type::String,
|
||||
expected_span: head,
|
||||
origin: v.span(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::ShellError;
|
||||
// use super::SubCommand;
|
||||
|
||||
// #[test]
|
||||
// fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||
// use crate::examples::test as test_examples;
|
||||
|
||||
// test_examples(SubCommand {})
|
||||
// }
|
||||
// }
|
48
crates/nu-command/src/strings/split/command.rs
Normal file
48
crates/nu-command/src/strings/split/command.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use nu_engine::get_full_help;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EvaluationContext},
|
||||
Signature, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SplitCommand;
|
||||
|
||||
impl Command for SplitCommand {
|
||||
fn name(&self) -> &str {
|
||||
"split"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("split")
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Split contents across desired subcommand (like row, column) via the separator."
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
context: &EvaluationContext,
|
||||
call: &Call,
|
||||
_input: Value,
|
||||
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
|
||||
Ok(Value::String {
|
||||
val: get_full_help(&SplitCommand.signature(), &SplitCommand.examples(), context),
|
||||
span: call.head,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::Command;
|
||||
// use super::ShellError;
|
||||
|
||||
// #[test]
|
||||
// fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||
// use crate::examples::test as test_examples;
|
||||
|
||||
// test_examples(Command {})
|
||||
// }
|
||||
// }
|
9
crates/nu-command/src/strings/split/mod.rs
Normal file
9
crates/nu-command/src/strings/split/mod.rs
Normal file
@ -0,0 +1,9 @@
|
||||
pub mod chars;
|
||||
pub mod column;
|
||||
pub mod command;
|
||||
pub mod row;
|
||||
|
||||
pub use chars::SubCommand as SplitChars;
|
||||
pub use column::SubCommand as SplitColumn;
|
||||
pub use command::SplitCommand as Split;
|
||||
pub use row::SubCommand as SplitRow;
|
87
crates/nu-command/src/strings/split/row.rs
Normal file
87
crates/nu-command/src/strings/split/row.rs
Normal file
@ -0,0 +1,87 @@
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EvaluationContext},
|
||||
ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
impl Command for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"split row"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("split row").required(
|
||||
"separator",
|
||||
SyntaxShape::String,
|
||||
"the character that denotes what separates rows",
|
||||
)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"splits contents over multiple rows via the separator."
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
context: &EvaluationContext,
|
||||
call: &Call,
|
||||
input: Value,
|
||||
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
|
||||
split_row(context, call, input)
|
||||
}
|
||||
}
|
||||
|
||||
fn split_row(
|
||||
context: &EvaluationContext,
|
||||
call: &Call,
|
||||
input: Value,
|
||||
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
|
||||
let name_span = call.head;
|
||||
let separator: Spanned<String> = call.req(context, 0)?;
|
||||
|
||||
Ok(input.flat_map(name_span, move |x| {
|
||||
split_row_helper(&x, &separator, name_span)
|
||||
}))
|
||||
}
|
||||
|
||||
fn split_row_helper(v: &Value, separator: &Spanned<String>, name: Span) -> Vec<Value> {
|
||||
if let Ok(s) = v.as_string() {
|
||||
let splitter = separator.item.replace("\\n", "\n");
|
||||
s.split(&splitter)
|
||||
.filter_map(|s| {
|
||||
if s.trim() != "" {
|
||||
Some(Value::String {
|
||||
val: s.into(),
|
||||
span: v.span(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
vec![Value::Error {
|
||||
error: ShellError::PipelineMismatch {
|
||||
expected: Type::String,
|
||||
expected_span: name,
|
||||
origin: v.span(),
|
||||
},
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::ShellError;
|
||||
// use super::SubCommand;
|
||||
|
||||
// #[test]
|
||||
// fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||
// use crate::examples::test as test_examples;
|
||||
|
||||
// test_examples(SubCommand {})
|
||||
// }
|
||||
// }
|
Reference in New Issue
Block a user