1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
pub trait MetricLabel {
    fn label_names() -> &'static [&'static str];
    fn emit_text_value(&self, target: &mut String, value: &str);
    fn emit_json_value(&self, target: &mut String, value: &str);
}

/// `macro_rules!` implementation of `count_tts`.
/// Source: <https://github.com/camsteffen/count-tts>
#[macro_export]
macro_rules! count_tts {
    () => (0);
    ($one:tt) => (1);
    ($($a:tt $b:tt)+) => ($crate::count_tts!($($a)+) << 1);
    ($odd:tt $($a:tt $b:tt)+) => ($crate::count_tts!($($a)+) << 1 | 1);
}

/// Used to declare a label key struct suitable for use in registering
/// counters in the counter registry.
///
/// Usage looks like:
///
/// ```ignore
/// label_key! {
///    pub struct LabelKey {
///       pub label1: String,
///    }
/// }
/// ```
///
/// Always include the trailing comma after each struct field!
///
/// The macro will also generate `BorrowedLabelKey` and `LabelKeyTrait`
/// types.  The `LabelKeyTrait` is implemented for both `LabelKey` and
/// `BorrowedLabelKey` and provides a `key()` method that will return a
/// `BorrowedLabelKey` for either.
///
/// The `BorrowedLabelKey` offers a `to_owned()` method to return a
/// `LabelKey`, and a `label_pairs()` method to return a fixed size
/// array representation consisting of the key and value pairs:
///
/// ```ignore
/// assert_eq!(BorrowedLabelKey { label1: "hello"}.label_pairs(),
///   [("label1", "hello")]
/// )
/// ```
#[macro_export]
macro_rules! label_key {
    (pub struct $name:ident {
        $(
            pub $fieldname:ident: String,
        )*
    }
    ) => {
        $crate::paste::paste!{
            #[derive(Clone, Hash, Eq, PartialEq)]
            pub struct $name {
                $(
                    pub $fieldname: String,
                )*
            }

            pub trait [<$name Trait>] {
                fn key<'k>(&'k self) -> [<Borrowed $name>]<'k>;
            }

            impl [<$name Trait>] for $name {
                fn key<'k>(&'k self) -> [<Borrowed $name>]<'k> {
                    [<Borrowed $name>] {
                        $(
                            $fieldname: self.$fieldname.as_str(),
                        )*
                    }
                }
            }

            // <https://github.com/sunshowers-code/borrow-complex-key-example/blob/main/src/lib.rs>
            // has a detailed explanation of this stuff.
            #[derive(Clone, Copy, Hash, Eq, PartialEq)]
            pub struct [<Borrowed $name>]<'a> {
                $(
                    pub $fieldname: &'a str,
                )*
            }

            impl<'a> [<Borrowed $name>] <'a> {
                #[allow(unused)]
                pub fn to_owned(&self) -> $name {
                    $name {
                        $(
                            $fieldname: self.$fieldname.to_string(),
                        )*
                    }
                }

                #[allow(unused)]
                pub fn label_pairs(&self) -> [(&str,&str); $crate::count_tts!($($fieldname)*)] {
                    [
                        $(
                            (stringify!($fieldname), self.$fieldname),
                        )*
                    ]
                }
            }

            impl<'a> From<&'a [<Borrowed $name>]<'a>> for $name {
                fn from(value: &'a [<Borrowed $name>]<'_>) -> $name {
                    value.to_owned()
                }
            }

            impl<'a> From<&'a dyn [<$name Trait>]> for $name {
                fn from(value: &'a (dyn [<$name Trait>] + 'a)) -> $name {
                    value.key().to_owned()
                }
            }

            impl<'a> [<$name Trait>] for [<Borrowed $name>] <'a> {
                fn key<'k>(&'k self) -> [<Borrowed $name>]<'k> {
                    *self
                }
            }

            impl<'a> ::std::borrow::Borrow<dyn [<$name Trait>] + 'a> for $name {
                fn borrow(&self) -> &(dyn [<$name Trait>] + 'a) {
                    self
                }
            }

            impl<'a> PartialEq for (dyn [<$name Trait>] + 'a) {
                fn eq(&self, other: &Self) -> bool {
                    self.key().eq(&other.key())
                }
            }

            impl<'a> Eq for (dyn [<$name Trait>] + 'a) {}
            impl<'a> ::std::hash::Hash for (dyn [<$name Trait>] + 'a) {
                fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
                    self.key().hash(state)
                }
            }

            impl $crate::labels::MetricLabel for $name {
                fn label_names() -> &'static [&'static str] {
                    const LABEL_NAMES: &'static [&'static str] = &[
                        $(
                            stringify!($fieldname),
                        )*
                    ];

                    LABEL_NAMES
                }

                fn emit_text_value(&self, target: &mut String, value: &str) {
                    let key = self.key();
                    let pairs = key.label_pairs();
                    target.push('{');
                    for (i, (key, value)) in pairs.iter().enumerate() {
                        if i > 0 {
                            target.push_str(", ");
                        }
                        target.push_str(key);
                        target.push_str("=\"");
                        target.push_str(value);
                        target.push_str("\"");
                    }
                    target.push_str("} ");
                    target.push_str(value);
                }

                fn emit_json_value(&self, target: &mut String, value: &str) {
                    let key = self.key();
                    let pairs = key.label_pairs();

                    if pairs.len() == 1 {
                        target.push('"');
                        target.push_str(pairs[0].1);
                        target.push_str("\":");
                        target.push_str(value);
                    } else {
                        target.push_str("{");
                        for (key, value) in pairs.iter() {
                            target.push_str("\"");
                            target.push_str(key);
                            target.push_str("\":\"");
                            target.push_str(value);
                            target.push_str("\",");
                        }

                        target.push_str("\"@\":");
                        target.push_str(value);
                        target.push_str("}");
                    }
                }
            }
        }
    };
}

#[cfg(test)]
mod test {
    #[test]
    fn test_label_macro() {
        label_key! {
            pub struct MyLabel {
                pub myname: String,
            }
        }

        assert_eq!(
            BorrowedMyLabel { myname: "hello" }.label_pairs(),
            [("myname", "hello")]
        );
    }

    #[test]
    fn test_labels_macro() {
        label_key! {
            pub struct MyLabels {
                pub myname: String,
                pub second_name: String,
            }
        }

        assert_eq!(
            BorrowedMyLabels {
                myname: "hello",
                second_name: "there"
            }
            .label_pairs(),
            [("myname", "hello"), ("second_name", "there")]
        );
    }
}