Skip to main content

obz_core/
cmd_path.rs

1//! Standard command identifiers for provider `command_flags` declarations.
2//!
3//! Providers use [`StandardCommand`] variants as keys in their `command_flags`
4//! slice to associate extension flags with specific commands.
5//!
6//! # Example
7//!
8//! ```rust
9//! use obz_core::cmd_path::StandardCommand;
10//!
11//! static MY_FLAGS: &[(StandardCommand, &[obz_core::FlagDescriptor])] = &[
12//!     (StandardCommand::MetricQuery, &[]),
13//!     (StandardCommand::LogSearch, &[]),
14//! ];
15//! ```
16
17// Generate the `StandardCommand` enum and its `signal()`/`subcommand()`
18// methods from a single declaration table.
19//
20// Each entry maps a variant name to its `signal / subcommand` pair.
21// Adding a new command is a one-liner — the enum definition **and** both
22// accessor methods are kept in sync automatically.
23macro_rules! standard_commands {
24    ( $( $(#[$attr:meta])* $variant:ident => $signal:literal / $subcommand:literal ),* $(,)? ) => {
25        /// Identifies a standard (core) command in the obz CLI.
26        ///
27        /// Used as the key in
28        /// [`ProviderMeta::command_flags`](crate::registry::ProviderMeta::command_flags)
29        /// to associate provider-specific flags with specific commands.
30        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
31        pub enum StandardCommand {
32            $( $(#[$attr])* $variant, )*
33        }
34
35        impl StandardCommand {
36            /// The signal group this command belongs to
37            /// (e.g. `"metric"`, `"log"`, `"trace"`).
38            pub const fn signal(self) -> &'static str {
39                match self {
40                    $( Self::$variant => $signal, )*
41                }
42            }
43
44            /// The subcommand name within its signal group
45            /// (e.g. `"query"`, `"search"`).
46            pub const fn subcommand(self) -> &'static str {
47                match self {
48                    $( Self::$variant => $subcommand, )*
49                }
50            }
51        }
52    };
53}
54
55standard_commands! {
56    /// `obz metric query`
57    MetricQuery      => "metric" / "query",
58    /// `obz metric list`
59    MetricList       => "metric" / "list",
60    /// `obz metric info`
61    MetricInfo       => "metric" / "info",
62    /// `obz metric labels`
63    MetricLabels     => "metric" / "labels",
64    /// `obz metric label-values`
65    MetricLabelValues => "metric" / "label-values",
66    /// `obz metric series`
67    MetricSeries     => "metric" / "series",
68    /// `obz log search`
69    LogSearch        => "log"    / "search",
70    /// `obz trace search`
71    TraceSearch      => "trace"  / "search",
72    /// `obz trace get`
73    TraceGet         => "trace"  / "get",
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn signal_and_subcommand_round_trip() {
82        assert_eq!(StandardCommand::MetricQuery.signal(), "metric");
83        assert_eq!(StandardCommand::MetricQuery.subcommand(), "query");
84        assert_eq!(StandardCommand::MetricList.signal(), "metric");
85        assert_eq!(StandardCommand::MetricList.subcommand(), "list");
86        assert_eq!(StandardCommand::MetricInfo.signal(), "metric");
87        assert_eq!(StandardCommand::MetricInfo.subcommand(), "info");
88        assert_eq!(StandardCommand::MetricLabels.signal(), "metric");
89        assert_eq!(StandardCommand::MetricLabels.subcommand(), "labels");
90        assert_eq!(StandardCommand::MetricLabelValues.signal(), "metric");
91        assert_eq!(
92            StandardCommand::MetricLabelValues.subcommand(),
93            "label-values"
94        );
95        assert_eq!(StandardCommand::MetricSeries.signal(), "metric");
96        assert_eq!(StandardCommand::MetricSeries.subcommand(), "series");
97        assert_eq!(StandardCommand::LogSearch.signal(), "log");
98        assert_eq!(StandardCommand::LogSearch.subcommand(), "search");
99        assert_eq!(StandardCommand::TraceSearch.signal(), "trace");
100        assert_eq!(StandardCommand::TraceSearch.subcommand(), "search");
101        assert_eq!(StandardCommand::TraceGet.signal(), "trace");
102        assert_eq!(StandardCommand::TraceGet.subcommand(), "get");
103    }
104}