diff --git a/protobuf-codegen/src/extensions.rs b/protobuf-codegen/src/extensions.rs index d3b417a46..b463dddd3 100644 --- a/protobuf-codegen/src/extensions.rs +++ b/protobuf-codegen/src/extensions.rs @@ -53,7 +53,7 @@ impl<'a> ExtGen<'a> { }; let field_type = format!("::protobuf::ext::ExtField{}", suffix); w.pub_const( - self.field.get_name(), // TODO: escape + &self.field.rust_name(), &format!( "{}<{}, {}>", field_type, diff --git a/protobuf-codegen/src/field.rs b/protobuf-codegen/src/field.rs index bad0c6876..5bae17500 100644 --- a/protobuf-codegen/src/field.rs +++ b/protobuf-codegen/src/field.rs @@ -474,7 +474,7 @@ impl<'a> FieldGen<'a> { FieldGen { root_scope: root_scope, syntax: field.message.get_scope().file_scope.syntax(), - rust_name: field.rust_name(), + rust_name: field.field.rust_name(), proto_type: field.field.get_field_type(), wire_type: field_type_wire_type(field.field.get_field_type()), enum_default_value: enum_default_value, diff --git a/protobuf-test/src/common/v2/test_ident_pb.proto b/protobuf-test/src/common/v2/test_ident_pb.proto index 91ade19a9..280e59bb8 100644 --- a/protobuf-test/src/common/v2/test_ident_pb.proto +++ b/protobuf-test/src/common/v2/test_ident_pb.proto @@ -1,5 +1,7 @@ syntax = "proto2"; +import "google/protobuf/descriptor.proto"; + package test_ident; // rust types defined in prelude @@ -65,3 +67,7 @@ message MessageReferencesEnumWithDefaultValueReserved { EnumWithDefaultValueReserved e4 = 4; } } + +extend google.protobuf.EnumValueOptions { + optional string type = 51234; +} diff --git a/protobuf/src/descriptorx.rs b/protobuf/src/descriptorx.rs index 6d8675821..cbc71d5fa 100644 --- a/protobuf/src/descriptorx.rs +++ b/protobuf/src/descriptorx.rs @@ -491,6 +491,19 @@ impl<'a> WithScope<'a> for MessageOrEnumWithScope<'a> { } } +pub trait FieldDescriptorProtoExt { + fn rust_name(&self) -> String; +} + +impl FieldDescriptorProtoExt for FieldDescriptorProto { + fn rust_name(&self) -> String { + if rust::is_rust_keyword(self.get_name()) { + format!("field_{}", self.get_name()) + } else { + self.get_name().to_string() + } + } +} #[derive(Clone)] pub struct FieldWithContext<'a> { @@ -524,12 +537,9 @@ impl<'a> FieldWithContext<'a> { } // field name in generated code + #[deprecated] pub fn rust_name(&self) -> String { - if rust::is_rust_keyword(self.field.get_name()) { - format!("field_{}", self.field.get_name()) - } else { - self.field.get_name().to_string() - } + self.field.rust_name() } // From field to file root