Make rust_crate_mapping flag optional

If the flag is not passed, it means the current proto_library has no deps.

PiperOrigin-RevId: 598846330
pull/15449/head
Marcel Hlopko 1 year ago committed by Copybara-Service
parent 88d991d310
commit e4ae0c110f
  1. 7
      src/google/protobuf/compiler/rust/context.cc
  2. 4
      src/google/protobuf/compiler/rust/crate_mapping.cc
  3. 10
      src/google/protobuf/compiler/rust/crate_mapping_unittest.cc

@ -67,12 +67,9 @@ absl::StatusOr<Options> Options::Parse(absl::string_view param) {
auto mapping_arg = absl::c_find_if(
args, [](auto& arg) { return arg.first == "bazel_crate_mapping"; });
if (mapping_arg == args.end()) {
return absl::InvalidArgumentError(
"Mandatory option `mapping` missing, please specify mapping file "
"path.");
if (mapping_arg != args.end()) {
opts.mapping_file_path = mapping_arg->second;
}
opts.mapping_file_path = mapping_arg->second;
return opts;
}

@ -23,8 +23,9 @@ namespace rust {
absl::StatusOr<absl::flat_hash_map<std::string, std::string>>
GetImportPathToCrateNameMap(const Options* opts) {
absl::flat_hash_map<std::string, std::string> mapping;
if (opts->mapping_file_path.empty()) {
return absl::InvalidArgumentError("Mapping file path is not specified");
return mapping;
}
std::string mapping_contents;
absl::Status status =
@ -33,7 +34,6 @@ GetImportPathToCrateNameMap(const Options* opts) {
return status;
}
absl::flat_hash_map<std::string, std::string> mapping;
std::vector<absl::string_view> lines =
absl::StrSplit(mapping_contents, '\n', absl::SkipEmpty());
size_t len = lines.size();

@ -15,6 +15,7 @@
using google::protobuf::compiler::rust::Options;
using testing::Eq;
using testing::IsEmpty;
using testing::UnorderedElementsAreArray;
namespace google {
@ -70,14 +71,11 @@ TEST(RustGenerator, GetImportPathToCrateNameMapSimple) {
EXPECT_THAT(result.value(), UnorderedElementsAreArray(expected));
}
TEST(RustGenerator,
GetImportPathToCrateNameMapErrorsOnNotSpecifiedMappingFile) {
TEST(RustGenerator, GetImportPathToCrateNameMapForEmptyMappingFile) {
const Options opts = {Kernel::kUpb};
auto result = GetImportPathToCrateNameMap(&opts);
EXPECT_FALSE(result.ok());
EXPECT_THAT(result.status().code(), Eq(absl::StatusCode::kInvalidArgument));
EXPECT_THAT(result.status().message(),
Eq("Mapping file path is not specified"));
EXPECT_TRUE(result.ok());
EXPECT_THAT(result.value(), IsEmpty());
}
TEST(RustGenerator, GetImportPathToCrateNameMapErrorsOnMissingFile) {

Loading…
Cancel
Save