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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Macro utilities for building a `DiagnosticsHierarchy`.

use crate::{ArrayContent, Property};

/// This macro simplifies creating diagnostics hierarchies, to remove the need of writing multiple
/// nested hierarchies and manually writing all properties.
///
/// Example:
///
/// ```
/// let hierarchy = hierarchy!{
///     root: {
///         some_int_property: 2,
///         some_nested_child: {
///             some_string_property: "foo",
///         }
///     }
/// };
/// ```
///
/// Using names like this will create a `DiagnosticsHierarchy` where `Key` is of type `String`.
///
/// It's possible to use expressions that resolve to some other type. In the following example, we
/// use `Field` variants as the key. The function `bar()` returns a `Field::Bar`.
///
/// ```
/// enum Field {
///     Foo,
///     Bar,
/// }
///
/// let hierarchy = hierarchy!{
///     root: {
///         Field::Foo => "bar",
///         bar() => 2,
///     }
/// };
/// ```
///
/// The only requirement (as in any `DiagnosticsHierarchy`'s `Key`) it must implement `AsRef<str>`.
///
/// It's also possible to use existing variables as keys. For example:
///
/// ```
/// let my_var = "my_key".to_string();
/// let hierarchy = hierarchy!{
///     root: {
///         var my_var: "foo",
///     }
/// };
/// ```
#[macro_export]
macro_rules! hierarchy {
    (@build $hierarchy:expr,) => {};

    // Handle adding a nodes to the hierarchy.
    (@build $hierarchy:expr, var $key:ident: { $($sub:tt)* }) => {{
        #[allow(unused_mut)]
        let mut child = $crate::DiagnosticsHierarchy::new($key, vec![], vec![]);
        $crate::hierarchy!(@build child, $($sub)*);
        $hierarchy.add_child(child);
    }};
    (@build $hierarchy:expr, var $key:ident: { $($sub:tt)* }, $($rest:tt)*) => {{
        $crate::hierarchy!(@build $hierarchy, var $key: { $($sub)* });
        $crate::hierarchy!(@build $hierarchy, $($rest)*);
    }};

    // Handle adding properties to the hierarchy.
    (@build $hierarchy:expr, var $key:ident: $value:expr) => {{
        use $crate::macros::IntoPropertyWithKey;
        let property = $value.into_property_with_key($key);
        $hierarchy.add_property(property);
    }};
    (@build $hierarchy:expr, var $key:ident: $value:expr, $($rest:tt)*) => {{
        $crate::hierarchy!(@build $hierarchy, var $key: $value);
        $crate::hierarchy!(@build $hierarchy, $($rest)*);
    }};

    // Allow a literal as key. It'll be treated as a String key.
    (@build $hierarchy:expr, $key:ident: $($rest:tt)+) => {{
        let key = stringify!($key).to_string();
        $crate::hierarchy!(@build $hierarchy, var key: $($rest)+);
    }};
    // Allow a string literal as key.
    (@build $hierarchy:expr, $key:tt: $($rest:tt)+) => {{
        let key: &'static str = $key;
        let key = $key.to_string();
        $crate::hierarchy!(@build $hierarchy, var key: $($rest)+);
    }};
    // Allow a expression as key, only for properties.
    (@build $hierarchy:expr, $key:expr => $value:expr) => {{
        let key = $key;
        $crate::hierarchy!(@build $hierarchy, var key: $value);
    }};
    (@build $hierarchy:expr, $key:expr => $value:expr, $($rest:tt)*) => {{
        let key = $key;
        $crate::hierarchy!(@build $hierarchy, var key: $value, $($rest)*);
    }};

    // Entry points
    (var $key:ident: { $($rest:tt)* }) => {{
        #[allow(unused_mut)]
        let mut hierarchy = $crate::DiagnosticsHierarchy::new($key, vec![], vec![]);
        $crate::hierarchy!(@build hierarchy, $($rest)*);
        hierarchy
    }};
    ($key:ident: $($rest:tt)+) => {{
        let key = stringify!($key).to_string();
        $crate::hierarchy!(var key: $($rest)+)
    }};
    ($key:tt: $($rest:tt)+) => {{
        let key : &'static str = $key;
        let key = key.to_string();
        $crate::tree_assertion!(var key: $($rest)+)
    }};
}

/// Trait implemented by all types that can be converted to a `Property`.
pub trait IntoPropertyWithKey<Key = String> {
    fn into_property_with_key(self, key: Key) -> Property<Key>;
}

macro_rules! impl_into_property_with_key {
    // Implementation for all `$type`s.
    ($property_name:ident, [$($type:ty),*]) => {
        $(
        impl<Key> IntoPropertyWithKey<Key> for $type {
            fn into_property_with_key(self, key: Key) -> Property<Key> {
                Property::$property_name(key, self.into())
            }
        }
        )*
    };

    // Implementation for all `Vec<$type>`s calling Into for each of them.
    ($property_name:ident, map:[$($type:ty),*]) => {
        $(
        impl<Key> IntoPropertyWithKey<Key> for $type {
            fn into_property_with_key(self, key: Key) -> Property<Key> {
                let value = self.into_iter().map(|value| value.into()).collect::<Vec<_>>();
                Property::$property_name(key, ArrayContent::Values(value))
            }
        }
        )*
    };
}

// TODO(https://fxbug.dev/42155846): support missing data types -> bytes, histograms
impl_into_property_with_key!(String, [String, &str]);
impl_into_property_with_key!(Int, [i64, i32, i16, i8]);
impl_into_property_with_key!(Uint, [u64, u32, u16, u8]);
impl_into_property_with_key!(Double, [f64, f32]);
impl_into_property_with_key!(Bool, [bool]);
impl_into_property_with_key!(DoubleArray, map:[Vec<f64>, Vec<f32>]);
impl_into_property_with_key!(IntArray, map:[Vec<i64>, Vec<i32>, Vec<i16>, Vec<i8>]);
impl_into_property_with_key!(UintArray, map:[Vec<u64>, Vec<u32>, Vec<u16>]);
impl_into_property_with_key!(StringList, [Vec<String>]);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{ArrayFormat, DiagnosticsHierarchy};

    #[fuchsia::test]
    fn test_empty_hierarchy() {
        let expected = DiagnosticsHierarchy::new_root();
        let h: DiagnosticsHierarchy = hierarchy! { root: {} };
        assert_eq!(expected, h);
    }

    #[fuchsia::test]
    fn test_all_types() {
        let string_list = vec!["foo".to_string(), "bar".to_string()];

        let mut expected = DiagnosticsHierarchy::new_root();
        expected.add_property(Property::String("string".to_string(), "some string".to_string()));
        expected.add_property(Property::String("strref".to_string(), "some str ref".to_string()));
        expected.add_property(Property::Int("int8".to_string(), 1i64));
        expected.add_property(Property::Int("int16".to_string(), 2i64));
        expected.add_property(Property::Int("int32".to_string(), 3i64));
        expected.add_property(Property::Int("int64".to_string(), 4i64));
        expected.add_property(Property::Uint("uint16".to_string(), 5u64));
        expected.add_property(Property::Uint("uint32".to_string(), 6u64));
        expected.add_property(Property::Uint("uint64".to_string(), 7u64));
        expected.add_property(Property::Double("float32".to_string(), 8.25f64));
        expected.add_property(Property::Double("float64".to_string(), 9f64));
        expected.add_property(Property::Bool("boolean".to_string(), true));
        expected.add_property(Property::DoubleArray(
            "array_float32".to_string(),
            ArrayContent::new(vec![1f64, 2.5], ArrayFormat::Default).unwrap(),
        ));
        expected.add_property(Property::DoubleArray(
            "array_float64".to_string(),
            ArrayContent::new(vec![3f64, 4.25], ArrayFormat::Default).unwrap(),
        ));
        expected.add_property(Property::IntArray(
            "array_int8".to_string(),
            ArrayContent::new(vec![1i64, 2, 3, 4], ArrayFormat::Default).unwrap(),
        ));
        expected.add_property(Property::IntArray(
            "array_int16".to_string(),
            ArrayContent::new(vec![5i64, 6, 7, 8], ArrayFormat::Default).unwrap(),
        ));
        expected.add_property(Property::IntArray(
            "array_int32".to_string(),
            ArrayContent::new(vec![2i64, 4], ArrayFormat::Default).unwrap(),
        ));
        expected.add_property(Property::IntArray(
            "array_int64".to_string(),
            ArrayContent::new(vec![6i64, 8], ArrayFormat::Default).unwrap(),
        ));
        expected.add_property(Property::UintArray(
            "array_uint16".to_string(),
            ArrayContent::new(vec![0u64, 9], ArrayFormat::Default).unwrap(),
        ));
        expected.add_property(Property::UintArray(
            "array_uint32".to_string(),
            ArrayContent::new(vec![1u64, 3, 5], ArrayFormat::Default).unwrap(),
        ));
        expected.add_property(Property::UintArray(
            "array_uint64".to_string(),
            ArrayContent::new(vec![7u64, 9], ArrayFormat::Default).unwrap(),
        ));
        expected.add_property(Property::StringList("string_list".to_string(), string_list.clone()));

        let result = hierarchy! {
            root: {
                string: "some string".to_string(),
                strref: "some str ref",
                int8:  1i8,
                int16: 2i16,
                int32: 3i32,
                int64: 4i64,
                uint16: 5u16,
                uint32: 6u32,
                uint64: 7u64,
                float32: 8.25f32,
                float64: 9f64,
                boolean: true,
                array_float32: vec![1f32, 2.5],
                array_float64: vec![3f64, 4.25],
                array_int8: vec![1i8,2,3,4],
                array_int16: vec![5i16,6,7,8],
                array_int32: vec![2i32,4],
                array_int64: vec![6i64,8],
                array_uint16: vec![0u16,9],
                array_uint32: vec![1u32,3, 5],
                array_uint64: vec![7u64, 9],
                string_list: string_list.clone(),
            }
        };

        assert_eq!(expected, result);
    }

    #[fuchsia::test]
    fn test_nested_hierarchy() {
        let mut expected = DiagnosticsHierarchy::new_root();
        expected.add_property_at_path(
            &["root", "sub1", "sub11", "sub111"],
            Property::Int("value".to_string(), 1i64),
        );
        expected.add_property_at_path(
            &["root", "sub1", "sub11", "sub111"],
            Property::String("other_value".to_string(), "foo".to_string()),
        );
        expected.add_property_at_path(
            &["root", "sub2", "sub21"],
            Property::Uint("value".to_string(), 2u64),
        );
        let _ = expected.get_or_add_node(&["root", "sub2", "sub22"]);
        let result = hierarchy! {
            root: {
                sub1: {
                    sub11: {
                        sub111: {
                            value: 1i64,
                            other_value: "foo",
                        }
                    }
                },
                sub2: {
                    sub21: {
                        value: 2u64,
                    },
                    sub22: {},
                }
            }
        };
        assert_eq!(expected, result);
    }

    #[fuchsia::test]
    fn test_var_key_syntax() {
        let mut expected = DiagnosticsHierarchy::new("foo", vec![], vec![]);
        expected
            .add_property_at_path(&["foo"], Property::String("bar".to_string(), "baz".to_string()));
        let some_key = "foo".to_string();
        let another_key = "bar".to_string();
        let result = hierarchy! {
            var some_key: {
                var another_key: "baz",
            }
        };
        assert_eq!(expected, result);
    }

    #[derive(Debug, PartialEq)]
    enum Field {
        Foo,
        Bar,
    }

    impl AsRef<str> for Field {
        fn as_ref(&self) -> &str {
            match self {
                Field::Foo => "foo",
                Field::Bar => "bar",
            }
        }
    }

    #[fuchsia::test]
    fn test_custom_key_type_for_properties() {
        let result = hierarchy! {
            root: {
                Field::Bar => 2u64,
                baz: {
                    Field::Foo => "baz",
                }
            }
        };
        assert_eq!(
            result,
            DiagnosticsHierarchy::new(
                "root",
                vec![Property::Uint(Field::Bar, 2u64)],
                vec![DiagnosticsHierarchy::new(
                    "baz",
                    vec![Property::String(Field::Foo, "baz".to_string())],
                    vec![]
                )],
            )
        );
    }
}