From 286cff39249e1b0383967edc283d734187f6e2e9 Mon Sep 17 00:00:00 2001 From: Chad Greenburg Date: Mon, 25 Oct 2021 13:35:53 -0500 Subject: [PATCH] Update to accept any number of label arguments and fixing unit tests. Signed-off-by: Chad Greenburg --- src/macros.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 11581031..90409264 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -141,7 +141,7 @@ fn test_opts_trailing_comma() { /// let opts = histogram_opts!(name, /// help, /// vec![1.0, 2.0], -/// labels!{"key".to_string() => "value".to_string(),}); +/// labels!{"key" => "value",}); /// assert_eq!(opts.common_opts.name, name); /// assert_eq!(opts.common_opts.help, help); /// assert_eq!(opts.buckets.len(), 2); @@ -160,9 +160,10 @@ macro_rules! histogram_opts { hopts.buckets($BUCKETS) }}; - ($NAME:expr, $HELP:expr, $BUCKETS:expr, $CONST_LABELS:expr $(,)?) => {{ + ($NAME:expr, $HELP:expr, $BUCKETS:expr $ ( , $CONST_LABELS:expr ) * $ ( , ) ? ) => {{ + use std::collections::HashMap; + let hopts = histogram_opts!($NAME, $HELP, $BUCKETS); - let lbs = HashMap::::new(); $( let mut lbs = lbs; @@ -193,13 +194,16 @@ fn test_histogram_opts_trailing_comma() { name, help, vec![1.0, 2.0], - labels! {"key".to_string() => "value".to_string(),}, + labels! {"key" => "value",}, + labels! {"key2" => "value2",}, ); assert_eq!(opts.common_opts.name, name); assert_eq!(opts.common_opts.help, help); assert_eq!(opts.buckets.len(), 2); assert!(opts.common_opts.const_labels.get("key").is_some()); assert_eq!(opts.common_opts.const_labels.get("key").unwrap(), "value"); + assert!(opts.common_opts.const_labels.get("key2").is_some()); + assert_eq!(opts.common_opts.const_labels.get("key2").unwrap(), "value2"); } /// Create a [`Counter`] and registers to default registry.