From 02213ceefa70bb84ae5f8c00321032ab777e1300 Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Thu, 11 Apr 2024 15:04:26 +0300 Subject: [PATCH 1/5] Add ability to choose between `Capitalized` and `Google` style enum declarations, default is `Google` This was causing problems on Windows platforms if the enum names were one of the defined names like `ERROR`, `DEBUG`, `TRUE`, `FALSE` etc Signed-off-by: Martynas Gurskas --- bindgen/src/bindings/cpp/gen_cpp/enum_.rs | 12 ++------ .../src/bindings/cpp/gen_cpp/filters/mod.rs | 28 +++++++++++++++---- bindgen/src/bindings/cpp/gen_cpp/mod.rs | 16 +++++++++++ bindgen/src/bindings/cpp/templates/enum.hpp | 14 +++++----- .../src/bindings/cpp/templates/enum_tmpl.cpp | 10 +++---- bindgen/src/bindings/cpp/templates/err.hpp | 4 +-- bindgen/src/bindings/cpp/templates/rec.hpp | 4 +-- 7 files changed, 56 insertions(+), 32 deletions(-) diff --git a/bindgen/src/bindings/cpp/gen_cpp/enum_.rs b/bindgen/src/bindings/cpp/gen_cpp/enum_.rs index 72cd408..adc419a 100644 --- a/bindgen/src/bindings/cpp/gen_cpp/enum_.rs +++ b/bindgen/src/bindings/cpp/gen_cpp/enum_.rs @@ -22,15 +22,7 @@ impl CodeType for EnumCodeType { format!("Type{}", self.id) } - fn literal(&self, literal: &Literal) -> String { - if let Literal::Enum(v, _) = literal { - format!( - "{}::{}", - self.type_label(), - CppCodeOracle.enum_variant_name(v) - ) - } else { - unreachable!(); - } + fn literal(&self, _: &Literal) -> String { + unreachable!(); } } diff --git a/bindgen/src/bindings/cpp/gen_cpp/filters/mod.rs b/bindgen/src/bindings/cpp/gen_cpp/filters/mod.rs index e2449f1..fa64ba1 100644 --- a/bindgen/src/bindings/cpp/gen_cpp/filters/mod.rs +++ b/bindgen/src/bindings/cpp/gen_cpp/filters/mod.rs @@ -11,6 +11,8 @@ use crate::bindings::cpp::gen_cpp::{ callback_interface, compounds, custom, enum_, miscellany, object, primitives, record, }; +use super::EnumStyle; + type Result = std::result::Result; #[derive(Clone)] @@ -25,8 +27,11 @@ impl CppCodeOracle { nm.to_string().to_upper_camel_case() } - pub(crate) fn enum_variant_name(&self, nm: &str) -> String { - nm.to_string().to_shouty_snake_case() + pub(crate) fn enum_variant_name(&self, nm: &str, style: &EnumStyle) -> String { + match style { + EnumStyle::Capitalized => nm.to_string().to_shouty_snake_case(), + EnumStyle::Google => format!("k{}", nm.to_string().to_upper_camel_case()), + } } pub(crate) fn fn_name(&self, nm: &str) -> String { @@ -107,8 +112,19 @@ pub(crate) fn var_name(nm: &str) -> Result { Ok(CppCodeOracle.var_name(nm)) } -pub(crate) fn literal_cpp(literal: &Literal, as_ct: &impl AsCodeType) -> Result { - Ok(as_ct.as_codetype().literal(literal)) +pub(crate) fn literal_cpp( + literal: &Literal, + as_ct: &impl AsCodeType, + enum_style: &EnumStyle, +) -> Result { + match literal { + Literal::Enum(name, _) => Ok(format!( + "{}::{}", + as_ct.as_codetype().type_label(), + CppCodeOracle.enum_variant_name(&name, enum_style), + )), + _ => Ok(as_ct.as_codetype().literal(literal)), + } } pub(crate) fn lift_fn(as_ct: &impl AsCodeType) -> Result { @@ -146,8 +162,8 @@ pub(crate) fn allocation_size_fn(as_ct: &impl AsCodeType) -> Result { )) } -pub(crate) fn variant_name(v: &Variant) -> Result { - Ok(CppCodeOracle.enum_variant_name(v.name())) +pub(crate) fn variant_name(v: &Variant, enum_style: &EnumStyle) -> Result { + Ok(CppCodeOracle.enum_variant_name(v.name(), enum_style)) } pub(crate) fn ffi_type_name(ffi_type: &FfiType) -> Result { diff --git a/bindgen/src/bindings/cpp/gen_cpp/mod.rs b/bindgen/src/bindings/cpp/gen_cpp/mod.rs index 6098857..29f01c9 100644 --- a/bindgen/src/bindings/cpp/gen_cpp/mod.rs +++ b/bindgen/src/bindings/cpp/gen_cpp/mod.rs @@ -24,6 +24,18 @@ use uniffi_bindgen::{ BindingsConfig, ComponentInterface, }; +#[derive(Serialize, Deserialize, Clone, Debug)] +enum EnumStyle { + Capitalized, + Google, +} + +impl Default for EnumStyle { + fn default() -> Self { + EnumStyle::Google + } +} + #[derive(Clone, Deserialize, Serialize, Debug, Default)] struct CustomTypesConfig { imports: Option>, @@ -36,12 +48,16 @@ struct CustomTypesConfig { pub(crate) struct Config { #[serde(default)] custom_types: HashMap, + #[serde(default)] + enum_style: EnumStyle, } #[derive(Clone, Deserialize, Serialize, Debug, Default)] pub(crate) struct ScaffoldingConfig { #[serde(default)] namespace: Option, + #[serde(default)] + enum_style: EnumStyle, } impl BindingsConfig for Config { diff --git a/bindgen/src/bindings/cpp/templates/enum.hpp b/bindgen/src/bindings/cpp/templates/enum.hpp index 0a355cc..d45f535 100644 --- a/bindgen/src/bindings/cpp/templates/enum.hpp +++ b/bindgen/src/bindings/cpp/templates/enum.hpp @@ -4,7 +4,7 @@ enum class {{ type_name }}: int32_t { {%- for variant in e.variants() %} {%- call macros::docstring(variant, 4) %} - {{ variant|variant_name }} = {{ loop.index }} + {{ variant|variant_name(config.enum_style) }} = {{ loop.index }} {%- if !loop.last %}, {%- endif %} {%- endfor %} @@ -20,19 +20,19 @@ struct {{ type_name }} { {%- for variant in e.variants() %} {%- call macros::docstring(variant, 4) %} - struct {{ variant|variant_name }} { + struct {{ variant|variant_name(config.enum_style) }} { {%- for field in variant.fields() %} {%- call macros::docstring(field, 8) %} {{ field|type_name }} {{ field.name()|var_name }} {%- match field.default_value() %} - {%- when Some with (literal) %} = {{ literal|literal_cpp(field) }};{%- else -%}; + {%- when Some with (literal) %} = {{ literal|literal_cpp(field, config.enum_style) }};{%- else -%}; {%- endmatch %} {%- endfor %} }; {%- endfor %} {%- for variant in e.variants() %} - {{ type_name }}({{ variant|variant_name }} variant): variant(variant) {} + {{ type_name }}({{ variant|variant_name(config.enum_style) }} variant): variant(variant) {} {%- endfor %} {{ type_name }}(const {{ type_name }} &other): variant(other.variant) {} @@ -51,13 +51,13 @@ struct {{ type_name }} { /** * Returns the variant of this enum */ - const std::variant<{% for variant in e.variants() %}{{ variant|variant_name }}{% if !loop.last %}, {% endif %}{% endfor %}> &get_variant() const { + const std::variant<{% for variant in e.variants() %}{{ variant|variant_name(config.enum_style) }}{% if !loop.last %}, {% endif %}{% endfor %}> &get_variant() const { return variant; } private: - std::variant<{% for variant in e.variants() %}{{ variant|variant_name }}{% if !loop.last %}, {% endif %}{% endfor %}> variant; + std::variant<{% for variant in e.variants() %}{{ variant|variant_name(config.enum_style) }}{% if !loop.last %}, {% endif %}{% endfor %}> variant; {{ type_name }}(); }; -{%- endif %} \ No newline at end of file +{%- endif %} diff --git a/bindgen/src/bindings/cpp/templates/enum_tmpl.cpp b/bindgen/src/bindings/cpp/templates/enum_tmpl.cpp index f01153d..c8b51cf 100644 --- a/bindgen/src/bindings/cpp/templates/enum_tmpl.cpp +++ b/bindgen/src/bindings/cpp/templates/enum_tmpl.cpp @@ -24,7 +24,7 @@ RustBuffer {{ ffi_converter_name }}::lower(const {{ type_name }} &val) { switch (variant) { {% for variant in e.variants() %} case {{ loop.index }}: - return {{ type_name }}::{{ variant|variant_name }}; + return {{ type_name }}::{{ variant|variant_name(config.enum_style) }}; {% endfor %} default: throw std::runtime_error("No matching {{ type_name }} variant"); @@ -34,7 +34,7 @@ RustBuffer {{ ffi_converter_name }}::lower(const {{ type_name }} &val) { void {{ ffi_converter_name }}::write(RustStream &stream, const {{ type_name }} &val) { switch (val) { {% for variant in e.variants() %} - case {{ type_name }}::{{ variant|variant_name }}: + case {{ type_name }}::{{ variant|variant_name(config.enum_style) }}: stream << static_cast({{ loop.index }}); break; {% endfor %} @@ -72,7 +72,7 @@ RustBuffer {{ ffi_converter_name }}::lower(const {{ type_name }} &val) { switch (variant_id) { {% for variant in e.variants() %} case {{ loop.index }}: - return {{ type_name }}::{{ variant|variant_name }} { + return {{ type_name }}::{{ variant|variant_name(config.enum_style) }} { {%- for field in variant.fields() %} .{{field.name()|var_name}} = {{ field|read_fn }}(stream), {%- endfor %} @@ -91,7 +91,7 @@ void {{ ffi_converter_name }}::write(RustStream &stream, const {{ type_name }} & std::visit([&](auto &&arg) { using T = std::decay_t; {%- for variant in e.variants() %} - {% if !loop.first %}else {% endif %}if constexpr (std::is_same_v) { + {% if !loop.first %}else {% endif %}if constexpr (std::is_same_v) { {%- for field in variant.fields() %} {{ field|write_fn }}(stream, arg.{{ field.name()|var_name }}); {%- endfor %} @@ -111,7 +111,7 @@ int32_t {{ ffi_converter_name }}::allocation_size(const {{ type_name|class_name size += std::visit([&](auto &&arg) { using T = std::decay_t; {%- for variant in e.variants() %} - {% if !loop.first %}else {% endif %}if constexpr (std::is_same_v) { + {% if !loop.first %}else {% endif %}if constexpr (std::is_same_v) { int32_t size = 0; {%- for field in variant.fields() %} size += {{ field|allocation_size_fn }}(arg.{{ field.name()|var_name }}); diff --git a/bindgen/src/bindings/cpp/templates/err.hpp b/bindgen/src/bindings/cpp/templates/err.hpp index 2d35ec6..6a72d04 100644 --- a/bindgen/src/bindings/cpp/templates/err.hpp +++ b/bindgen/src/bindings/cpp/templates/err.hpp @@ -31,7 +31,7 @@ struct {{ variant.name()|class_name }}: {{ class_name }} { {%- for field in variant.fields() %} {{ field|type_name }} {{ field.name()|var_name }} {%- match field.default_value() %} - {% when Some with (literal) %} = {{ literal|literal_cpp(field) }};{% else %}; + {% when Some with (literal) %} = {{ literal|literal_cpp(field, config.enum_style) }};{% else %}; {%- endmatch %} {%- endfor %} @@ -48,4 +48,4 @@ struct {{ variant.name()|class_name }}: {{ class_name }} { }; {%- endfor %} } // namespace {{ class_name|to_lower_snake_case }} -{%- endif %} \ No newline at end of file +{%- endif %} diff --git a/bindgen/src/bindings/cpp/templates/rec.hpp b/bindgen/src/bindings/cpp/templates/rec.hpp index f65c719..061e0b4 100644 --- a/bindgen/src/bindings/cpp/templates/rec.hpp +++ b/bindgen/src/bindings/cpp/templates/rec.hpp @@ -6,7 +6,7 @@ struct {{ type_name }} { {%- call macros::docstring(field, 4) %} {{ field|type_name }} {{ field.name()|var_name }} {%- match field.default_value() %} - {%- when Some with (literal) %} = {{ literal|literal_cpp(field) }};{%- else -%}; + {%- when Some with (literal) %} = {{ literal|literal_cpp(field, config.enum_style) }};{%- else -%}; {%- endmatch %} {%- endfor %} -}; \ No newline at end of file +}; From 1bcbe76c2adbc6a194d99b91ed06decb7bc3de85 Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Thu, 11 Apr 2024 15:04:44 +0300 Subject: [PATCH 2/5] Update tests to match new enum style default Signed-off-by: Martynas Gurskas --- .../coverall/lib_coverall.cpp | 8 ++--- .../coverall/lib_coverall.hpp | 36 +++++++++---------- .../rondpoint/lib_rondpoint.hpp | 24 ++++++------- cpp-tests/tests/coverall/main.cpp | 4 +-- cpp-tests/tests/rondpoint/main.cpp | 20 +++++------ 5 files changed, 46 insertions(+), 46 deletions(-) diff --git a/cpp-tests/scaffolding_tests/coverall/lib_coverall.cpp b/cpp-tests/scaffolding_tests/coverall/lib_coverall.cpp index 11804d0..013b7bc 100644 --- a/cpp-tests/scaffolding_tests/coverall/lib_coverall.cpp +++ b/cpp-tests/scaffolding_tests/coverall/lib_coverall.cpp @@ -227,10 +227,10 @@ std::vector> coverall::get_traits() { coverall::MaybeSimpleDict coverall::get_maybe_simple_dict(int8_t index) { if (index == 0) { - return { coverall::MaybeSimpleDict::YEAH {} }; + return { coverall::MaybeSimpleDict::kYeah {} }; } else if (index == 1) { - return { coverall::MaybeSimpleDict::NAH {} }; + return { coverall::MaybeSimpleDict::kNah {} }; } throw std::runtime_error("invalid index"); @@ -238,10 +238,10 @@ coverall::MaybeSimpleDict coverall::get_maybe_simple_dict(int8_t index) { coverall::SimpleFlatMacroEnum coverall::get_simple_flat_macro_enum(int8_t index) { if (index == 0) { - return { coverall::SimpleFlatMacroEnum::FIRST { "the first" } }; + return { coverall::SimpleFlatMacroEnum::kFirst { "the first" } }; } else if (index == 1) { - return { coverall::SimpleFlatMacroEnum::SECOND { 2 } }; + return { coverall::SimpleFlatMacroEnum::kSecond { 2 } }; } throw std::runtime_error("invalid index"); diff --git a/cpp-tests/scaffolding_tests/coverall/lib_coverall.hpp b/cpp-tests/scaffolding_tests/coverall/lib_coverall.hpp index 042bddc..8939a3a 100644 --- a/cpp-tests/scaffolding_tests/coverall/lib_coverall.hpp +++ b/cpp-tests/scaffolding_tests/coverall/lib_coverall.hpp @@ -51,9 +51,9 @@ namespace { }; enum class Color { - RED, - BLUE, - GREEN + kRed, + kBlue, + kGreen }; class Patch { @@ -150,48 +150,48 @@ namespace { class MaybeSimpleDict { public: - struct YEAH { + struct kYeah { SimpleDict d; }; - struct NAH {}; + struct kNah {}; - MaybeSimpleDict(MaybeSimpleDict::YEAH value) { this->variant = value; } - MaybeSimpleDict(MaybeSimpleDict::NAH value) { this->variant = value; } + MaybeSimpleDict(MaybeSimpleDict::kYeah value) { this->variant = value; } + MaybeSimpleDict(MaybeSimpleDict::kNah value) { this->variant = value; } - std::variant variant; + std::variant variant; }; class SimpleFlatEnum { public: - struct FIRST { + struct kFirst { std::string val; }; - struct SECOND { + struct kSecond { uint16_t num; }; - SimpleFlatEnum(SimpleFlatEnum::FIRST value) { this->variant = value; } - SimpleFlatEnum(SimpleFlatEnum::SECOND value) { this->variant = value; } + SimpleFlatEnum(SimpleFlatEnum::kFirst value) { this->variant = value; } + SimpleFlatEnum(SimpleFlatEnum::kSecond value) { this->variant = value; } - std::variant variant; + std::variant variant; }; class SimpleFlatMacroEnum { public: - struct FIRST { + struct kFirst { std::string val; }; - struct SECOND { + struct kSecond { uint16_t num; }; - SimpleFlatMacroEnum(SimpleFlatMacroEnum::FIRST value) { this->variant = value; } - SimpleFlatMacroEnum(SimpleFlatMacroEnum::SECOND value) { this->variant = value; } + SimpleFlatMacroEnum(SimpleFlatMacroEnum::kFirst value) { this->variant = value; } + SimpleFlatMacroEnum(SimpleFlatMacroEnum::kSecond value) { this->variant = value; } - std::variant variant; + std::variant variant; }; namespace coverall_error { diff --git a/cpp-tests/scaffolding_tests/rondpoint/lib_rondpoint.hpp b/cpp-tests/scaffolding_tests/rondpoint/lib_rondpoint.hpp index 5bf6fbe..bc18893 100644 --- a/cpp-tests/scaffolding_tests/rondpoint/lib_rondpoint.hpp +++ b/cpp-tests/scaffolding_tests/rondpoint/lib_rondpoint.hpp @@ -12,32 +12,32 @@ namespace { }; enum class MinusculeMajusculeEnum { - MINUSCULE_MAJUSCULE_VARIANT + kMinusculeMajusculeVariant }; enum class Enumeration { - UN, - DEUX, - TROIS + kUn, + kDeux, + kTrois }; class EnumerationAvecDonnees { public: - struct ZERO {}; - struct UN { + struct kZero {}; + struct kUn { uint32_t premier; }; - struct DEUX { + struct kDeux { uint32_t premier; std::string second; }; - EnumerationAvecDonnees(EnumerationAvecDonnees::ZERO variant) { this->variant = variant; } - EnumerationAvecDonnees(EnumerationAvecDonnees::UN variant) { this->variant = variant; } - EnumerationAvecDonnees(EnumerationAvecDonnees::DEUX variant) { this->variant = variant; } + EnumerationAvecDonnees(EnumerationAvecDonnees::kZero variant) { this->variant = variant; } + EnumerationAvecDonnees(EnumerationAvecDonnees::kUn variant) { this->variant = variant; } + EnumerationAvecDonnees(EnumerationAvecDonnees::kDeux variant) { this->variant = variant; } - std::variant variant; + std::variant variant; }; struct Dictionnaire { @@ -82,7 +82,7 @@ namespace { std::string string_var = "default"; std::vector list_var = {}; - Enumeration enumeration_var = Enumeration::DEUX; + Enumeration enumeration_var = Enumeration::kDeux; std::optional dictionnaire_var = std::nullopt; }; diff --git a/cpp-tests/tests/coverall/main.cpp b/cpp-tests/tests/coverall/main.cpp index 0d33839..aa1f8f0 100644 --- a/cpp-tests/tests/coverall/main.cpp +++ b/cpp-tests/tests/coverall/main.cpp @@ -116,8 +116,8 @@ void test_complex_errors() { void test_interface_in_dicts() { auto coveralls = coverall::Coveralls::init("test_interface_in_dicts"); - coveralls->add_patch(coverall::Patch::init(coverall::Color::RED)); - coveralls->add_repair(coverall::Repair {.when = std::chrono::system_clock::now(), .patch = coverall::Patch::init(coverall::Color::GREEN)}); + coveralls->add_patch(coverall::Patch::init(coverall::Color::kRed)); + coveralls->add_repair(coverall::Repair {.when = std::chrono::system_clock::now(), .patch = coverall::Patch::init(coverall::Color::kGreen)}); ASSERT_EQ(2, coveralls->get_repairs().size()); } diff --git a/cpp-tests/tests/rondpoint/main.cpp b/cpp-tests/tests/rondpoint/main.cpp index 7ea2169..5b8ae87 100644 --- a/cpp-tests/tests/rondpoint/main.cpp +++ b/cpp-tests/tests/rondpoint/main.cpp @@ -26,7 +26,7 @@ void affirm_enchaine(const T& c, const F& func, Args&& ...arg) { void test_copy() { auto dict = rondpoint::Dictionnaire { - .un = rondpoint::Enumeration::DEUX, + .un = rondpoint::Enumeration::kDeux, .deux = true, .petit_nombre = 0, .gros_nombre = 123456789u @@ -37,15 +37,15 @@ void test_copy() { ASSERT_EQ(dict.petit_nombre, copied_dict.petit_nombre); ASSERT_EQ(dict.gros_nombre, copied_dict.gros_nombre); - auto list = std::vector { rondpoint::Enumeration::UN, rondpoint::Enumeration::DEUX }; + auto list = std::vector { rondpoint::Enumeration::kUn, rondpoint::Enumeration::kDeux}; auto copied_list = rondpoint::copie_enumerations(list); ASSERT_EQ(list.size(), copied_list.size()); ASSERT_TRUE(std::equal(list.begin(), list.end(), copied_list.begin())); auto map = std::unordered_map { - { "zero", rondpoint::EnumerationAvecDonnees::ZERO {}}, - { "un", rondpoint::EnumerationAvecDonnees::UN {.premier = 2}}, - { "deux", rondpoint::EnumerationAvecDonnees::DEUX {.premier= 1, .second = "test"}} + { "zero", rondpoint::EnumerationAvecDonnees::kZero {}}, + { "un", rondpoint::EnumerationAvecDonnees::kUn {.premier = 2}}, + { "deux", rondpoint::EnumerationAvecDonnees::kDeux {.premier= 1, .second = "test"}} }; auto copied_map = rondpoint::copie_carte(map); ASSERT_EQ(map.size(), copied_map.size()); @@ -54,10 +54,10 @@ void test_copy() { auto v = value; std::visit([&v](auto&& arg) { using T = std::decay_t; - if constexpr (std::is_same_v) { - } else if constexpr (std::is_same_v) { + if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { ASSERT_EQ(2, arg.premier); - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { ASSERT_EQ(1, arg.premier); ASSERT_EQ("test", arg.second); } @@ -176,7 +176,7 @@ void test_default_parameter_literals_in_record() { ASSERT_EQ(default_dict.boolean_var, true); ASSERT_EQ(default_dict.string_var, "default"); ASSERT_EQ(default_dict.list_var, std::vector{}); - ASSERT_EQ(default_dict.enumeration_var, rondpoint::Enumeration::DEUX); + ASSERT_EQ(default_dict.enumeration_var, rondpoint::Enumeration::kDeux); ASSERT_EQ(default_dict.dictionnaire_var, std::nullopt); auto rt = rondpoint::Retourneur::init(); @@ -194,7 +194,7 @@ void test_default_parameter_literals_in_record() { .boolean_var = true, .string_var = "default", .list_var = std::vector{}, - .enumeration_var = rondpoint::Enumeration::DEUX, + .enumeration_var = rondpoint::Enumeration::kDeux, .dictionnaire_var = std::nullopt }; auto copied_dict = rt->identique_optionneur_dictionnaire(dict); From 3f785813a0f2f4d001ac27a678fcfa4d5bcb131f Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Thu, 11 Apr 2024 15:30:52 +0300 Subject: [PATCH 3/5] Add enum style test Use the other enum style to verify that compilation is successful Signed-off-by: Martynas Gurskas --- Cargo.lock | 8 +++++ cpp-tests/CMakeLists.txt | 2 ++ .../enum_style_test/lib_enum_style_test.cpp | 18 +++++++++++ .../enum_style_test/lib_enum_style_test.hpp | 30 +++++++++++++++++++ cpp-tests/tests/enum_style_test/main.cpp | 13 ++++++++ fixtures/Cargo.toml | 1 + fixtures/enum-style-test/Cargo.toml | 14 +++++++++ fixtures/enum-style-test/build.rs | 3 ++ .../enum-style-test/src/enum_style_test.udl | 19 ++++++++++++ fixtures/enum-style-test/src/lib.rs | 24 +++++++++++++++ fixtures/enum-style-test/uniffi.toml | 5 ++++ fixtures/src/lib.rs | 1 + 12 files changed, 138 insertions(+) create mode 100644 cpp-tests/scaffolding_tests/enum_style_test/lib_enum_style_test.cpp create mode 100644 cpp-tests/scaffolding_tests/enum_style_test/lib_enum_style_test.hpp create mode 100644 cpp-tests/tests/enum_style_test/main.cpp create mode 100644 fixtures/enum-style-test/Cargo.toml create mode 100644 fixtures/enum-style-test/build.rs create mode 100644 fixtures/enum-style-test/src/enum_style_test.udl create mode 100644 fixtures/enum-style-test/src/lib.rs create mode 100644 fixtures/enum-style-test/uniffi.toml diff --git a/Cargo.lock b/Cargo.lock index 1b56677..c3ead5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -871,6 +871,7 @@ name = "uniffi-bindgen-cpp-fixtures" version = "1.0.0" dependencies = [ "uniffi-custom-types-builtin", + "uniffi-enum-style-test", "uniffi-example-arithmetic", "uniffi-example-callbacks", "uniffi-example-custom-types", @@ -896,6 +897,13 @@ dependencies = [ "uniffi", ] +[[package]] +name = "uniffi-enum-style-test" +version = "0.1.0" +dependencies = [ + "uniffi", +] + [[package]] name = "uniffi-example-arithmetic" version = "0.22.0" diff --git a/cpp-tests/CMakeLists.txt b/cpp-tests/CMakeLists.txt index 6f3ccd3..d37b9f1 100644 --- a/cpp-tests/CMakeLists.txt +++ b/cpp-tests/CMakeLists.txt @@ -86,6 +86,7 @@ test_case(coverall) test_case(uniffi_docstring) test_case(trait_methods) test_case(custom_types_builtin) +test_case(enum_style_test) scaffolding_test_case(arithmetic) scaffolding_test_case(callbacks) @@ -99,6 +100,7 @@ scaffolding_test_case(todolist) scaffolding_test_case(traits) scaffolding_test_case(coverall) scaffolding_test_case(custom_types_builtin) +scaffolding_test_case(enum_style_test) add_library(uniffi_fixtures SHARED ${SCAFFOLDING_LIB_FILES}) diff --git a/cpp-tests/scaffolding_tests/enum_style_test/lib_enum_style_test.cpp b/cpp-tests/scaffolding_tests/enum_style_test/lib_enum_style_test.cpp new file mode 100644 index 0000000..fad9ffa --- /dev/null +++ b/cpp-tests/scaffolding_tests/enum_style_test/lib_enum_style_test.cpp @@ -0,0 +1,18 @@ +#include + +enum_style_test::SimpleEnum enum_style_test::get_simple_enum() { + return enum_style_test::SimpleEnum::VARIANT_ONE; +} + +void enum_style_test::set_simple_enum(enum_style_test::SimpleEnum) { +} + +enum_style_test::ComplexEnum enum_style_test::get_complex_enum() { + return enum_style_test::ComplexEnum { enum_style_test::ComplexEnum::VARIANT_ONE { 1 } }; +} + +void enum_style_test::set_complex_enum(enum_style_test::ComplexEnum) { +} + + +#include diff --git a/cpp-tests/scaffolding_tests/enum_style_test/lib_enum_style_test.hpp b/cpp-tests/scaffolding_tests/enum_style_test/lib_enum_style_test.hpp new file mode 100644 index 0000000..6c338e0 --- /dev/null +++ b/cpp-tests/scaffolding_tests/enum_style_test/lib_enum_style_test.hpp @@ -0,0 +1,30 @@ +#include +#include + +namespace { + namespace enum_style_test { + enum class SimpleEnum: int32_t { + VARIANT_ONE = 1, + VARIANT_TWO = 2, + VARIANT_THREE = 3 + }; + + + class ComplexEnum { + public: + struct VARIANT_ONE { uint32_t num; }; + struct VARIANT_TWO { float flt; }; + + ComplexEnum(ComplexEnum::VARIANT_ONE variant) { this->variant = variant; } + ComplexEnum(ComplexEnum::VARIANT_TWO variant) { this->variant = variant; } + + std::variant variant; + }; + + SimpleEnum get_simple_enum(); + void set_simple_enum(SimpleEnum e); + + ComplexEnum get_complex_enum(); + void set_complex_enum(ComplexEnum e); + } +} diff --git a/cpp-tests/tests/enum_style_test/main.cpp b/cpp-tests/tests/enum_style_test/main.cpp new file mode 100644 index 0000000..6c82a36 --- /dev/null +++ b/cpp-tests/tests/enum_style_test/main.cpp @@ -0,0 +1,13 @@ +#include + +#include + +int main() { + auto simple = enum_style_test::get_simple_enum(); + enum_style_test::set_simple_enum(simple); + + auto complex = enum_style_test::get_complex_enum(); + enum_style_test::set_complex_enum(complex); + + return 0; +} diff --git a/fixtures/Cargo.toml b/fixtures/Cargo.toml index 958d813..b76ddb9 100644 --- a/fixtures/Cargo.toml +++ b/fixtures/Cargo.toml @@ -21,3 +21,4 @@ uniffi-fixture-docstring = { git = "https://github.com/NordSecurity/uniffi-rs.gi uniffi-fixture-time = { git = "https://github.com/NordSecurity/uniffi-rs.git", tag = "v0.3.1+v0.25.0" } uniffi-fixture-trait-methods = { git = "https://github.com/NordSecurity/uniffi-rs.git", tag = "v0.3.1+v0.25.0" } uniffi-custom-types-builtin = { path = "custom-types-builtin" } +uniffi-enum-style-test = { path = "enum-style-test" } diff --git a/fixtures/enum-style-test/Cargo.toml b/fixtures/enum-style-test/Cargo.toml new file mode 100644 index 0000000..679b821 --- /dev/null +++ b/fixtures/enum-style-test/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "uniffi-enum-style-test" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["lib", "cdylib"] +name = "uniffi_enum_style_test" + +[dependencies] +uniffi = { workspace = true } + +[build-dependencies] +uniffi = { workspace = true, features = ["build"] } diff --git a/fixtures/enum-style-test/build.rs b/fixtures/enum-style-test/build.rs new file mode 100644 index 0000000..c357e91 --- /dev/null +++ b/fixtures/enum-style-test/build.rs @@ -0,0 +1,3 @@ +fn main() { + uniffi::generate_scaffolding("src/enum_style_test.udl").unwrap(); +} diff --git a/fixtures/enum-style-test/src/enum_style_test.udl b/fixtures/enum-style-test/src/enum_style_test.udl new file mode 100644 index 0000000..b805102 --- /dev/null +++ b/fixtures/enum-style-test/src/enum_style_test.udl @@ -0,0 +1,19 @@ +enum SimpleEnum { + "VariantOne", + "VariantTwo", + "VariantThree", +}; + +[Enum] +interface ComplexEnum { + VariantOne(u32 num); + VariantTwo(f32 flt); +}; + +namespace enum_style_test { + SimpleEnum get_simple_enum(); + void set_simple_enum(SimpleEnum e); + + ComplexEnum get_complex_enum(); + void set_complex_enum(ComplexEnum e); +}; diff --git a/fixtures/enum-style-test/src/lib.rs b/fixtures/enum-style-test/src/lib.rs new file mode 100644 index 0000000..067a6e7 --- /dev/null +++ b/fixtures/enum-style-test/src/lib.rs @@ -0,0 +1,24 @@ +enum SimpleEnum { + VariantOne, + VariantTwo, + VariantThree, +} + +enum ComplexEnum { + VariantOne { num: u32 }, + VariantTwo { flt: f32 }, +} + +fn get_simple_enum() -> SimpleEnum { + SimpleEnum::VariantOne +} + +fn set_simple_enum(_: SimpleEnum) {} + +fn get_complex_enum() -> ComplexEnum { + ComplexEnum::VariantOne { num: 42 } +} + +fn set_complex_enum(_: ComplexEnum) {} + +uniffi::include_scaffolding!("enum_style_test"); diff --git a/fixtures/enum-style-test/uniffi.toml b/fixtures/enum-style-test/uniffi.toml new file mode 100644 index 0000000..9be0b03 --- /dev/null +++ b/fixtures/enum-style-test/uniffi.toml @@ -0,0 +1,5 @@ +[bindings.cpp] +enum_style = "Capitalized" + +[scaffolding.cpp] +enum_style = "Capitalized" diff --git a/fixtures/src/lib.rs b/fixtures/src/lib.rs index e581470..c03d159 100644 --- a/fixtures/src/lib.rs +++ b/fixtures/src/lib.rs @@ -14,4 +14,5 @@ mod uniffi_fixtures { uniffi_fixture_callbacks::uniffi_reexport_scaffolding!(); uniffi_cpp_custom_types_builtin::uniffi_reexport_scaffolding!(); + uniffi_enum_style_test::uniffi_reexport_scaffolding!(); } From b5db753cb3853e0a366d9f3670e483b4d66afed9 Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Thu, 11 Apr 2024 15:34:25 +0300 Subject: [PATCH 4/5] Fix Rust code Signed-off-by: Martynas Gurskas --- bindgen/src/bindings/cpp/gen_cpp/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindgen/src/bindings/cpp/gen_cpp/mod.rs b/bindgen/src/bindings/cpp/gen_cpp/mod.rs index 29f01c9..a590026 100644 --- a/bindgen/src/bindings/cpp/gen_cpp/mod.rs +++ b/bindgen/src/bindings/cpp/gen_cpp/mod.rs @@ -25,7 +25,7 @@ use uniffi_bindgen::{ }; #[derive(Serialize, Deserialize, Clone, Debug)] -enum EnumStyle { +pub enum EnumStyle { Capitalized, Google, } From 3a5b5861d8929900b0a694333d1cce796dc14778 Mon Sep 17 00:00:00 2001 From: Martynas Gurskas Date: Thu, 11 Apr 2024 16:05:05 +0300 Subject: [PATCH 5/5] Update docs Signed-off-by: Martynas Gurskas --- CHANGELOG.md | 6 ++++++ docs/CONFIGURATION.md | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18df8b9..e664062 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +#### v0.6.0+v0.25.0 + +---- +- Core: Added ability to customize enum variant naming styles +- Core: **BREAKING** Changed default enum variant naming style to `kEnumVariant` + #### v0.5.0+v0.25.0 ---- diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 6dfa65f..68f93df 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -30,3 +30,9 @@ uniffi-bindgen-cpp path/to/definitions.udl --config path/to/uniffi.toml - `from_custom` (required) - an expression to convert from the custom type into underlying type. `{}` will will be expanded into variable containing the custom value. The expression is used in a return statement, i.e. `return `. + +- `enum_style` - style for enum variant naming, possible options are: + - `"Capitalized"` - producing enum variants named `ENUM_VARIANT` + - `"Google"` - producing enum variants name `kEnumVariant` (default) + +NOTE: the `enum_style` option is separate for bindings and scaffolding generators, to apply this to the scaffolding generator, use the section `[scaffolding.cpp]` instead.