metriken_derive/
lib.rs

1// Copyright 2021 Twitter, Inc.
2// Licensed under the Apache License, Version 2.0
3// http://www.apache.org/licenses/LICENSE-2.0
4
5use proc_macro::TokenStream;
6
7mod args;
8mod metric;
9
10/// Declare a global metric that can be accessed via the `metrics` method.
11///
12/// Note that this will change the type of the generated static to be
13/// `MetricInstance<MetricTy>`. It implements both [`Deref`] and [`DerefMut`]
14/// so it can be used much the same as a normal static.
15///
16/// # Parameters
17/// - (optional) `crate`: The path to the `metriken` crate. This allows the
18///   `metric` macro to be used within other macros that get exported to
19///   third-party crates which may not have added `metriken` to their
20///   Cargo.toml.
21/// - (optional) `formatter`: A function to be used to determine the output name
22///   for this metric.
23///
24/// [`Deref`]: std::ops::Deref
25/// [`DerefMut`]: std::ops::DerefMut
26#[proc_macro_attribute]
27pub fn metric(attr: TokenStream, item: TokenStream) -> TokenStream {
28    match metric::metric(attr, item) {
29        Ok(tokens) => tokens.into(),
30        Err(e) => e.to_compile_error().into(),
31    }
32}