Skip to content

Commit 412adb3

Browse files
committed
also support renaming enumerants
1 parent 8b8c456 commit 412adb3

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

capnpc/rust.capnp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@0x83b3c14c3c8dd083;
22

3-
annotation name @0xc2fe4c6d100166d0 (field, struct, enum) :Text;
3+
annotation name @0xc2fe4c6d100166d0 (field, struct, enum, enumerant) :Text;
44
# Rename something in the generated code. The value that you specify in this
55
# annotion must follow capnp capitalization conventions. So, for example,
66
# a struct could be named `StructFoo`, even though that will get translated

capnpc/src/codegen.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ fn get_field_name(field: schema_capnp::field::Reader) -> capnp::Result<&str> {
252252
field.get_name()
253253
}
254254

255+
fn get_enumerant_name(enumerant: schema_capnp::enumerant::Reader) -> capnp::Result<&str> {
256+
for annotation in enumerant.get_annotations()?.iter() {
257+
if annotation.get_id() == NAME_ANNOTATION_ID {
258+
if let schema_capnp::value::Text(t) = annotation.get_value()?.which()? {
259+
return t;
260+
} else {
261+
return Err(capnp::Error::failed(format!("expected rust.name annotation value to be of type Text")));
262+
}
263+
}
264+
}
265+
enumerant.get_name()
266+
}
267+
255268
enum NameKind {
256269
// convert camel case to snake case, and avoid Rust keywords
257270
Module,
@@ -1464,7 +1477,7 @@ fn generate_node(gen: &GeneratorContext,
14641477
let mut match_branches = Vec::new();
14651478
let enumerants = enum_reader.get_enumerants()?;
14661479
for ii in 0..enumerants.len() {
1467-
let enumerant = capitalize_first_letter(enumerants.get(ii).get_name()?);
1480+
let enumerant = capitalize_first_letter(get_enumerant_name(enumerants.get(ii))?);
14681481
members.push(Line(format!("{} = {},", enumerant, ii)));
14691482
match_branches.push(
14701483
Line(format!("{} => ::std::result::Result::Ok({}::{}),", ii, last_name, enumerant)));
@@ -1837,7 +1850,7 @@ fn generate_node(gen: &GeneratorContext,
18371850
let enumerants = e.get_enumerants()?;
18381851
if (v as u32) < enumerants.len() {
18391852
let variant =
1840-
capitalize_first_letter(enumerants.get(v as u32).get_name()?);
1853+
capitalize_first_letter(get_enumerant_name(enumerants.get(v as u32))?);
18411854
let type_string = typ.type_string(gen, Leaf::Owned)?;
18421855
Line(format!("pub const {}: {} = {}::{};",
18431856
styled_name,

capnpc/test/test.capnp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,19 @@ struct TestNameAnnotation $Rust.name("RenamedStruct") {
756756
enum BadlyNamedEnum $Rust.name("RenamedEnum") {
757757
foo @0;
758758
bar @1;
759+
baz @2 $Rust.name("qux");
759760
}
760761
761762
anotherBadFieldName @2 :BadlyNamedEnum $Rust.name("anotherGoodFieldName");
762-
# ...
763+
764+
struct NestedStruct $Rust.name("RenamedNestedStruct") {
765+
badNestedFieldName @0 :Bool $Rust.name("goodNestedFieldName");
766+
anotherBadNestedFieldName @1 :NestedStruct $Rust.name("anotherGoodNestedFieldName");
767+
768+
enum DeeplyNestedEnum $Rust.name("RenamedDeeplyNestedEnum") {
769+
quux @0;
770+
corge @1;
771+
grault @2 $Rust.name("garply");
772+
}
773+
}
763774
}

0 commit comments

Comments
 (0)