changeset 52558:2d52ae3c5f76

rhg: simplify SubCommand macro a bit more Remove the `name` field from the `SubCommand` record, make it an accessor instead, which makes the record smaller, and the macro smaller.
author Arseniy Alekseyev <aalekseyev@janestreet.com>
date Mon, 30 Dec 2024 12:15:52 +0000
parents b89c934e6269
children 5502109ac769
files rust/rhg/src/main.rs
diffstat 1 files changed, 10 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/rust/rhg/src/main.rs	Fri Dec 13 15:05:37 2024 +0000
+++ b/rust/rhg/src/main.rs	Mon Dec 30 12:15:52 2024 +0000
@@ -543,21 +543,22 @@
 struct SubCommand {
     run: RunFn,
     args: clap::Command,
-    name: String,
-    /// used for reporting collision
+    /// used for reporting name collisions
     origin: String,
 }
 
+impl SubCommand {
+    fn name(&self) -> String {
+        self.args.get_name().to_string()
+    }
+}
+
 macro_rules! subcommand {
     ($command: ident) => {{
-        let args = commands::$command::args();
-        let name = args.get_name().to_string();
-        let origin = stringify!($command).to_string();
         SubCommand {
-            args,
+            args: commands::$command::args(),
             run: commands::$command::run,
-            name,
-            origin,
+            origin: stringify!($command).to_string(),
         }
     }};
 }
@@ -577,7 +578,7 @@
     }
 
     pub fn add(&mut self, subcommand: SubCommand) {
-        let name = subcommand.name;
+        let name = subcommand.name();
         if let Some((origin_old, _)) = self
             .run
             .insert(name.clone(), (subcommand.origin.clone(), subcommand.run))