Macro prometheus_utils::label_enum
source · [−]macro_rules! label_enum {
($(#[$attr:meta])* enum $N:ident { $($(#[$var_attr:meta])* $V:ident),* $(,)* }) => { ... };
($(#[$attr:meta])* pub enum $N:ident { $($(#[$var_attr:meta])* $V:ident),* $(,)* }) => { ... };
($(#[$attr:meta])* pub ($($vis:tt)+) enum $N:ident { $($(#[$var_attr:meta])* $V:ident),* $(,)* }) => { ... };
}
Expand description
Declare an enum intended to be used as a Prometheus label.
This helper macro can only be used to define enums consisting of tags without values.
Each tag corresponds to a possible Prometheus label value. The macro then generates
two functions, as_str
and all_variants
, as described in the example below. Those
funtions are intended to assist in implementing the Labels
trait for a label struct
that contains the enum, ensuring a consistent conversion to strings for label values,
and that all possible variants are included when implementing possible_label_values
.
Example
When using the macro, define exactly one enum
inside, as you would normally:
ⓘ
label_enum! {
pub(crate) enum MyErrorLabel {
IoError,
TimeoutError,
MemoryError,
}
}
The macro will declare the enum exactly as provided. But in addition, it will generate two functions:
ⓘ
impl MyErrorLabel {
/// The name of this enum variant, as a string slice.
pub fn as_str(&self) -> &'static str { ... }
/// A vector containing one instance of each of the enum's variants.
pub fn all_variants() -> Vec<Self> { ... }
}