The purpose is to avoid duplicating the mapping of different types that are only relevant to the serializer but not to the exposed api (e.g. FIXED32 vs INT32) Treat type=GROUP as rust-type=MESSAGE here which is all that is needed for us to support groups in the rust codegen. The RustFieldType is parallel to the preexisting FieldDescriptor::CppType which _almost_ does what we need, but it treats Bytes and Strings as the same cpptype which Rust codegen doesn't. PiperOrigin-RevId: 609416940pull/15921/head
parent
b9483e03c7
commit
033ff1710e
8 changed files with 189 additions and 115 deletions
@ -0,0 +1,53 @@ |
||||
#include "google/protobuf/compiler/rust/rust_field_type.h" |
||||
|
||||
#include "absl/log/absl_log.h" |
||||
#include "google/protobuf/descriptor.h" |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
namespace compiler { |
||||
namespace rust { |
||||
|
||||
RustFieldType GetRustFieldType(const FieldDescriptor& field) { |
||||
return GetRustFieldType(field.type()); |
||||
} |
||||
|
||||
RustFieldType GetRustFieldType(FieldDescriptor::Type type) { |
||||
switch (type) { |
||||
case FieldDescriptor::TYPE_BOOL: |
||||
return RustFieldType::BOOL; |
||||
case FieldDescriptor::TYPE_INT32: |
||||
case FieldDescriptor::TYPE_SINT32: |
||||
case FieldDescriptor::TYPE_SFIXED32: |
||||
return RustFieldType::INT32; |
||||
case FieldDescriptor::TYPE_INT64: |
||||
case FieldDescriptor::TYPE_SINT64: |
||||
case FieldDescriptor::TYPE_SFIXED64: |
||||
return RustFieldType::INT64; |
||||
case FieldDescriptor::TYPE_FIXED32: |
||||
case FieldDescriptor::TYPE_UINT32: |
||||
return RustFieldType::UINT32; |
||||
case FieldDescriptor::TYPE_FIXED64: |
||||
case FieldDescriptor::TYPE_UINT64: |
||||
return RustFieldType::UINT64; |
||||
case FieldDescriptor::TYPE_FLOAT: |
||||
return RustFieldType::FLOAT; |
||||
case FieldDescriptor::TYPE_DOUBLE: |
||||
return RustFieldType::DOUBLE; |
||||
case FieldDescriptor::TYPE_BYTES: |
||||
return RustFieldType::BYTES; |
||||
case FieldDescriptor::TYPE_STRING: |
||||
return RustFieldType::STRING; |
||||
case FieldDescriptor::TYPE_MESSAGE: |
||||
case FieldDescriptor::TYPE_GROUP: |
||||
return RustFieldType::MESSAGE; |
||||
case FieldDescriptor::TYPE_ENUM: |
||||
return RustFieldType::ENUM; |
||||
} |
||||
ABSL_LOG(FATAL) << "Unknown field type: " << type; |
||||
} |
||||
|
||||
} // namespace rust
|
||||
} // namespace compiler
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
@ -0,0 +1,39 @@ |
||||
#ifndef GOOGLE_PROTOBUF_COMPILER_RUST_RUST_FIELD_TYPE_H__ |
||||
#define GOOGLE_PROTOBUF_COMPILER_RUST_RUST_FIELD_TYPE_H__ |
||||
|
||||
#include "google/protobuf/descriptor.h" |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
namespace compiler { |
||||
namespace rust { |
||||
|
||||
// An enum of all of the singular types as they should be seen by Rust. This
|
||||
// is parallel to FieldDescriptor::CppType with the main difference being that
|
||||
// that String and Bytes are treated as different types.
|
||||
enum class RustFieldType { |
||||
INT32, |
||||
INT64, |
||||
UINT32, |
||||
UINT64, |
||||
DOUBLE, |
||||
FLOAT, |
||||
BOOL, |
||||
ENUM, |
||||
STRING, |
||||
BYTES, |
||||
MESSAGE, |
||||
}; |
||||
|
||||
// Note: for 'repeated X field' this returns the corresponding type of X.
|
||||
// For map fields this returns MESSAGE.
|
||||
RustFieldType GetRustFieldType(const FieldDescriptor& field); |
||||
|
||||
RustFieldType GetRustFieldType(FieldDescriptor::Type type); |
||||
|
||||
} // namespace rust
|
||||
} // namespace compiler
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
|
||||
#endif // GOOGLE_PROTOBUF_COMPILER_RUST_RUST_FIELD_TYPE_H__
|
Loading…
Reference in new issue