use heck for string casing (#4081)

I removed the Inflector dependency in favor of heck for two reasons:
- to close #3674.
- heck seems simpler and actively maintained

We could probably alter the structure of the `str_` module to expose the
individual casing behaviors better.
I did not feel as confident on changing those signatures.

So I took a lazier approach of a macro in the `mod.rs` that creates the public
shimming function to heck's traits.
This commit is contained in:
Eli Flanagan 2021-12-14 10:43:48 -05:00 committed by GitHub
parent a3c349746f
commit e919f9a73b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 44 additions and 35 deletions

20
Cargo.lock generated
View file

@ -2,16 +2,6 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "Inflector"
version = "0.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3"
dependencies = [
"lazy_static",
"regex",
]
[[package]] [[package]]
name = "addr2line" name = "addr2line"
version = "0.17.0" version = "0.17.0"
@ -1548,6 +1538,12 @@ dependencies = [
"unicode-segmentation", "unicode-segmentation",
] ]
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.19" version = "0.1.19"
@ -2362,7 +2358,6 @@ dependencies = [
name = "nu-command" name = "nu-command"
version = "0.41.0" version = "0.41.0"
dependencies = [ dependencies = [
"Inflector",
"base64", "base64",
"bigdecimal", "bigdecimal",
"calamine", "calamine",
@ -2381,6 +2376,7 @@ dependencies = [
"futures 0.3.18", "futures 0.3.18",
"glob", "glob",
"hamcrest2", "hamcrest2",
"heck 0.4.0",
"htmlescape", "htmlescape",
"ical", "ical",
"indexmap", "indexmap",
@ -4474,7 +4470,7 @@ version = "0.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149" checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149"
dependencies = [ dependencies = [
"heck", "heck 0.3.3",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",

View file

@ -30,7 +30,7 @@ nu-pretty-hex = { version = "0.41.0", path="../nu-pretty-hex" }
url = "2.2.1" url = "2.2.1"
mime = "0.3.16" mime = "0.3.16"
Inflector = "0.11" heck = "0.4.0"
base64 = "0.13.0" base64 = "0.13.0"
bigdecimal = { version = "0.3.0", features = ["serde"] } bigdecimal = { version = "0.3.0", features = ["serde"] }
calamine = "0.18.0" calamine = "0.18.0"

View file

@ -1,6 +1,5 @@
use super::operate; use super::{operate, to_lower_camel_case};
use crate::prelude::*; use crate::prelude::*;
use inflector::cases::camelcase::to_camel_case;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, Value}; use nu_protocol::{Signature, SyntaxShape, Value};
@ -25,7 +24,7 @@ impl WholeStreamCommand for SubCommand {
} }
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
operate(args, &to_camel_case) operate(args, &to_lower_camel_case)
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
@ -40,7 +39,7 @@ impl WholeStreamCommand for SubCommand {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::ShellError; use super::ShellError;
use super::{to_camel_case, SubCommand}; use super::{to_lower_camel_case, SubCommand};
use crate::commands::strings::str_::case::action; use crate::commands::strings::str_::case::action;
use nu_source::Tag; use nu_source::Tag;
use nu_test_support::value::string; use nu_test_support::value::string;
@ -57,7 +56,7 @@ mod tests {
let word = string("this-is-the-first-case"); let word = string("this-is-the-first-case");
let expected = string("thisIsTheFirstCase"); let expected = string("thisIsTheFirstCase");
let actual = action(&word, Tag::unknown(), &to_camel_case).unwrap(); let actual = action(&word, Tag::unknown(), &to_lower_camel_case).unwrap();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[test] #[test]
@ -65,7 +64,7 @@ mod tests {
let word = string("this_is_the_second_case"); let word = string("this_is_the_second_case");
let expected = string("thisIsTheSecondCase"); let expected = string("thisIsTheSecondCase");
let actual = action(&word, Tag::unknown(), &to_camel_case).unwrap(); let actual = action(&word, Tag::unknown(), &to_lower_camel_case).unwrap();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
} }

View file

@ -1,6 +1,5 @@
use super::operate; use super::{operate, to_kebab_case};
use crate::prelude::*; use crate::prelude::*;
use inflector::cases::kebabcase::to_kebab_case;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, Value}; use nu_protocol::{Signature, SyntaxShape, Value};

View file

@ -16,6 +16,24 @@ pub use pascal_case::SubCommand as PascalCase;
pub use screaming_snake_case::SubCommand as ScreamingSnakeCase; pub use screaming_snake_case::SubCommand as ScreamingSnakeCase;
pub use snake_case::SubCommand as SnakeCase; pub use snake_case::SubCommand as SnakeCase;
use heck::ToKebabCase;
use heck::ToLowerCamelCase;
use heck::ToShoutySnakeCase;
use heck::ToSnakeCase;
use heck::ToUpperCamelCase;
macro_rules! create_heck_function {
($func_name:ident) => {
pub fn $func_name(a_slice: &str) -> String {
a_slice.$func_name()
}
};
}
create_heck_function!(to_upper_camel_case);
create_heck_function!(to_lower_camel_case);
create_heck_function!(to_kebab_case);
create_heck_function!(to_shouty_snake_case);
create_heck_function!(to_snake_case);
struct Arguments { struct Arguments {
column_paths: Vec<ColumnPath>, column_paths: Vec<ColumnPath>,
} }

View file

@ -1,6 +1,5 @@
use super::operate; use super::{operate, to_upper_camel_case};
use crate::prelude::*; use crate::prelude::*;
use inflector::cases::pascalcase::to_pascal_case;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, Value}; use nu_protocol::{Signature, SyntaxShape, Value};
@ -25,7 +24,7 @@ impl WholeStreamCommand for SubCommand {
} }
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
operate(args, &to_pascal_case) operate(args, &to_upper_camel_case)
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
@ -40,7 +39,7 @@ impl WholeStreamCommand for SubCommand {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::ShellError; use super::ShellError;
use super::{to_pascal_case, SubCommand}; use super::{to_upper_camel_case, SubCommand};
use crate::commands::strings::str_::case::action; use crate::commands::strings::str_::case::action;
use nu_source::Tag; use nu_source::Tag;
use nu_test_support::value::string; use nu_test_support::value::string;
@ -57,7 +56,7 @@ mod tests {
let word = string("this-is-the-first-case"); let word = string("this-is-the-first-case");
let expected = string("ThisIsTheFirstCase"); let expected = string("ThisIsTheFirstCase");
let actual = action(&word, Tag::unknown(), &to_pascal_case).unwrap(); let actual = action(&word, Tag::unknown(), &to_upper_camel_case).unwrap();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[test] #[test]
@ -65,7 +64,7 @@ mod tests {
let word = string("this_is_the_second_case"); let word = string("this_is_the_second_case");
let expected = string("ThisIsTheSecondCase"); let expected = string("ThisIsTheSecondCase");
let actual = action(&word, Tag::unknown(), &to_pascal_case).unwrap(); let actual = action(&word, Tag::unknown(), &to_upper_camel_case).unwrap();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
} }

View file

@ -1,6 +1,5 @@
use super::operate; use super::{operate, to_shouty_snake_case};
use crate::prelude::*; use crate::prelude::*;
use inflector::cases::screamingsnakecase::to_screaming_snake_case;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, Value}; use nu_protocol::{Signature, SyntaxShape, Value};
@ -25,7 +24,7 @@ impl WholeStreamCommand for SubCommand {
} }
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
operate(args, &to_screaming_snake_case) operate(args, &to_shouty_snake_case)
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
@ -40,7 +39,7 @@ impl WholeStreamCommand for SubCommand {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::ShellError; use super::ShellError;
use super::{to_screaming_snake_case, SubCommand}; use super::{to_shouty_snake_case, SubCommand};
use crate::commands::strings::str_::case::action; use crate::commands::strings::str_::case::action;
use nu_source::Tag; use nu_source::Tag;
use nu_test_support::value::string; use nu_test_support::value::string;
@ -57,7 +56,7 @@ mod tests {
let word = string("this-is-the-first-case"); let word = string("this-is-the-first-case");
let expected = string("THIS_IS_THE_FIRST_CASE"); let expected = string("THIS_IS_THE_FIRST_CASE");
let actual = action(&word, Tag::unknown(), &to_screaming_snake_case).unwrap(); let actual = action(&word, Tag::unknown(), &to_shouty_snake_case).unwrap();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[test] #[test]
@ -65,7 +64,7 @@ mod tests {
let word = string("this_is_the_second_case"); let word = string("this_is_the_second_case");
let expected = string("THIS_IS_THE_SECOND_CASE"); let expected = string("THIS_IS_THE_SECOND_CASE");
let actual = action(&word, Tag::unknown(), &to_screaming_snake_case).unwrap(); let actual = action(&word, Tag::unknown(), &to_shouty_snake_case).unwrap();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
} }

View file

@ -1,6 +1,5 @@
use super::operate; use super::{operate, to_snake_case};
use crate::prelude::*; use crate::prelude::*;
use inflector::cases::snakecase::to_snake_case;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, Value}; use nu_protocol::{Signature, SyntaxShape, Value};