From f362a01e0c826367021169132d03931cc19721d6 Mon Sep 17 00:00:00 2001 From: teesh3rt Date: Mon, 24 Mar 2025 21:09:33 +0200 Subject: [PATCH] feat: dabble with macros a bit more --- src/registry.rs | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/registry.rs b/src/registry.rs index 7db5749..bea03a2 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -1,26 +1,33 @@ -use super::boxcmd::Boxcmd; +use crate::boxcmd::Boxcmd; use boxutils::registry::CommandRegistry; +macro_rules! register { + ($registry:expr, { $($cmd_name:expr => $cmd:expr),* }) => { + $( + $registry.register($cmd_name, Box::new($cmd)); + )* + }; +} + pub fn get_registry() -> CommandRegistry { let mut registry = CommandRegistry::new(); - registry.register("hello", Box::new(coreutils::commands::Hello)); - registry.register("cat", Box::new(coreutils::commands::Cat)); - registry.register("echo", Box::new(coreutils::commands::Echo)); - registry.register("mkdir", Box::new(coreutils::commands::Mkdir)); - registry.register("ash", Box::new(shell::ash::Ash)); - registry.register("dd", Box::new(coreutils::commands::Dd)); - registry.register("nproc", Box::new(coreutils::commands::Nproc)); - registry.register("true", Box::new(coreutils::commands::True)); - registry.register("false", Box::new(coreutils::commands::False)); - registry.register( - "test", - Box::new(coreutils::commands::Test::without_bracket()), - ); - registry.register("[", Box::new(coreutils::commands::Test::with_bracket())); - registry.register("yes", Box::new(coreutils::commands::Yes)); - registry.register("pwd", Box::new(coreutils::commands::Pwd)); - registry.register("box", Box::new(Boxcmd)); + register!(registry, { + "hello" => coreutils::commands::Hello, + "cat" => coreutils::commands::Cat, + "echo" => coreutils::commands::Echo, + "mkdir" => coreutils::commands::Mkdir, + "ash" => shell::ash::Ash, + "dd" => coreutils::commands::Dd, + "nproc" => coreutils::commands::Nproc, + "true" => coreutils::commands::True, + "false" => coreutils::commands::False, + "test" => coreutils::commands::Test::without_bracket(), + "[" => coreutils::commands::Test::with_bracket(), + "yes" => coreutils::commands::Yes, + "pwd" => coreutils::commands::Pwd, + "box" => Boxcmd + }); registry }