fuchsia_inspect_contrib/
id_enum.rs

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
// Copyright 2024 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.

//! Provides trait and functions to log mapping from IDs of an enum to its variants
//! or mapped variant values.
//!
//! ## Example
//!
//! ```rust
//! use diagnostics_assertions::assert_data_tree;
//! use fuchsia_inspect::Inspector;
//! use strum_macros::{Display, EnumIter};
//!
//! #[derive(Display, EnumIter)]
//! enum Enum {
//!     A,
//!     B,
//! }
//!
//! impl IdEnum for Enum {
//!     type Id = u8;
//!     fn to_id(&self) -> Self::Id {
//!         match self {
//!             Self::A => 0,
//!             Self::B => 1,
//!         }
//!     }
//! }
//!
//! let inspector = Inspector::default();
//! inspect_record_id_enum::<Enum>(inspector.root(), "enum");
//! assert_data_tree!(inspector, root: {
//!     "enum": {
//!         "0": "A",
//!         "1": "B",
//!     }
//! });
//!
//! inspect_record_id_enum_mapped::<Enum, _>(inspector.root(), "highlight", |variant| {
//!     match variant {
//!         Enum::A => None,
//!         Enum::B => Some(2u8),
//!     }
//! });
//! assert_data_tree!(inspector, root: contains {
//!     "highlight": {
//!         "1": 2u64,
//!     }
//! });
//! ```

use fuchsia_inspect::Node as InspectNode;
use std::fmt::Display;
use strum;

use crate::log::WriteInspect;

pub trait IdEnum {
    type Id: Into<u64>;
    fn to_id(&self) -> Self::Id;
}

/// Record the mapping of `IdEnum`'s ids to variants in Inspect.
pub fn inspect_record_id_enum<R>(node: &InspectNode, name: &str)
where
    R: IdEnum + strum::IntoEnumIterator + Display,
{
    let enum_node = node.create_child(name);
    for variant in R::iter() {
        enum_node.record_string(variant.to_id().into().to_string(), format!("{variant}"));
    }
    node.record(enum_node);
}

/// Record the mapping of `IdEnum`'s ids to mapped variant values.
/// Only variants with mapped `Some` values are recorded.
pub fn inspect_record_id_enum_mapped<R, V>(
    node: &InspectNode,
    name: &str,
    map_fn: impl Fn(&R) -> Option<V>,
) where
    R: IdEnum + strum::IntoEnumIterator,
    V: WriteInspect,
{
    let enum_node = node.create_child(name);
    for variant in R::iter() {
        let value = map_fn(&variant);
        if let Some(value) = value {
            let key = variant.to_id().into().to_string();
            value.write_inspect(&enum_node, key);
        }
    }
    node.record(enum_node);
}

#[cfg(test)]
mod tests {
    use super::*;
    use diagnostics_assertions::assert_data_tree;
    use fuchsia_inspect::Inspector;
    use strum_macros::{Display, EnumIter};

    #[derive(Display, EnumIter)]
    enum Enum {
        A,
        B,
    }

    impl IdEnum for Enum {
        type Id = u8;
        fn to_id(&self) -> Self::Id {
            match self {
                Self::A => 0,
                Self::B => 1,
            }
        }
    }

    #[test]
    fn test_inspect_record_id_enum() {
        let inspector = Inspector::default();
        inspect_record_id_enum::<Enum>(inspector.root(), "enum");
        assert_data_tree!(inspector, root: {
            "enum": {
                "0": "A",
                "1": "B",
            }
        });
    }

    #[test]
    fn test_inspect_record_id_enum_mapped() {
        let inspector = Inspector::default();
        inspect_record_id_enum_mapped::<Enum, _>(inspector.root(), "highlight", |variant| {
            match variant {
                Enum::A => None,
                Enum::B => Some(2u8),
            }
        });
        assert_data_tree!(inspector, root: {
            "highlight": {
                "1": 2u64,
            },
        });
    }
}