From e42228e2d507234438d9d2a1ea3c2981c4808847 Mon Sep 17 00:00:00 2001 From: theduke Date: Sat, 24 Jun 2017 20:19:43 +0200 Subject: [PATCH] Duplicating to_camel_case in codegen --- juniper_codegen/src/util.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/juniper_codegen/src/util.rs b/juniper_codegen/src/util.rs index e38169da..78125122 100644 --- a/juniper_codegen/src/util.rs +++ b/juniper_codegen/src/util.rs @@ -46,3 +46,40 @@ pub fn keyed_item_value(item: &NestedMetaItem, name: &str, must_be_string: bool) }, } } + +// Note: duplicated from juniper crate! +#[doc(hidden)] +pub fn to_camel_case(s: &str) -> String { + let mut dest = String::new(); + + for (i, part) in s.split('_').enumerate() { + if i > 0 && part.len() == 1 { + dest.push_str(&part.to_uppercase()); + } + else if i > 0 && part.len() > 1 { + let first = part.chars().next().unwrap().to_uppercase().collect::(); + let second = &part[1..]; + + dest.push_str(&first); + dest.push_str(second); + } + else if i == 0 { + dest.push_str(part); + } + } + + dest +} + +#[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("first_second")[..], "firstSecond"); + assert_eq!(&to_camel_case("first_")[..], "first"); + assert_eq!(&to_camel_case("a_b_c")[..], "aBC"); + assert_eq!(&to_camel_case("a_bc")[..], "aBc"); + assert_eq!(&to_camel_case("a_b")[..], "aB"); + assert_eq!(&to_camel_case("a")[..], "a"); + assert_eq!(&to_camel_case("")[..], ""); +}