Cratification: move some str case commands to nu-cmd-extra (#9926)

I am moving the following str case commands to nu-cmd-extra (as
discussed in the core team meeting the other day)

* camel-case
* kebab-case
* pascal-case
* screaming-snake-case
* snake-case
* title-case
This commit is contained in:
Michael Angerman
2023-08-06 06:40:44 -07:00
committed by GitHub
parent 066790552a
commit 58f98a4260
17 changed files with 145 additions and 27 deletions

View File

@ -88,7 +88,14 @@ pub fn add_extra_command_context(mut engine_state: EngineState) -> EngineState {
strings::format::FileSize,
strings::format::FormatDuration,
strings::encode_decode::EncodeHex,
strings::encode_decode::DecodeHex
strings::encode_decode::DecodeHex,
strings::str_::case::Str,
strings::str_::case::StrCamelCase,
strings::str_::case::StrKebabCase,
strings::str_::case::StrPascalCase,
strings::str_::case::StrScreamingSnakeCase,
strings::str_::case::StrSnakeCase,
strings::str_::case::StrTitleCase
);
bind_command!(formats::ToHtml, formats::FromUrl);

View File

@ -1,2 +1,3 @@
pub(crate) mod encode_decode;
pub(crate) mod format;
pub(crate) mod str_;

View File

@ -0,0 +1,99 @@
use inflector::cases::camelcase::to_camel_case;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use super::operate;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str camel-case"
}
fn signature(&self) -> Signature {
Signature::build("str camel-case")
.input_output_types(vec![
(Type::String, Type::String),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
])
.allow_variants_without_examples(true)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, convert strings at the given cell paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Convert a string to camelCase."
}
fn search_terms(&self) -> Vec<&str> {
vec!["convert", "style", "caps", "convention"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input, &to_camel_case)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to camelCase",
example: " 'NuShell' | str camel-case",
result: Some(Value::test_string("nuShell")),
},
Example {
description: "convert a string to camelCase",
example: "'this-is-the-first-case' | str camel-case",
result: Some(Value::test_string("thisIsTheFirstCase")),
},
Example {
description: "convert a string to camelCase",
example: " 'this_is_the_second_case' | str camel-case",
result: Some(Value::test_string("thisIsTheSecondCase")),
},
Example {
description: "convert a column from a table to camelCase",
example: r#"[[lang, gems]; [nu_test, 100]] | str camel-case lang"#,
result: Some(Value::List {
vals: vec![Value::Record {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![Value::test_string("nuTest"), Value::test_int(100)],
}],
span: Span::test_data(),
}),
},
]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -0,0 +1,98 @@
use inflector::cases::kebabcase::to_kebab_case;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use super::operate;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str kebab-case"
}
fn signature(&self) -> Signature {
Signature::build("str kebab-case")
.input_output_types(vec![
(Type::String, Type::String),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
])
.allow_variants_without_examples(true)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, convert strings at the given cell paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Convert a string to kebab-case."
}
fn search_terms(&self) -> Vec<&str> {
vec!["convert", "style", "hyphens", "convention"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input, &to_kebab_case)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to kebab-case",
example: "'NuShell' | str kebab-case",
result: Some(Value::test_string("nu-shell")),
},
Example {
description: "convert a string to kebab-case",
example: "'thisIsTheFirstCase' | str kebab-case",
result: Some(Value::test_string("this-is-the-first-case")),
},
Example {
description: "convert a string to kebab-case",
example: "'THIS_IS_THE_SECOND_CASE' | str kebab-case",
result: Some(Value::test_string("this-is-the-second-case")),
},
Example {
description: "convert a column from a table to kebab-case",
example: r#"[[lang, gems]; [nuTest, 100]] | str kebab-case lang"#,
result: Some(Value::List {
vals: vec![Value::Record {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![Value::test_string("nu-test"), Value::test_int(100)],
}],
span: Span::test_data(),
}),
},
]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -0,0 +1,74 @@
mod camel_case;
mod kebab_case;
mod pascal_case;
mod screaming_snake_case;
mod snake_case;
mod str_;
mod title_case;
pub use camel_case::SubCommand as StrCamelCase;
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;
pub use str_::Str;
pub use title_case::SubCommand as StrTitleCase;
use nu_engine::CallExt;
use nu_cmd_base::input_handler::{operate as general_operate, CmdArgument};
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{PipelineData, ShellError, Span, Value};
struct Arguments<F: Fn(&str) -> String + Send + Sync + 'static> {
case_operation: &'static F,
cell_paths: Option<Vec<CellPath>>,
}
impl<F: Fn(&str) -> String + Send + Sync + 'static> CmdArgument for Arguments<F> {
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
self.cell_paths.take()
}
}
pub fn operate<F>(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
case_operation: &'static F,
) -> Result<PipelineData, ShellError>
where
F: Fn(&str) -> String + Send + Sync + 'static,
{
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
let args = Arguments {
case_operation,
cell_paths,
};
general_operate(action, args, input, call.head, engine_state.ctrlc.clone())
}
fn action<F>(input: &Value, args: &Arguments<F>, head: Span) -> Value
where
F: Fn(&str) -> String + Send + Sync + 'static,
{
let case_operation = args.case_operation;
match input {
Value::String { val, .. } => Value::String {
val: case_operation(val),
span: head,
},
Value::Error { .. } => input.clone(),
_ => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {
exp_input_type: "string".into(),
wrong_type: input.get_type().to_string(),
dst_span: head,
src_span: input.expect_span(),
}),
},
}
}

View File

@ -0,0 +1,99 @@
use inflector::cases::pascalcase::to_pascal_case;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use super::operate;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str pascal-case"
}
fn signature(&self) -> Signature {
Signature::build("str pascal-case")
.input_output_types(vec![
(Type::String, Type::String),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
])
.allow_variants_without_examples(true)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, convert strings at the given cell paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Convert a string to PascalCase."
}
fn search_terms(&self) -> Vec<&str> {
vec!["convert", "style", "caps", "upper", "convention"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input, &to_pascal_case)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to PascalCase",
example: "'nu-shell' | str pascal-case",
result: Some(Value::test_string("NuShell")),
},
Example {
description: "convert a string to PascalCase",
example: "'this-is-the-first-case' | str pascal-case",
result: Some(Value::test_string("ThisIsTheFirstCase")),
},
Example {
description: "convert a string to PascalCase",
example: "'this_is_the_second_case' | str pascal-case",
result: Some(Value::test_string("ThisIsTheSecondCase")),
},
Example {
description: "convert a column from a table to PascalCase",
example: r#"[[lang, gems]; [nu_test, 100]] | str pascal-case lang"#,
result: Some(Value::List {
vals: vec![Value::Record {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![Value::test_string("NuTest"), Value::test_int(100)],
}],
span: Span::test_data(),
}),
},
]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -0,0 +1,99 @@
use inflector::cases::screamingsnakecase::to_screaming_snake_case;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use super::operate;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str screaming-snake-case"
}
fn signature(&self) -> Signature {
Signature::build("str screaming-snake-case")
.input_output_types(vec![
(Type::String, Type::String),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
])
.allow_variants_without_examples(true)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, convert strings at the given cell paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Convert a string to SCREAMING_SNAKE_CASE."
}
fn search_terms(&self) -> Vec<&str> {
vec!["convert", "style", "underscore", "convention"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input, &to_screaming_snake_case)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to SCREAMING_SNAKE_CASE",
example: r#" "NuShell" | str screaming-snake-case"#,
result: Some(Value::test_string("NU_SHELL")),
},
Example {
description: "convert a string to SCREAMING_SNAKE_CASE",
example: r#" "this_is_the_second_case" | str screaming-snake-case"#,
result: Some(Value::test_string("THIS_IS_THE_SECOND_CASE")),
},
Example {
description: "convert a string to SCREAMING_SNAKE_CASE",
example: r#""this-is-the-first-case" | str screaming-snake-case"#,
result: Some(Value::test_string("THIS_IS_THE_FIRST_CASE")),
},
Example {
description: "convert a column from a table to SCREAMING_SNAKE_CASE",
example: r#"[[lang, gems]; [nu_test, 100]] | str screaming-snake-case lang"#,
result: Some(Value::List {
vals: vec![Value::Record {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![Value::test_string("NU_TEST"), Value::test_int(100)],
}],
span: Span::test_data(),
}),
},
]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -0,0 +1,98 @@
use inflector::cases::snakecase::to_snake_case;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use super::operate;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str snake-case"
}
fn signature(&self) -> Signature {
Signature::build("str snake-case")
.input_output_types(vec![
(Type::String, Type::String),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
])
.allow_variants_without_examples(true)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, convert strings at the given cell paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Convert a string to snake_case."
}
fn search_terms(&self) -> Vec<&str> {
vec!["convert", "style", "underscore", "lower", "convention"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input, &to_snake_case)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to snake_case",
example: r#" "NuShell" | str snake-case"#,
result: Some(Value::test_string("nu_shell")),
},
Example {
description: "convert a string to snake_case",
example: r#" "this_is_the_second_case" | str snake-case"#,
result: Some(Value::test_string("this_is_the_second_case")),
},
Example {
description: "convert a string to snake_case",
example: r#""this-is-the-first-case" | str snake-case"#,
result: Some(Value::test_string("this_is_the_first_case")),
},
Example {
description: "convert a column from a table to snake_case",
example: r#"[[lang, gems]; [nuTest, 100]] | str snake-case lang"#,
result: Some(Value::List {
vals: vec![Value::Record {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![Value::test_string("nu_test"), Value::test_int(100)],
}],
span: Span::test_data(),
}),
},
]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -0,0 +1,49 @@
use nu_engine::get_full_help;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
#[derive(Clone)]
pub struct Str;
impl Command for Str {
fn name(&self) -> &str {
"str"
}
fn signature(&self) -> Signature {
Signature::build("str")
.category(Category::Strings)
.input_output_types(vec![(Type::Nothing, Type::String)])
}
fn usage(&self) -> &str {
"Various commands for working with string data."
}
fn extra_usage(&self) -> &str {
"You must use one of the following subcommands. Using this command as-is will only produce this help message."
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
Ok(Value::String {
val: get_full_help(
&Str.signature(),
&Str.examples(),
engine_state,
stack,
self.is_parser_keyword(),
),
span: call.head,
}
.into_pipeline_data())
}
}

View File

@ -0,0 +1,94 @@
use inflector::cases::titlecase::to_title_case;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use super::operate;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str title-case"
}
fn signature(&self) -> Signature {
Signature::build("str title-case")
.input_output_types(vec![
(Type::String, Type::String),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
])
.allow_variants_without_examples(true)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, convert strings at the given cell paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Convert a string to Title Case."
}
fn search_terms(&self) -> Vec<&str> {
vec!["convert", "style", "convention"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input, &to_title_case)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to Title Case",
example: "'nu-shell' | str title-case",
result: Some(Value::test_string("Nu Shell")),
},
Example {
description: "convert a string to Title Case",
example: "'this is a test case' | str title-case",
result: Some(Value::test_string("This Is A Test Case")),
},
Example {
description: "convert a column from a table to Title Case",
example: r#"[[title, count]; ['nu test', 100]] | str title-case title"#,
result: Some(Value::List {
vals: vec![Value::Record {
span: Span::test_data(),
cols: vec!["title".to_string(), "count".to_string()],
vals: vec![Value::test_string("Nu Test"), Value::test_int(100)],
}],
span: Span::test_data(),
}),
},
]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -0,0 +1,3 @@
pub(crate) mod case;
pub use case::*;