Skip to content

Commit

Permalink
enhance: Replace unwrap() with Result to handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
eval-exec committed Mar 7, 2023
1 parent 36acf47 commit 7ea049f
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ fn test_histogram_opts_trailing_comma() {
#[macro_export(local_inner_macros)]
macro_rules! register_counter {
(@of_type $TYPE:ident, $OPTS:expr) => {{
let counter = $crate::$TYPE::with_opts($OPTS).unwrap();
$crate::register(Box::new(counter.clone())).map(|_| counter)
$crate::$TYPE::with_opts($OPTS).and_then(|counter| {
$crate::register(Box::new(counter.clone())).map(|_| counter)
})
}};

($OPTS:expr $(,)?) => {{
Expand Down Expand Up @@ -259,8 +260,9 @@ fn test_register_counter_trailing_comma() {
#[macro_export(local_inner_macros)]
macro_rules! register_counter_with_registry {
(@of_type $TYPE: ident, $OPTS:expr, $REGISTRY:expr) => {{
let counter = $crate::$TYPE::with_opts($OPTS).unwrap();
$REGISTRY.register(Box::new(counter.clone())).map(|_| counter)
$crate::$TYPE::with_opts($OPTS).and_then(|counter| {
$REGISTRY.register(Box::new(counter.clone())).map(|_| counter)
})
}};

($OPTS:expr, $REGISTRY:expr $(,)?) => {{
Expand Down Expand Up @@ -360,15 +362,19 @@ fn test_register_int_counter() {
#[doc(hidden)]
macro_rules! __register_counter_vec {
($TYPE:ident, $OPTS:expr, $LABELS_NAMES:expr) => {{
let counter_vec = $crate::$TYPE::new($OPTS, $LABELS_NAMES).unwrap();
$crate::register(Box::new(counter_vec.clone())).map(|_| counter_vec)
$crate::$TYPE::new($OPTS, $LABELS_NAMES).and_then(|counter_vec| {
$crate::default_registry()
.register(Box::new(counter_vec.clone()))
.map(|_| counter_vec)
})
}};

($TYPE:ident, $OPTS:expr, $LABELS_NAMES:expr, $REGISTRY:expr) => {{
let counter_vec = $crate::$TYPE::new($OPTS, $LABELS_NAMES).unwrap();
$REGISTRY
.register(Box::new(counter_vec.clone()))
.map(|_| counter_vec)
$crate::$TYPE::new($OPTS, $LABELS_NAMES).and_then(|counter_vec| {
$REGISTRY
.register(Box::new(counter_vec.clone()))
.map(|_| counter_vec)
})
}};
}

Expand Down Expand Up @@ -542,13 +548,16 @@ fn test_register_int_counter_vec() {
#[doc(hidden)]
macro_rules! __register_gauge {
($TYPE:ident, $OPTS:expr) => {{
let gauge = $crate::$TYPE::with_opts($OPTS).unwrap();
$crate::register(Box::new(gauge.clone())).map(|_| gauge)
$crate::$TYPE::with_opts($OPTS).and_then(|gauge| {
$crate::default_registry()
.register(Box::new(gauge.clone()))
.map(|_| gauge)
})
}};

($TYPE:ident, $OPTS:expr, $REGISTRY:expr) => {{
let gauge = $crate::$TYPE::with_opts($OPTS).unwrap();
$REGISTRY.register(Box::new(gauge.clone())).map(|_| gauge)
$crate::$TYPE::with_opts($OPTS)
.and_then(|gauge| $REGISTRY.register(Box::new(gauge.clone())).map(|_| gauge))
}};
}

Expand Down Expand Up @@ -669,15 +678,16 @@ macro_rules! register_int_gauge_with_registry {
#[doc(hidden)]
macro_rules! __register_gauge_vec {
($TYPE:ident, $OPTS:expr, $LABELS_NAMES:expr $(,)?) => {{
let gauge_vec = $crate::$TYPE::new($OPTS, $LABELS_NAMES).unwrap();
$crate::register(Box::new(gauge_vec.clone())).map(|_| gauge_vec)
$crate::$TYPE::new($OPTS, $LABELS_NAMES)
.and_then(|gauge_vec| $crate::register(Box::new(gauge_vec.clone())).map(|_| gauge_vec))
}};

($TYPE:ident, $OPTS:expr, $LABELS_NAMES:expr, $REGISTRY:expr $(,)?) => {{
let gauge_vec = $crate::$TYPE::new($OPTS, $LABELS_NAMES).unwrap();
$REGISTRY
.register(Box::new(gauge_vec.clone()))
.map(|_| gauge_vec)
$crate::$TYPE::new($OPTS, $LABELS_NAMES).and_then(|gauge_vec| {
$REGISTRY
.register(Box::new(gauge_vec.clone()))
.map(|_| gauge_vec)
})
}};
}

Expand Down Expand Up @@ -916,8 +926,8 @@ macro_rules! register_histogram {
};

($HOPTS:expr $(,)?) => {{
let histogram = $crate::Histogram::with_opts($HOPTS).unwrap();
$crate::register(Box::new(histogram.clone())).map(|_| histogram)
$crate::Histogram::with_opts($HOPTS)
.and_then(|histogram| $crate::register(Box::new(histogram.clone())).map(|_| histogram))
}};
}

Expand Down Expand Up @@ -971,10 +981,11 @@ macro_rules! register_histogram_with_registry {
};

($HOPTS:expr, $REGISTRY:expr $(,)?) => {{
let histogram = $crate::Histogram::with_opts($HOPTS).unwrap();
$REGISTRY
.register(Box::new(histogram.clone()))
.map(|_| histogram)
$crate::Histogram::with_opts($HOPTS).and_then(|histogram| {
$REGISTRY
.register(Box::new(histogram.clone()))
.map(|_| histogram)
})
}};
}

Expand Down Expand Up @@ -1029,8 +1040,9 @@ fn test_register_histogram_with_registry_trailing_comma() {
#[macro_export(local_inner_macros)]
macro_rules! register_histogram_vec {
($HOPTS:expr, $LABELS_NAMES:expr $(,)?) => {{
let histogram_vec = $crate::HistogramVec::new($HOPTS, $LABELS_NAMES).unwrap();
$crate::register(Box::new(histogram_vec.clone())).map(|_| histogram_vec)
$crate::HistogramVec::new($HOPTS, $LABELS_NAMES).and_then(|histogram_vec| {
$crate::register(Box::new(histogram_vec.clone())).map(|_| histogram_vec)
})
}};

($NAME:expr, $HELP:expr, $LABELS_NAMES:expr $(,)?) => {{
Expand Down Expand Up @@ -1091,10 +1103,11 @@ fn test_register_histogram_vec_trailing_comma() {
#[macro_export(local_inner_macros)]
macro_rules! register_histogram_vec_with_registry {
($HOPTS:expr, $LABELS_NAMES:expr, $REGISTRY:expr $(,)?) => {{
let histogram_vec = $crate::HistogramVec::new($HOPTS, $LABELS_NAMES).unwrap();
$REGISTRY
.register(Box::new(histogram_vec.clone()))
.map(|_| histogram_vec)
$crate::HistogramVec::new($HOPTS, $LABELS_NAMES).and_then(|histogram_vec| {
$REGISTRY
.register(Box::new(histogram_vec.clone()))
.map(|_| histogram_vec)
})
}};

($NAME:expr, $HELP:expr, $LABELS_NAMES:expr, $REGISTRY:expr $(,)?) => {{
Expand Down

0 comments on commit 7ea049f

Please sign in to comment.