From c56fff88a21dc347dc635d2d7e540603f765d153 Mon Sep 17 00:00:00 2001 From: jean-airoldie Date: Wed, 26 Sep 2018 16:35:02 -0400 Subject: [PATCH] rust: Fixed MakeCamelCase (#4932) (#4936) * Fixed MakeCamelCase behavior when supplied Upper_Camel_Case, snake_case and UPPERCASE strings. * Modified the rust integration test to reflect changes. --- src/idl_gen_rust.cpp | 14 ++++++++++---- tests/monster_test_generated.rs | 2 +- tests/namespace_test/namespace_test1_generated.rs | 12 ++++++------ tests/rust_usage_test/tests/integration_test.rs | 2 +- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 38773e4e9..393006591 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -33,13 +33,19 @@ static std::string GeneratedFileName(const std::string &path, std::string MakeSnakeCase(const std::string &in) { std::string s; for (size_t i = 0; i < in.length(); i++) { - if (islower(in[i])) { - s += static_cast(in[i]); - } else { - if (i > 0) { + if (i == 0) { + s += static_cast(tolower(in[0])); + } else if (in[i] == '_') { + s += '_'; + } else if (!islower(in[i])) { + // Prevent duplicate underscores for Upper_Snake_Case strings + // and UPPERCASE strings. + if (islower(in[i - 1])) { s += '_'; } s += static_cast(tolower(in[i])); + } else { + s += in[i]; } } return s; diff --git a/tests/monster_test_generated.rs b/tests/monster_test_generated.rs index 1b3f1ab6f..d2316d745 100644 --- a/tests/monster_test_generated.rs +++ b/tests/monster_test_generated.rs @@ -1110,7 +1110,7 @@ impl<'a> Monster<'a> { #[inline] #[allow(non_snake_case)] - pub fn test_as_my_game___example_2___monster(&'a self) -> Option { + pub fn test_as_my_game_example_2_monster(&'a self) -> Option { if self.test_type() == Any::MyGame_Example2_Monster { self.test().map(|u| super::example_2::Monster::init_from_table(u)) } else { diff --git a/tests/namespace_test/namespace_test1_generated.rs b/tests/namespace_test/namespace_test1_generated.rs index b4720444c..d12189170 100644 --- a/tests/namespace_test/namespace_test1_generated.rs +++ b/tests/namespace_test/namespace_test1_generated.rs @@ -34,8 +34,8 @@ pub enum EnumInNestedNS { } -const ENUM_MIN_ENUM_IN_NESTED_N_S: i8 = 0; -const ENUM_MAX_ENUM_IN_NESTED_N_S: i8 = 2; +const ENUM_MIN_ENUM_IN_NESTED_NS: i8 = 0; +const ENUM_MAX_ENUM_IN_NESTED_NS: i8 = 2; impl<'a> flatbuffers::Follow<'a> for EnumInNestedNS { type Inner = Self; @@ -69,22 +69,22 @@ impl flatbuffers::Push for EnumInNestedNS { } #[allow(non_camel_case_types)] -const ENUM_VALUES_ENUM_IN_NESTED_N_S:[EnumInNestedNS; 3] = [ +const ENUM_VALUES_ENUM_IN_NESTED_NS:[EnumInNestedNS; 3] = [ EnumInNestedNS::A, EnumInNestedNS::B, EnumInNestedNS::C ]; #[allow(non_camel_case_types)] -const ENUM_NAMES_ENUM_IN_NESTED_N_S:[&'static str; 3] = [ +const ENUM_NAMES_ENUM_IN_NESTED_NS:[&'static str; 3] = [ "A", "B", "C" ]; -pub fn enum_name_enum_in_nested_n_s(e: EnumInNestedNS) -> &'static str { +pub fn enum_name_enum_in_nested_ns(e: EnumInNestedNS) -> &'static str { let index: usize = e as usize; - ENUM_NAMES_ENUM_IN_NESTED_N_S[index] + ENUM_NAMES_ENUM_IN_NESTED_NS[index] } // struct StructInNestedNS, aligned to 4 diff --git a/tests/rust_usage_test/tests/integration_test.rs b/tests/rust_usage_test/tests/integration_test.rs index 54c63592c..4f3eca435 100644 --- a/tests/rust_usage_test/tests/integration_test.rs +++ b/tests/rust_usage_test/tests/integration_test.rs @@ -331,7 +331,7 @@ mod roundtrip_generated_code { Some("foo")); assert_eq!(mon.test_as_monster().unwrap().name(), Some("foo")); assert_eq!(mon.test_as_test_simple_table_with_enum(), None); - assert_eq!(mon.test_as_my_game___example_2___monster(), None); + assert_eq!(mon.test_as_my_game_example_2_monster(), None); } #[test] fn union_default() {