OSDN Git Service

rusty-packets: generate a file that's ready for content
authorZach Johnson <zachoverflow@google.com>
Fri, 20 Nov 2020 18:26:01 +0000 (10:26 -0800)
committerZach Johnson <zachoverflow@google.com>
Fri, 20 Nov 2020 19:41:41 +0000 (11:41 -0800)
Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: Ia3d9e7e3d0b8f388204fd22d6ce4df2760af60de

gd/packet/parser/gen_rust.cc

index 0ae4d5c..f39b061 100644 (file)
  */
 
 #include <filesystem>
+#include <fstream>
+#include <iostream>
 #include "declarations.h"
 
 bool generate_rust_source_one_file(
     __attribute__((unused)) const Declarations& decls,
-    __attribute__((unused)) const std::filesystem::path& input_file,
-    __attribute__((unused)) const std::filesystem::path& include_dir,
-    __attribute__((unused)) const std::filesystem::path& out_dir,
+    const std::filesystem::path& input_file,
+    const std::filesystem::path& include_dir,
+    const std::filesystem::path& out_dir,
     __attribute__((unused)) const std::string& root_namespace) {
-  // TODO do fun things
+  auto gen_relative_path = input_file.lexically_relative(include_dir).parent_path();
+
+  auto input_filename = input_file.filename().string().substr(0, input_file.filename().string().find(".pdl"));
+  auto gen_path = out_dir / gen_relative_path;
+
+  std::filesystem::create_directories(gen_path);
+
+  auto gen_file = gen_path / (input_filename + ".rs");
+
+  std::cout << "generating " << gen_file << std::endl;
+
+  std::ofstream out_file;
+  out_file.open(gen_file);
+  if (!out_file.is_open()) {
+    std::cerr << "can't open " << gen_file << std::endl;
+    return false;
+  }
+
+  out_file << "// @generated rust packets from " << input_file.filename().string();
+
+  out_file.close();
   return true;
 }