Skip to content

Commit 5ca9573

Browse files
committed
Change enum names to be more idiomatic.
Fix #52
1 parent 678fe09 commit 5ca9573

File tree

5 files changed

+60
-54
lines changed

5 files changed

+60
-54
lines changed

generators/c-oo-bindgen/src/doc.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::CFormatting;
2-
use heck::{CamelCase, SnakeCase};
2+
use heck::{ShoutySnakeCase, SnakeCase};
33
use oo_bindgen::doc::*;
44
use oo_bindgen::formatting::*;
55
use oo_bindgen::Library;
@@ -68,14 +68,16 @@ fn reference_print(
6868
DocReference::ClassConstructor(class_name) => {
6969
let handle = lib.find_class(class_name).unwrap();
7070
f.write(&format!(
71-
"@ref {}",
71+
"@ref {}_{}",
72+
lib.c_ffi_prefix,
7273
handle.constructor.as_ref().unwrap().name
7374
))?;
7475
}
7576
DocReference::ClassDestructor(class_name) => {
7677
let handle = lib.find_class(class_name).unwrap();
7778
f.write(&format!(
78-
"@ref {}",
79+
"@ref {}_{}",
80+
lib.c_ffi_prefix,
7981
handle.destructor.as_ref().unwrap().name
8082
))?;
8183
}
@@ -93,7 +95,7 @@ fn reference_print(
9395
.find_method(method_name)
9496
.unwrap()
9597
.name;
96-
f.write(&format!("@ref {}", func_name))?;
98+
f.write(&format!("@ref {}_{}", lib.c_ffi_prefix, func_name))?;
9799
}
98100
DocReference::StructElement(struct_name, element_name) => {
99101
let handle = lib.find_struct(struct_name).unwrap();
@@ -111,9 +113,9 @@ fn reference_print(
111113
let handle = lib.find_enum(enum_name).unwrap();
112114
f.write(&format!(
113115
"@ref {}_{}_{}",
114-
lib.c_ffi_prefix,
115-
handle.name.to_camel_case(),
116-
variant_name.to_camel_case()
116+
lib.c_ffi_prefix.to_shouty_snake_case(),
117+
handle.name.to_shouty_snake_case(),
118+
variant_name.to_shouty_snake_case()
117119
))?;
118120
}
119121
DocReference::Interface(interface_name) => {

generators/c-oo-bindgen/src/lib.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ clippy::all
4646

4747
use crate::doc::*;
4848
use crate::formatting::*;
49-
use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
49+
use heck::{ShoutySnakeCase, SnakeCase};
5050
use oo_bindgen::callback::*;
5151
use oo_bindgen::class::*;
5252
use oo_bindgen::constants::{ConstantSetHandle, ConstantValue, Representation};
@@ -374,9 +374,10 @@ fn write_struct_definition(
374374
StructElementType::StructRef(_) => None,
375375
StructElementType::Enum(handle, default) => default.clone().map(|x| {
376376
format!(
377-
"@ref {}_{}",
378-
handle.to_c_type(&lib.c_ffi_prefix),
379-
x.to_camel_case()
377+
"@ref {}_{}_{}",
378+
lib.c_ffi_prefix.to_shouty_snake_case(),
379+
handle.name.to_shouty_snake_case(),
380+
x.to_shouty_snake_case()
380381
)
381382
}),
382383
StructElementType::ClassRef(_) => None,
@@ -540,9 +541,9 @@ fn write_struct_initializer(
540541
Some(value) => match handle.find_variant_by_name(value) {
541542
Some(variant) => format!(
542543
"{}_{}_{}",
543-
&lib.c_ffi_prefix,
544-
handle.name.to_camel_case(),
545-
variant.name.to_camel_case()
544+
lib.c_ffi_prefix.to_shouty_snake_case(),
545+
handle.name.to_shouty_snake_case(),
546+
variant.name.to_shouty_snake_case()
546547
),
547548
None => panic!("Variant {} not found in {}", value, handle.name),
548549
},
@@ -585,9 +586,9 @@ fn write_enum_definition(
585586
doxygen(f, |f| doxygen_print(f, &variant.doc, lib))?;
586587
f.writeln(&format!(
587588
"{}_{}_{} = {},",
588-
&lib.c_ffi_prefix,
589-
handle.name.to_camel_case(),
590-
variant.name.to_camel_case(),
589+
lib.c_ffi_prefix.to_shouty_snake_case(),
590+
handle.name.to_shouty_snake_case(),
591+
variant.name.to_shouty_snake_case(),
591592
variant.value
592593
))?;
593594
}
@@ -610,7 +611,7 @@ fn write_enum_definition(
610611
f.writeln(&format!(
611612
"static const char* {}_{}_to_string({} value)",
612613
&lib.c_ffi_prefix,
613-
handle.name.to_camel_case(),
614+
handle.name.to_snake_case(),
614615
handle.to_c_type(&lib.c_ffi_prefix)
615616
))?;
616617
blocked(f, |f| {
@@ -619,13 +620,13 @@ fn write_enum_definition(
619620
for variant in &handle.variants {
620621
f.writeln(&format!(
621622
"case {}_{}_{}: return \"{}\";",
622-
&lib.c_ffi_prefix,
623-
handle.name.to_camel_case(),
624-
variant.name.to_camel_case(),
623+
lib.c_ffi_prefix.to_shouty_snake_case(),
624+
handle.name.to_shouty_snake_case(),
625+
variant.name.to_shouty_snake_case(),
625626
variant.name
626627
))?;
627628
}
628-
f.writeln("default: return \"\";")
629+
f.writeln(&format!("default: return \"Unknown{}\";", handle.name))
629630
})
630631
})
631632
}
@@ -641,8 +642,9 @@ fn write_class_declaration(
641642
doxygen_print(
642643
f,
643644
&Doc::from(&*format!(
644-
"Iterator of {{struct:{}}}. See @ref {}.",
645+
"Iterator of {{struct:{}}}. See @ref {}_{}.",
645646
handle.item_type.name(),
647+
lib.c_ffi_prefix,
646648
handle.native_func.name
647649
)),
648650
lib,
@@ -652,9 +654,11 @@ fn write_class_declaration(
652654
doxygen_print(
653655
f,
654656
&Doc::from(&*format!(
655-
"Collection of {}. See @ref {} and @ref {}.",
657+
"Collection of {}. See @ref {}_{} and @ref {}_{}.",
656658
handle.item_type.to_c_type(&lib.c_ffi_prefix),
659+
lib.c_ffi_prefix,
657660
handle.add_func.name,
661+
lib.c_ffi_prefix,
658662
handle.delete_func.name
659663
)),
660664
lib,

tests/bindings/c/enum_tests.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,117 +5,117 @@
55

66
static void test_enum_zero_to_five()
77
{
8-
foo_enum_zero_to_five_t value = foo_EnumZeroToFive_Zero;
8+
foo_enum_zero_to_five_t value = FOO_ENUM_ZERO_TO_FIVE_ZERO;
99
foo_enum_zero_to_five_t result = foo_enum_zero_to_five_echo(value);
1010
assert(result == value);
1111
assert(result == 0);
1212

13-
value = foo_EnumZeroToFive_One;
13+
value = FOO_ENUM_ZERO_TO_FIVE_ONE;
1414
result = foo_enum_zero_to_five_echo(value);
1515
assert(result == value);
1616
assert(result == 1);
1717

18-
value = foo_EnumZeroToFive_Two;
18+
value = FOO_ENUM_ZERO_TO_FIVE_TWO;
1919
result = foo_enum_zero_to_five_echo(value);
2020
assert(result == value);
2121
assert(result == 2);
2222

23-
value = foo_EnumZeroToFive_Three;
23+
value = FOO_ENUM_ZERO_TO_FIVE_THREE;
2424
result = foo_enum_zero_to_five_echo(value);
2525
assert(result == value);
2626
assert(result == 3);
2727

28-
value = foo_EnumZeroToFive_Four;
28+
value = FOO_ENUM_ZERO_TO_FIVE_FOUR;
2929
result = foo_enum_zero_to_five_echo(value);
3030
assert(result == value);
3131
assert(result == 4);
3232

33-
value = foo_EnumZeroToFive_Five;
33+
value = FOO_ENUM_ZERO_TO_FIVE_FIVE;
3434
result = foo_enum_zero_to_five_echo(value);
3535
assert(result == value);
3636
assert(result == 5);
3737
}
3838

3939
static void test_enum_one_to_six()
4040
{
41-
foo_enum_one_to_six_t value = foo_EnumOneToSix_One;
41+
foo_enum_one_to_six_t value = FOO_ENUM_ONE_TO_SIX_ONE;
4242
foo_enum_one_to_six_t result = foo_enum_one_to_six_echo(value);
4343
assert(result == value);
4444
assert(result == 1);
4545

46-
value = foo_EnumOneToSix_Two;
46+
value = FOO_ENUM_ONE_TO_SIX_TWO;
4747
result = foo_enum_one_to_six_echo(value);
4848
assert(result == value);
4949
assert(result == 2);
5050

51-
value = foo_EnumOneToSix_Three;
51+
value = FOO_ENUM_ONE_TO_SIX_THREE;
5252
result = foo_enum_one_to_six_echo(value);
5353
assert(result == value);
5454
assert(result == 3);
5555

56-
value = foo_EnumOneToSix_Four;
56+
value = FOO_ENUM_ONE_TO_SIX_FOUR;
5757
result = foo_enum_one_to_six_echo(value);
5858
assert(result == value);
5959
assert(result == 4);
6060

61-
value = foo_EnumOneToSix_Five;
61+
value = FOO_ENUM_ONE_TO_SIX_FIVE;
6262
result = foo_enum_one_to_six_echo(value);
6363
assert(result == value);
6464
assert(result == 5);
6565

66-
value = foo_EnumOneToSix_Six;
66+
value = FOO_ENUM_ONE_TO_SIX_SIX;
6767
result = foo_enum_one_to_six_echo(value);
6868
assert(result == value);
6969
assert(result == 6);
7070
}
7171

7272
static void test_enum_disjoint()
7373
{
74-
foo_enum_disjoint_t value = foo_EnumDisjoint_Five;
74+
foo_enum_disjoint_t value = FOO_ENUM_DISJOINT_FIVE;
7575
foo_enum_disjoint_t result = foo_enum_disjoint_echo(value);
7676
assert(result == value);
7777
assert(result == 5);
7878

79-
value = foo_EnumDisjoint_One;
79+
value = FOO_ENUM_DISJOINT_ONE;
8080
result = foo_enum_disjoint_echo(value);
8181
assert(result == value);
8282
assert(result == 1);
8383

84-
value = foo_EnumDisjoint_Twenty;
84+
value = FOO_ENUM_DISJOINT_TWENTY;
8585
result = foo_enum_disjoint_echo(value);
8686
assert(result == value);
8787
assert(result == 20);
8888

89-
value = foo_EnumDisjoint_Four;
89+
value = FOO_ENUM_DISJOINT_FOUR;
9090
result = foo_enum_disjoint_echo(value);
9191
assert(result == value);
9292
assert(result == 4);
9393

94-
value = foo_EnumDisjoint_Seven;
94+
value = FOO_ENUM_DISJOINT_SEVEN;
9595
result = foo_enum_disjoint_echo(value);
9696
assert(result == value);
9797
assert(result == 7);
9898

99-
value = foo_EnumDisjoint_Two;
99+
value = FOO_ENUM_DISJOINT_TWO;
100100
result = foo_enum_disjoint_echo(value);
101101
assert(result == value);
102102
assert(result == 2);
103103
}
104104

105105
static void test_enum_single()
106106
{
107-
foo_enum_single_t value = foo_EnumSingle_Single;
107+
foo_enum_single_t value = FOO_ENUM_SINGLE_SINGLE;
108108
foo_enum_single_t result = foo_enum_single_echo(value);
109109
assert(result == value);
110110
assert(result == 0);
111111
}
112112

113113
static void test_enum_to_string()
114114
{
115-
assert(strcmp("Two", foo_EnumZeroToFive_to_string(foo_EnumZeroToFive_Two)) == 0);
116-
assert(strcmp("Five", foo_EnumDisjoint_to_string(foo_EnumDisjoint_Five)) == 0);
117-
assert(strcmp("Single", foo_EnumSingle_to_string(foo_EnumSingle_Single)) == 0);
118-
assert(strcmp("", foo_EnumSingle_to_string((foo_enum_single_t)foo_EnumZeroToFive_Four)) == 0);
115+
assert(strcmp("Two", foo_enum_zero_to_five_to_string(FOO_ENUM_ZERO_TO_FIVE_TWO)) == 0);
116+
assert(strcmp("Five", foo_enum_disjoint_to_string(FOO_ENUM_DISJOINT_FIVE)) == 0);
117+
assert(strcmp("Single", foo_enum_single_to_string(FOO_ENUM_SINGLE_SINGLE)) == 0);
118+
assert(strcmp("UnknownEnumSingle", foo_enum_single_to_string((foo_enum_single_t)FOO_ENUM_ZERO_TO_FIVE_FOUR)) == 0);
119119
}
120120

121121
void enum_tests()

tests/bindings/c/error_tests.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111
static void test_integer_out_parameter()
1212
{
1313
uint32_t number = 0;
14-
assert(foo_get_special_number(WRONG_PASSWORD, &number) == foo_MyError_BadPassword);
14+
assert(foo_get_special_number(WRONG_PASSWORD, &number) == FOO_MY_ERROR_BAD_PASSWORD);
1515
assert(number == 0);
16-
assert(foo_get_special_number(CORRECT_PASSWORD, &number) == foo_MyError_Ok);
16+
assert(foo_get_special_number(CORRECT_PASSWORD, &number) == FOO_MY_ERROR_OK);
1717
assert(number == MAGIC_NUMBER);
1818
}
1919

2020
static void test_allocation_via_out_parameter()
2121
{
2222
foo_class_with_password_t* instance = NULL;
23-
assert(foo_create_class_with_password(WRONG_PASSWORD, &instance) == foo_MyError_BadPassword);
23+
assert(foo_create_class_with_password(WRONG_PASSWORD, &instance) == FOO_MY_ERROR_BAD_PASSWORD);
2424
assert(!instance);
25-
assert(foo_create_class_with_password(CORRECT_PASSWORD, &instance) == foo_MyError_Ok);
25+
assert(foo_create_class_with_password(CORRECT_PASSWORD, &instance) == FOO_MY_ERROR_OK);
2626
assert(instance);
2727

2828
uint32_t number = 0;
29-
assert(foo_get_special_value_from_class(instance, &number) == foo_MyError_Ok);
29+
assert(foo_get_special_value_from_class(instance, &number) == FOO_MY_ERROR_OK);
3030
assert(number == MAGIC_NUMBER);
3131

3232
foo_destroy_class_with_password(instance);

tests/bindings/c/structure_tests.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static foo_structure_t create_struct()
2727
{
2828
.test = 41
2929
},
30-
.enum_value = foo_StructureEnum_Var2,
30+
.enum_value = FOO_STRUCTURE_ENUM_VAR2,
3131

3232
.duration_millis = 4200,
3333
.duration_seconds = 76,
@@ -53,7 +53,7 @@ static void check_struct(foo_structure_t* structure)
5353
assert(fabs(structure->double_value + 56.78) < 0.001);
5454

5555
assert(structure->structure_value.test == 41);
56-
assert(structure->enum_value == foo_StructureEnum_Var2);
56+
assert(structure->enum_value == FOO_STRUCTURE_ENUM_VAR2);
5757

5858
assert(structure->duration_millis == 4200);
5959
assert(structure->duration_seconds == 76);

0 commit comments

Comments
 (0)