Skip to main content

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!("{name}:0x{suffix:x}");
17    result
18}
19
20pub struct Table {
21    _rows: Vec<Row>,
22    _node: inspect::Node,
23    _object_name: inspect::StringProperty,
24    _binary_data: inspect::BytesProperty,
25}
26
27impl Table {
28    pub fn new(row_count: usize, col_count: usize, node: inspect::Node) -> Self {
29        let _object_name = node.create_string("object_name", "Example Table");
30        let _binary_data = node.create_bytes("binary_data", vec![0x20, 0x0, 0x11, 0x12, 0x5]);
31        let total = (row_count * col_count) as f64;
32        let mut idx: f64 = 0.0;
33        let mut rows = vec![];
34        for i in 0..row_count {
35            let node_name = unique_name("row");
36            let mut row = Row::new(node.create_child(&node_name));
37            for j in 0..col_count {
38                idx += 1.0;
39                row.add_cell(&format!("({i},{j})"), (i * j) as i64, 100.0 * idx / total);
40            }
41            rows.push(row);
42        }
43        Self { _node: node, _object_name, _binary_data, _rows: rows }
44    }
45}
46
47struct Cell {
48    _node: inspect::Node,
49    _name: inspect::StringProperty,
50    _value: inspect::IntProperty,
51    _double_value: inspect::DoubleProperty,
52}
53
54impl Cell {
55    fn new(name: &str, value: i64, double_value: f64, node: inspect::Node) -> Self {
56        let _name = node.create_string("name", name);
57        let _value = node.create_int("value", value);
58        let _double_value = node.create_double("double_value", double_value);
59        Self { _node: node, _name, _value, _double_value }
60    }
61}
62
63struct Row {
64    node: inspect::Node,
65    cells: Vec<Cell>,
66}
67
68impl Row {
69    fn new(node: inspect::Node) -> Self {
70        Self { node, cells: vec![] }
71    }
72
73    fn add_cell(&mut self, name: &str, value: i64, double_value: f64) {
74        let node_name = unique_name("cell");
75        self.cells.push(Cell::new(name, value, double_value, self.node.create_child(&node_name)));
76    }
77}