forked from extern/nushell
@ -6,9 +6,9 @@ use nu_protocol::{
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Case;
|
||||
pub struct Str;
|
||||
|
||||
impl Command for Case {
|
||||
impl Command for Str {
|
||||
fn name(&self) -> &str {
|
||||
"str"
|
||||
}
|
||||
@ -18,7 +18,7 @@ impl Command for Case {
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Converts strings into different kind of cases: camel, kebab, pascal, snake, and screaming snake."
|
||||
"Various commands for working with string data."
|
||||
}
|
||||
|
||||
fn run(
|
||||
@ -29,7 +29,7 @@ impl Command for Case {
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(Value::String {
|
||||
val: get_full_help(&Case.signature(), &Case.examples(), engine_state),
|
||||
val: get_full_help(&Str.signature(), &Str.examples(), engine_state),
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
@ -44,6 +44,6 @@ mod test {
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
test_examples(Case {})
|
||||
test_examples(Str {})
|
||||
}
|
||||
}
|
||||
|
@ -5,12 +5,12 @@ pub mod pascal_case;
|
||||
pub mod screaming_snake_case;
|
||||
pub mod snake_case;
|
||||
|
||||
pub use camel_case::SubCommand as CamelCase;
|
||||
pub use command::Case;
|
||||
pub use kebab_case::SubCommand as KebabCase;
|
||||
pub use pascal_case::SubCommand as PascalCase;
|
||||
pub use screaming_snake_case::SubCommand as ScreamingSnakeCase;
|
||||
pub use snake_case::SubCommand as SnakeCase;
|
||||
pub use camel_case::SubCommand as StrCamelCase;
|
||||
pub use command::Str;
|
||||
pub use kebab_case::SubCommand as StrKebabCase;
|
||||
pub use pascal_case::SubCommand as StrPascalCase;
|
||||
pub use screaming_snake_case::SubCommand as StrScreamingSnakeCase;
|
||||
pub use snake_case::SubCommand as StrSnakeCase;
|
||||
|
||||
use nu_engine::CallExt;
|
||||
|
||||
|
95
crates/nu-command/src/strings/str_/collect.rs
Normal file
95
crates/nu-command/src/strings/str_/collect.rs
Normal file
@ -0,0 +1,95 @@
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct StrCollect;
|
||||
|
||||
impl Command for StrCollect {
|
||||
fn name(&self) -> &str {
|
||||
"str collect"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("str collect").optional(
|
||||
"separator",
|
||||
SyntaxShape::String,
|
||||
"optional separator to use when creating string",
|
||||
)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"creates a string from the input, optionally using a separator"
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let separator: Option<String> = call.opt(engine_state, stack, 0)?;
|
||||
|
||||
#[allow(clippy::needless_collect)]
|
||||
let strings: Vec<Result<String, ShellError>> =
|
||||
input.into_iter().map(|value| value.as_string()).collect();
|
||||
let strings: Result<Vec<_>, _> = strings.into_iter().collect::<Result<_, _>>();
|
||||
|
||||
match strings {
|
||||
Ok(strings) => {
|
||||
let output = if let Some(separator) = separator {
|
||||
strings.join(&separator)
|
||||
} else {
|
||||
strings.join("")
|
||||
};
|
||||
|
||||
Ok(Value::String {
|
||||
val: output,
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
}
|
||||
_ => Err(ShellError::CantConvert(
|
||||
"string".into(),
|
||||
"non-string input".into(),
|
||||
call.head,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Create a string from input",
|
||||
example: "['nu', 'shell'] | str collect",
|
||||
result: Some(Value::String {
|
||||
val: "nushell".to_string(),
|
||||
span: Span::unknown(),
|
||||
}),
|
||||
},
|
||||
Example {
|
||||
description: "Create a string from input with a separator",
|
||||
example: "['nu', 'shell'] | str collect '-'",
|
||||
result: Some(Value::String {
|
||||
val: "nu-shell".to_string(),
|
||||
span: Span::unknown(),
|
||||
}),
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
test_examples(StrCollect {})
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
mod case;
|
||||
mod collect;
|
||||
|
||||
pub use case::*;
|
||||
pub use collect::*;
|
||||
|
Reference in New Issue
Block a user