1 #include "google/protobuf/compiler/rust/crate_mapping.h" 2 3 #include <fcntl.h> 4 5 #include <cstddef> 6 #include <cstdio> 7 #include <string> 8 #include <vector> 9 10 #include "google/protobuf/testing/file.h" 11 #include "absl/container/flat_hash_map.h" 12 #include "absl/status/status.h" 13 #include "absl/status/statusor.h" 14 #include "absl/strings/numbers.h" 15 #include "absl/strings/str_cat.h" 16 #include "absl/strings/str_split.h" 17 #include "absl/strings/string_view.h" 18 #include "google/protobuf/compiler/rust/context.h" 19 20 namespace google { 21 namespace protobuf { 22 namespace compiler { 23 namespace rust { 24 25 absl::StatusOr<absl::flat_hash_map<std::string, std::string>> GetImportPathToCrateNameMap(const Options * opts)26GetImportPathToCrateNameMap(const Options* opts) { 27 absl::flat_hash_map<std::string, std::string> mapping; 28 if (opts->mapping_file_path.empty()) { 29 return mapping; 30 } 31 std::string mapping_contents; 32 absl::Status status = 33 File::ReadFileToString(opts->mapping_file_path, &mapping_contents, true); 34 if (!status.ok()) { 35 return status; 36 } 37 38 std::vector<absl::string_view> lines = 39 absl::StrSplit(mapping_contents, '\n', absl::SkipEmpty()); 40 size_t len = lines.size(); 41 42 size_t idx = 0; 43 while (idx < len) { 44 absl::string_view crate_name = lines[idx++]; 45 size_t files_cnt; 46 if (!absl::SimpleAtoi(lines[idx++], &files_cnt)) { 47 return absl::InvalidArgumentError( 48 "Couldn't parse number of import paths in mapping file"); 49 } 50 for (size_t i = 0; i < files_cnt; ++i) { 51 mapping.insert({std::string(lines[idx++]), std::string(crate_name)}); 52 } 53 } 54 return mapping; 55 } 56 57 } // namespace rust 58 } // namespace compiler 59 } // namespace protobuf 60 } // namespace google 61