Import protobuf-codegen-2.14.0
* Add OWNERS, Android.bp, and README.android.
* Hard code version number in src/lib.rs for now.
It could be in a smarter update_package.sh to get the
new version number from Cargo.tom. But until then,
the difference in lib.rs will be caught and fixed manually.
* Rename protoc_gen_rust to protoc-gen-rust for aprotoc plugin.
Bug: 143953733
Test: make
Change-Id: I9b3c3b9f2e7ad0eb203c26534f2b6ba5fac46eef
diff --git a/src/extensions.rs b/src/extensions.rs
new file mode 100644
index 0000000..a3f8901
--- /dev/null
+++ b/src/extensions.rs
@@ -0,0 +1,109 @@
+use super::code_writer::CodeWriter;
+use super::rust_types_values::*;
+use field::rust_field_name_for_protobuf_field_name;
+use protobuf::descriptor::*;
+use protobuf_name::ProtobufAbsolutePath;
+use scope::RootScope;
+use Customize;
+
+struct ExtGen<'a> {
+ file: &'a FileDescriptorProto,
+ root_scope: &'a RootScope<'a>,
+ field: &'a FieldDescriptorProto,
+ customize: Customize,
+}
+
+impl<'a> ExtGen<'a> {
+ fn extendee_rust_name(&self) -> String {
+ type_name_to_rust_relative(
+ &ProtobufAbsolutePath::from(self.field.get_extendee()),
+ self.file,
+ true,
+ self.root_scope,
+ )
+ }
+
+ fn repeated(&self) -> bool {
+ match self.field.get_label() {
+ FieldDescriptorProto_Label::LABEL_REPEATED => true,
+ FieldDescriptorProto_Label::LABEL_OPTIONAL => false,
+ FieldDescriptorProto_Label::LABEL_REQUIRED => {
+ panic!("required ext field: {}", self.field.get_name())
+ }
+ }
+ }
+
+ fn return_type_gen(&self) -> ProtobufTypeGen {
+ if self.field.has_type_name() {
+ let rust_name_relative = type_name_to_rust_relative(
+ &ProtobufAbsolutePath::from(self.field.get_type_name()),
+ self.file,
+ true,
+ self.root_scope,
+ );
+ match self.field.get_field_type() {
+ FieldDescriptorProto_Type::TYPE_MESSAGE => {
+ ProtobufTypeGen::Message(rust_name_relative)
+ }
+ FieldDescriptorProto_Type::TYPE_ENUM => ProtobufTypeGen::Enum(rust_name_relative),
+ t => panic!("unknown type: {:?}", t),
+ }
+ } else {
+ ProtobufTypeGen::Primitive(self.field.get_field_type(), PrimitiveTypeVariant::Default)
+ }
+ }
+
+ fn write(&self, w: &mut CodeWriter) {
+ let suffix = if self.repeated() {
+ "Repeated"
+ } else {
+ "Optional"
+ };
+ let field_type = format!("::protobuf::ext::ExtField{}", suffix);
+ w.pub_const(
+ rust_field_name_for_protobuf_field_name(self.field.get_name()).get(),
+ &format!(
+ "{}<{}, {}>",
+ field_type,
+ self.extendee_rust_name(),
+ self.return_type_gen().rust_type(&self.customize),
+ ),
+ &format!(
+ "{} {{ field_number: {}, phantom: ::std::marker::PhantomData }}",
+ field_type,
+ self.field.get_number()
+ ),
+ );
+ }
+}
+
+pub(crate) fn write_extensions(
+ file: &FileDescriptorProto,
+ root_scope: &RootScope,
+ w: &mut CodeWriter,
+ customize: &Customize,
+) {
+ if file.get_extension().is_empty() {
+ return;
+ }
+
+ w.write_line("");
+ w.pub_mod("exts", |w| {
+ w.write_line("use protobuf::Message as Message_imported_for_functions;");
+
+ for field in file.get_extension() {
+ if field.get_field_type() == FieldDescriptorProto_Type::TYPE_GROUP {
+ continue;
+ }
+
+ w.write_line("");
+ ExtGen {
+ file: file,
+ root_scope: root_scope,
+ field: field,
+ customize: customize.clone(),
+ }
+ .write(w);
+ }
+ });
+}