[rust] Fix `gn desc` for rust targets

Fix DescBuilder to respect the `what` argument when extracting the
values for `crate_root` and `crate_name` of rust targets.

Rename RustTargetGenerator to RustValuesGenerator since it is used
to fill the RustValues structure in the Target instances and the
source file are named rust_values_generator.{h,cc}.

Bug: none
Change-Id: I2e7685550173065fddfb43b4527da6e631eb79a0
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/9140
Reviewed-by: Brett Wilson <brettw@chromium.org>
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
diff --git a/src/gn/binary_target_generator.cc b/src/gn/binary_target_generator.cc
index 7322adb..9811a73 100644
--- a/src/gn/binary_target_generator.cc
+++ b/src/gn/binary_target_generator.cc
@@ -67,7 +67,7 @@
     return;
 
   if (target_->source_types_used().RustSourceUsed()) {
-    RustTargetGenerator rustgen(target_, scope_, function_call_, err_);
+    RustValuesGenerator rustgen(target_, scope_, function_call_, err_);
     rustgen.Run();
     if (err_->has_error())
       return;
diff --git a/src/gn/command_desc.cc b/src/gn/command_desc.cc
index 278d45d..cdb2e74 100644
--- a/src/gn/command_desc.cc
+++ b/src/gn/command_desc.cc
@@ -21,6 +21,7 @@
 #include "gn/switches.h"
 #include "gn/target.h"
 #include "gn/variables.h"
+#include "gn/rust_variables.h"
 
 namespace commands {
 
@@ -301,6 +302,8 @@
           {variables::kWalkKeys, DefaultHandler},
           {variables::kWeakFrameworks, DefaultHandler},
           {variables::kWriteOutputConversion, DefaultHandler},
+          {variables::kRustCrateName, DefaultHandler},
+          {variables::kRustCrateRoot, DefaultHandler},
           {"runtime_deps", DefaultHandler}};
 }
 
@@ -351,6 +354,8 @@
   // Entries with DefaultHandler are present to enforce order
   HandleProperty("type", handler_map, v, dict);
   HandleProperty("toolchain", handler_map, v, dict);
+  HandleProperty(variables::kRustCrateName, handler_map, v, dict);
+  HandleProperty(variables::kRustCrateRoot, handler_map, v, dict);
   HandleProperty(variables::kVisibility, handler_map, v, dict);
   HandleProperty(variables::kMetadata, handler_map, v, dict);
   HandleProperty(variables::kTestonly, handler_map, v, dict);
diff --git a/src/gn/desc_builder.cc b/src/gn/desc_builder.cc
index 9590975..5006723 100644
--- a/src/gn/desc_builder.cc
+++ b/src/gn/desc_builder.cc
@@ -15,6 +15,7 @@
 #include "gn/input_file.h"
 #include "gn/parse_tree.h"
 #include "gn/runtime_deps.h"
+#include "gn/rust_variables.h"
 #include "gn/scope.h"
 #include "gn/settings.h"
 #include "gn/standard_out.h"
@@ -326,10 +327,15 @@
     }
 
     if (target_->source_types_used().RustSourceUsed()) {
-      res->SetWithoutPathExpansion(
-          "crate_root", RenderValue(target_->rust_values().crate_root()));
-      res->SetKey("crate_name",
-                  base::Value(target_->rust_values().crate_name()));
+      if (what(variables::kRustCrateRoot)) {
+        res->SetWithoutPathExpansion(
+            variables::kRustCrateRoot,
+            RenderValue(target_->rust_values().crate_root()));
+      }
+      if (what(variables::kRustCrateName)) {
+        res->SetKey(variables::kRustCrateName,
+                    base::Value(target_->rust_values().crate_name()));
+      }
     }
 
     // General target meta variables.
diff --git a/src/gn/rust_values_generator.cc b/src/gn/rust_values_generator.cc
index 926ab21..e71aff6 100644
--- a/src/gn/rust_values_generator.cc
+++ b/src/gn/rust_values_generator.cc
@@ -18,7 +18,7 @@
     "\"crate_type\" must be one of \"bin\", \"cdylib\", \"dylib\", or "
     "\"proc-macro\", \"rlib\", \"staticlib\".";
 
-RustTargetGenerator::RustTargetGenerator(Target* target,
+RustValuesGenerator::RustValuesGenerator(Target* target,
                                          Scope* scope,
                                          const FunctionCallNode* function_call,
                                          Err* err)
@@ -27,9 +27,9 @@
       function_call_(function_call),
       err_(err) {}
 
-RustTargetGenerator::~RustTargetGenerator() = default;
+RustValuesGenerator::~RustValuesGenerator() = default;
 
-void RustTargetGenerator::Run() {
+void RustValuesGenerator::Run() {
   // source_set targets don't need any special Rust handling.
   if (target_->output_type() == Target::SOURCE_SET)
     return;
@@ -65,7 +65,7 @@
     return;
 }
 
-bool RustTargetGenerator::FillCrateName() {
+bool RustValuesGenerator::FillCrateName() {
   const Value* value = scope_->GetValue(variables::kRustCrateName, true);
   if (!value) {
     // The target name will be used.
@@ -79,7 +79,7 @@
   return true;
 }
 
-bool RustTargetGenerator::FillCrateType() {
+bool RustValuesGenerator::FillCrateType() {
   const Value* value = scope_->GetValue(variables::kRustCrateType, true);
   if (!value) {
     // Require shared_library and loadable_module targets to tell us what
@@ -129,7 +129,7 @@
   return false;
 }
 
-bool RustTargetGenerator::FillCrateRoot() {
+bool RustValuesGenerator::FillCrateRoot() {
   const Value* value = scope_->GetValue(variables::kRustCrateRoot, true);
   if (!value) {
     // If there's only one source, use that.
@@ -163,7 +163,7 @@
   return true;
 }
 
-bool RustTargetGenerator::FillAliasedDeps() {
+bool RustValuesGenerator::FillAliasedDeps() {
   const Value* value = scope_->GetValue(variables::kRustAliasedDeps, true);
   if (!value)
     return true;
diff --git a/src/gn/rust_values_generator.h b/src/gn/rust_values_generator.h
index 0804b8b..d902978 100644
--- a/src/gn/rust_values_generator.h
+++ b/src/gn/rust_values_generator.h
@@ -11,13 +11,13 @@
 class FunctionCallNode;
 
 // Collects and writes specified data.
-class RustTargetGenerator {
+class RustValuesGenerator {
  public:
-  RustTargetGenerator(Target* target,
+  RustValuesGenerator(Target* target,
                       Scope* scope,
                       const FunctionCallNode* function_call,
                       Err* err);
-  ~RustTargetGenerator();
+  ~RustValuesGenerator();
 
   void Run();
 
@@ -33,7 +33,7 @@
   const FunctionCallNode* function_call_;
   Err* err_;
 
-  DISALLOW_COPY_AND_ASSIGN(RustTargetGenerator);
+  DISALLOW_COPY_AND_ASSIGN(RustValuesGenerator);
 };
 
 #endif  // TOOLS_GN_RUST_VALUES_GENERATOR_H_