-
Notifications
You must be signed in to change notification settings - Fork 34
Allow specifying result labels for enum variants #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
6ce2de0
2f913a6
3352b7c
1a199d8
5cf0228
e3381ce
78f8588
8c8175e
1b3931d
2175ccc
dba8214
3df90d4
4621282
6b1869a
4e67714
b88cff3
fccbe70
f5182a3
2ff0388
a0acdc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,26 +149,6 @@ impl GaugeLabels { | |
| // and this answer explains why it works: | ||
| // https://users.rust-lang.org/t/how-to-check-types-within-macro/33803/8 | ||
|
|
||
| pub trait GetLabelsFromResult { | ||
| fn __autometrics_get_labels(&self) -> Option<ResultAndReturnTypeLabels> { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| impl<T, E> GetLabelsFromResult for Result<T, E> { | ||
| fn __autometrics_get_labels(&self) -> Option<ResultAndReturnTypeLabels> { | ||
| match self { | ||
| Ok(ok) => Some(( | ||
| ok.__autometrics_get_result_label().unwrap_or(OK_KEY), | ||
| ok.__autometrics_static_str(), | ||
| )), | ||
| Err(err) => Some(( | ||
| err.__autometrics_get_result_label().unwrap_or(ERROR_KEY), | ||
| err.__autometrics_static_str(), | ||
| )), | ||
| } | ||
| } | ||
| } | ||
| pub enum LabelArray { | ||
| Three([Label; 3]), | ||
| Four([Label; 4]), | ||
|
|
@@ -188,9 +168,7 @@ impl Deref for LabelArray { | |
| } | ||
|
|
||
| pub trait GetLabels { | ||
| fn __autometrics_get_labels(&self) -> Option<ResultAndReturnTypeLabels> { | ||
| None | ||
| } | ||
| fn __autometrics_get_labels(&self) -> Option<&'static str>; | ||
| } | ||
|
|
||
| /// Implement the given trait for &T and all primitive types. | ||
|
|
@@ -230,8 +208,6 @@ macro_rules! impl_trait_for_types { | |
| }; | ||
| } | ||
|
|
||
| impl_trait_for_types!(GetLabels); | ||
|
|
||
| pub trait GetStaticStrFromIntoStaticStr<'a> { | ||
| fn __autometrics_static_str(&'a self) -> Option<&'static str>; | ||
| } | ||
|
|
@@ -252,12 +228,75 @@ pub trait GetStaticStr { | |
| } | ||
| impl_trait_for_types!(GetStaticStr); | ||
|
|
||
| /// Implementation detail to get enum variants to specify their own | ||
| /// "result" label | ||
| pub trait GetResultLabel { | ||
| /// Return the value to use for the [result](RESULT_KEY) value in the reported metrics | ||
| fn __autometrics_get_result_label(&self) -> Option<&'static str> { | ||
| None | ||
| } | ||
| #[macro_export] | ||
| macro_rules! result_labels { | ||
|
||
| ($e:expr) => {{ | ||
| use $crate::__private::{ | ||
| GetLabels, GetStaticStr, ResultAndReturnTypeLabels, ERROR_KEY, OK_KEY, | ||
| }; | ||
| $crate::__private::spez! { | ||
gagbo marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better if we could call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling the function Generating the |
||
| for val = $e; | ||
|
|
||
| match<T: GetLabels, E: GetLabels> &Result<T, E> -> Option<ResultAndReturnTypeLabels> { | ||
| match val { | ||
| Ok(ok) => Some(( | ||
| ok.__autometrics_get_labels().unwrap_or(OK_KEY), | ||
| ok.__autometrics_static_str(), | ||
| )), | ||
| Err(err) => Some(( | ||
| err.__autometrics_get_labels().unwrap_or(ERROR_KEY), | ||
| err.__autometrics_static_str(), | ||
| )), | ||
| } | ||
| } | ||
|
|
||
| match<T: GetLabels, E> &Result<T, E> -> Option<ResultAndReturnTypeLabels> { | ||
| match val { | ||
| Ok(ok) => Some(( | ||
| ok.__autometrics_get_labels().unwrap_or(OK_KEY), | ||
| ok.__autometrics_static_str(), | ||
| )), | ||
| Err(err) => Some(( | ||
| ERROR_KEY, | ||
| err.__autometrics_static_str(), | ||
| )), | ||
| } | ||
| } | ||
|
|
||
| match<T, E: GetLabels> &Result<T, E> -> Option<ResultAndReturnTypeLabels> { | ||
| match val { | ||
| Ok(ok) => Some(( | ||
| OK_KEY, | ||
| ok.__autometrics_static_str(), | ||
| )), | ||
| Err(err) => Some(( | ||
| err.__autometrics_get_labels().unwrap_or(ERROR_KEY), | ||
| err.__autometrics_static_str(), | ||
| )), | ||
| } | ||
| } | ||
|
|
||
| match<T, E> &Result<T, E> -> Option<ResultAndReturnTypeLabels> { | ||
| match val { | ||
| Ok(ok) => Some(( | ||
| OK_KEY, | ||
| ok.__autometrics_static_str(), | ||
| )), | ||
| Err(err) => Some(( | ||
| ERROR_KEY, | ||
| err.__autometrics_static_str(), | ||
| )), | ||
| } | ||
| } | ||
|
|
||
| match<T: GetLabels> &T -> Option<ResultAndReturnTypeLabels> { | ||
| val.__autometrics_get_labels().map(|label| | ||
| (label, val.__autometrics_static_str())) | ||
| } | ||
|
|
||
| match<T> &T -> Option<ResultAndReturnTypeLabels> { | ||
| None | ||
| } | ||
| } | ||
| }}; | ||
| } | ||
| impl_trait_for_types!(GetResultLabel); | ||
Uh oh!
There was an error while loading. Please reload this page.