diff --git a/Cargo.lock b/Cargo.lock index d0cad9a54..5cd53e2b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1470,6 +1470,7 @@ dependencies = [ "dora-tracing", "dunce", "duration-str", + "enum_dispatch", "env_logger", "eyre", "futures", @@ -1956,6 +1957,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" +[[package]] +name = "enum_dispatch" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa18ce2bc66555b3218614519ac839ddb759a7d6720732f979ef8d13be147ecd" +dependencies = [ + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.111", +] + [[package]] name = "enumflags2" version = "0.7.12" diff --git a/binaries/cli/Cargo.toml b/binaries/cli/Cargo.toml index 93819a0cc..3ddafc8c7 100644 --- a/binaries/cli/Cargo.toml +++ b/binaries/cli/Cargo.toml @@ -24,6 +24,7 @@ python = ["pyo3"] arrow = { workspace = true } clap = { version = "4.0.3", features = ["derive"] } clap_complete = "4.5.61" +enum_dispatch = "0.3.13" eyre = "0.6.8" dora-core = { workspace = true, features = ["zenoh"] } dora-message = { workspace = true } diff --git a/binaries/cli/src/command/mod.rs b/binaries/cli/src/command/mod.rs index 4a14f08da..a219087b8 100644 --- a/binaries/cli/src/command/mod.rs +++ b/binaries/cli/src/command/mod.rs @@ -35,10 +35,11 @@ use runtime::Runtime; use self_::SelfSubCommand; use start::Start; use stop::Stop; -use topic::Topic; +use topic::{Echo, Hz, List, Topic}; use up::Up; /// dora-rs cli client +#[enum_dispatch::enum_dispatch(Executable)] #[derive(Debug, clap::Subcommand)] pub enum Command { Check(Check), @@ -67,10 +68,8 @@ pub enum Command { Topic(Topic), Completion(Completion), - Self_ { - #[clap(subcommand)] - command: SelfSubCommand, - }, + #[clap(subcommand)] + Self_(SelfSubCommand), } fn default_tracing() -> eyre::Result<()> { @@ -86,30 +85,7 @@ fn default_tracing() -> eyre::Result<()> { Ok(()) } +#[enum_dispatch::enum_dispatch] pub trait Executable { fn execute(self) -> eyre::Result<()>; } - -impl Executable for Command { - fn execute(self) -> eyre::Result<()> { - match self { - Command::Check(args) => args.execute(), - Command::Coordinator(args) => args.execute(), - Command::Graph(args) => args.execute(), - Command::Build(args) => args.execute(), - Command::New(args) => args.execute(), - Command::Run(args) => args.execute(), - Command::Up(args) => args.execute(), - Command::Destroy(args) => args.execute(), - Command::Start(args) => args.execute(), - Command::Stop(args) => args.execute(), - Command::List(args) => args.execute(), - Command::Logs(args) => args.execute(), - Command::Daemon(args) => args.execute(), - Command::Self_ { command } => command.execute(), - Command::Runtime(args) => args.execute(), - Command::Topic(args) => args.execute(), - Command::Completion(args) => args.execute(), - } - } -} diff --git a/binaries/cli/src/command/topic.rs b/binaries/cli/src/command/topic.rs index 415922ea8..7754fa39d 100644 --- a/binaries/cli/src/command/topic.rs +++ b/binaries/cli/src/command/topic.rs @@ -1,7 +1,4 @@ -use crate::command::{ - Executable, - topic::{echo::Echo, hz::Hz, list::List}, -}; +pub use crate::command::topic::{echo::Echo, hz::Hz, list::List}; mod echo; mod hz; @@ -9,19 +6,10 @@ mod list; mod selector; /// Manage and inspect dataflow topics. +#[enum_dispatch::enum_dispatch(Executable)] #[derive(Debug, clap::Subcommand)] pub enum Topic { List(List), Echo(Echo), Hz(Hz), } - -impl Executable for Topic { - fn execute(self) -> eyre::Result<()> { - match self { - Topic::List(cmd) => cmd.execute(), - Topic::Echo(cmd) => cmd.execute(), - Topic::Hz(cmd) => cmd.execute(), - } - } -} diff --git a/binaries/cli/src/output.rs b/binaries/cli/src/output.rs index b9486653b..b465151af 100644 --- a/binaries/cli/src/output.rs +++ b/binaries/cli/src/output.rs @@ -54,7 +54,7 @@ pub fn print_log_message( let node_id = node_id .to_string() .bold() - .color(word_to_color(&node_id.to_string())); + .color(word_to_color(node_id.as_ref())); let padding = if daemon.is_empty() { "" } else { " " }; format!("{node_id}{padding}{daemon}{colon} ") }