Update to_camel_case to respect the rust convention of prefixing '_' for unused vars (#684)

This commit is contained in:
Alexander Lyon 2020-06-28 07:15:19 +01:00 committed by GitHub
parent 37a37d462f
commit 714d602fb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View file

@ -7,7 +7,13 @@ use std::borrow::Cow;
pub fn to_camel_case(s: &'_ str) -> Cow<'_, str> {
let mut dest = Cow::Borrowed(s);
for (i, part) in s.split('_').enumerate() {
// handle '_' to be more friendly with the
// _var convention for unused variables
let s_iter = if s.starts_with('_') { &s[1..] } else { s }
.split('_')
.enumerate();
for (i, part) in s_iter {
if i > 0 && part.len() == 1 {
dest += Cow::Owned(part.to_uppercase());
} else if i > 0 && part.len() > 1 {
@ -32,7 +38,7 @@ pub fn to_camel_case(s: &'_ str) -> Cow<'_, str> {
#[test]
fn test_to_camel_case() {
assert_eq!(&to_camel_case("test")[..], "test");
assert_eq!(&to_camel_case("_test")[..], "Test");
assert_eq!(&to_camel_case("_test")[..], "test");
assert_eq!(&to_camel_case("first_second")[..], "firstSecond");
assert_eq!(&to_camel_case("first_")[..], "first");
assert_eq!(&to_camel_case("a_b_c")[..], "aBC");

View file

@ -233,7 +233,13 @@ fn get_doc_attr(attrs: &[Attribute]) -> Option<Vec<MetaNameValue>> {
pub fn to_camel_case(s: &str) -> String {
let mut dest = String::new();
for (i, part) in s.split('_').enumerate() {
// handle '_' to be more friendly with the
// _var convention for unused variables
let s_iter = if s.starts_with('_') { &s[1..] } else { s }
.split('_')
.enumerate();
for (i, part) in s_iter {
if i > 0 && part.len() == 1 {
dest.push_str(&part.to_uppercase());
} else if i > 0 && part.len() > 1 {
@ -1874,7 +1880,7 @@ mod test {
#[test]
fn test_to_camel_case() {
assert_eq!(&to_camel_case("test")[..], "test");
assert_eq!(&to_camel_case("_test")[..], "Test");
assert_eq!(&to_camel_case("_test")[..], "test");
assert_eq!(&to_camel_case("first_second")[..], "firstSecond");
assert_eq!(&to_camel_case("first_")[..], "first");
assert_eq!(&to_camel_case("a_b_c")[..], "aBC");