inspect_testing/
table.rs

1// Copyright 2019 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.
4
5use fuchsia_inspect as inspect;
6use std::sync::atomic::{AtomicUsize, Ordering};
7
8static CURRENT_SUFFIX: AtomicUsize = AtomicUsize::new(0);
9
10pub fn reset_unique_names() {
11    CURRENT_SUFFIX.store(0, Ordering::SeqCst);
12}
13
14pub fn unique_name(name: &str) -> String {
15    let suffix = CURRENT_SUFFIX.fetch_add(1, Ordering::SeqCst);
16    let result = format!("{}:0x{:x}", name, suffix);
17    result
18}
19
20#[derive(Clone)]
21pub enum NodeObject {
22    Root(TableData),
23    Table(TableData),
24    Row(RowData),
25    Cell(CellData),
26}
27
28#[derive(Clone)]
29pub struct TableData {
30    pub node_name: String,
31    pub object_name: String,
32    pub binary_data: Vec<u8>,
33    pub rows: Vec<RowData>,
34}
35
36#[derive(Clone)]
37pub struct RowData {
38    pub node_name: String,
39    pub cells: Vec<CellData>,
40}
41
42#[derive(Clone)]
43pub struct CellData {
44    pub node_name: String,
45    pub name: String,
46    pub value: i64,
47    pub double_value: f64,
48}
49
50impl NodeObject {
51    pub fn get_node_name(&self) -> String {
52        match self {
53            NodeObject::Root(_) => "root".to_string(),
54            NodeObject::Table(t) => t.node_name.clone(),
55            NodeObject::Row(r) => r.node_name.clone(),
56            NodeObject::Cell(c) => c.node_name.clone(),
57        }
58    }
59}
60
61pub struct Table {
62    rows: Vec<Row>,
63
64    // For the VMO
65    _node: inspect::Node,
66    _object_name: inspect::StringProperty,
67    _binary_data: inspect::BytesProperty,
68
69    // TODO(https://fxbug.dev/42118093): remove when the FIDL service is deprecated
70    data: TableData,
71}
72
73impl Table {
74    pub fn new(row_count: usize, col_count: usize, node_name: &str, node: inspect::Node) -> Self {
75        let data = TableData {
76            object_name: "Example Table".to_string(),
77            binary_data: vec![0x20, 0x0, 0x11, 0x12, 0x5],
78            node_name: node_name.to_string(),
79            rows: vec![],
80        };
81        let _object_name = node.create_string("object_name", &data.object_name);
82        let _binary_data = node.create_bytes("binary_data", data.binary_data.clone());
83        let total = (row_count * col_count) as f64;
84        let mut idx: f64 = 0.0;
85        let mut rows = vec![];
86        for i in 0..row_count {
87            let node_name = unique_name("row");
88            let mut row = Row::new(&node_name, node.create_child(&node_name));
89            for j in 0..col_count {
90                idx += 1.0;
91                row.add_cell(&format!("({},{})", i, j), (i * j) as i64, 100.0 * idx / total);
92            }
93            rows.push(row);
94        }
95        Self { _node: node, _object_name, _binary_data, rows, data }
96    }
97
98    pub fn get_node_object(&self) -> NodeObject {
99        let mut data = self.data.clone();
100        data.rows = self.rows.iter().map(|row| row.get_data()).collect();
101        NodeObject::Root(data)
102    }
103}
104
105struct Cell {
106    _node: inspect::Node,
107    _name: inspect::StringProperty,
108    _value: inspect::IntProperty,
109    _double_value: inspect::DoubleProperty,
110
111    // TODO(https://fxbug.dev/42118093): remove when the FIDL service is deprecated.
112    data: CellData,
113}
114
115impl Cell {
116    fn new(
117        name: &str,
118        value: i64,
119        double_value: f64,
120        node_name: &str,
121        node: inspect::Node,
122    ) -> Self {
123        let _name = node.create_string("name", name);
124        let _value = node.create_int("value", value);
125        let _double_value = node.create_double("double_value", double_value);
126        let data = CellData {
127            node_name: node_name.to_string(),
128            name: name.to_string(),
129            value,
130            double_value,
131        };
132        Self { data, _node: node, _name, _value, _double_value }
133    }
134}
135
136struct Row {
137    node: inspect::Node,
138    cells: Vec<Cell>,
139
140    // TODO(https://fxbug.dev/42118093): remove when the FIDL service is deprecated.
141    data: RowData,
142}
143
144impl Row {
145    fn new(node_name: &str, node: inspect::Node) -> Self {
146        let data = RowData { node_name: node_name.to_string(), cells: vec![] };
147        Self { data, node, cells: vec![] }
148    }
149
150    fn add_cell(&mut self, name: &str, value: i64, double_value: f64) {
151        let node_name = unique_name("cell");
152        self.cells.push(Cell::new(
153            name,
154            value,
155            double_value,
156            &node_name,
157            self.node.create_child(&node_name),
158        ));
159    }
160
161    fn get_data(&self) -> RowData {
162        let mut data = self.data.clone();
163        data.cells = self.cells.iter().map(|cell| cell.data.clone()).collect();
164        data
165    }
166}