1// Copyright 2022 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use fuchsia_inspect::{Node, Property, StringProperty};
6use fuchsia_inspect_derive::Unit;
7// use itertools::Itertools;
8use std::collections::VecDeque;
910/// Wrapper around [std::collections::VecDeque] that only holds [String].
11///
12/// Implements [fuchsia_inspect_derive::Unit], which allows it to be written
13/// to inspect as a single property with its value being a comma-separated list
14/// that's concatenation of all of the items in the VecDeque.
15///
16/// To use this in a a structure that implements [fuchsia_inspect_derive::Inspect], wrap this in the
17/// [fuchsia_inspect_derive::IValue] smart pointer and it will automatically update the value of the
18/// inspect property when updated.
19#[derive(Default)]
20pub struct JoinableInspectVecDeque(pub VecDeque<String>);
2122impl JoinableInspectVecDeque {
23fn join(&self) -> String {
24self.0.iter().map(String::as_str).collect::<Vec<_>>().join(",")
25 }
26}
2728impl Unit for JoinableInspectVecDeque {
29type Data = StringProperty;
3031fn inspect_create(&self, parent: &Node, name: impl AsRef<str>) -> Self::Data {
32 parent.create_string(name.as_ref(), self.join())
33 }
3435fn inspect_update(&self, data: &mut Self::Data) {
36 data.set(&self.join());
37 }
38}
3940#[cfg(test)]
41mod tests {
42use crate::joinable_inspect_vecdeque::JoinableInspectVecDeque;
43use diagnostics_assertions::assert_data_tree;
44use fuchsia_inspect::{Inspector, Node};
45use fuchsia_inspect_derive::{IValue, Inspect, WithInspect};
4647#[derive(Default, Inspect)]
48struct TestInspectWrapper {
49 inspect_node: Node,
50pub test_vec_deque: IValue<JoinableInspectVecDeque>,
51 }
5253// Tests that adding and removing from JoinableInspectVecDeque updates the inspect tree.
54#[fuchsia::test]
55fn test_vec_deque() {
56let inspector = Inspector::default();
5758let mut wrapper = TestInspectWrapper::default()
59 .with_inspect(inspector.root(), "wrapper_node")
60 .expect("Failed to attach wrapper_node");
6162 wrapper.test_vec_deque.as_mut().0.push_back("test1".to_string());
63 wrapper.test_vec_deque.as_mut().0.push_back("test2".to_string());
64 wrapper.test_vec_deque.as_mut().0.push_back("test3".to_string());
6566let _ = wrapper.test_vec_deque.as_mut().0.pop_front();
6768assert_data_tree!(inspector, root: {
69 wrapper_node: {
70"test_vec_deque": "test2,test3",
71 }
72 });
73 }
74}