inspect_format/
block_type.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
5//! Definitions for all the block types.
6
7use num_derive::{FromPrimitive, ToPrimitive};
8use std::fmt;
9
10/// The type of a block.
11#[derive(Debug, Clone, Copy, Eq, Ord, PartialEq, PartialOrd, FromPrimitive, ToPrimitive)]
12#[repr(u8)]
13pub enum BlockType {
14    /// Contains index of the next free block of the same order.
15    Free = 0,
16
17    /// Available to be changed to a different class. Transitonal.
18    Reserved = 1,
19
20    /// One header at the beginning of the VMO region. Index 0.
21    Header = 2,
22
23    /// An entry in the tree, which might hold nodes, metrics or properties.
24    /// Contains a reference count.
25    NodeValue = 3,
26
27    /// Numeric properties.
28    IntValue = 4,
29    UintValue = 5,
30    DoubleValue = 6,
31
32    /// String or bytevector property value.
33    BufferValue = 7,
34
35    /// Contains a string payload.
36    Extent = 8,
37
38    /// Gives blocks a human-readable identifier.
39    Name = 9,
40
41    /// A deleted object
42    Tombstone = 10,
43
44    /// An array value
45    ArrayValue = 11,
46
47    /// A link value
48    LinkValue = 12,
49
50    /// A boolean value
51    BoolValue = 13,
52
53    /// A string reference value.
54    StringReference = 14,
55}
56
57impl fmt::Display for BlockType {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        match *self {
60            BlockType::Free => write!(f, "FREE"),
61            BlockType::Reserved => write!(f, "RESERVED"),
62            BlockType::Header => write!(f, "HEADER"),
63            BlockType::NodeValue => write!(f, "NODE_VALUE"),
64            BlockType::IntValue => write!(f, "INT_VALUE"),
65            BlockType::UintValue => write!(f, "UINT_VALUE"),
66            BlockType::DoubleValue => write!(f, "DOUBLE_VALUE"),
67            BlockType::BufferValue => write!(f, "BUFFER_VALUE"),
68            BlockType::Extent => write!(f, "EXTENT"),
69            BlockType::Name => write!(f, "NAME"),
70            BlockType::Tombstone => write!(f, "TOMBSTONE"),
71            BlockType::ArrayValue => write!(f, "ARRAY_VALUE"),
72            BlockType::LinkValue => write!(f, "LINK_VALUE"),
73            BlockType::BoolValue => write!(f, "BOOL_VALUE"),
74            BlockType::StringReference => write!(f, "STRING_REFERENCE"),
75        }
76    }
77}
78
79impl BlockType {
80    /// Returns whether the type is for a `*_VALUE` block or not.
81    pub fn is_any_value(&self) -> bool {
82        matches!(
83            *self,
84            BlockType::NodeValue
85                | BlockType::IntValue
86                | BlockType::UintValue
87                | BlockType::DoubleValue
88                | BlockType::BufferValue
89                | BlockType::ArrayValue
90                | BlockType::LinkValue
91                | BlockType::BoolValue
92        )
93    }
94
95    /// Returns whether the type is of a numeric `*_VALUE` block or not.
96    pub fn is_numeric_value(&self) -> bool {
97        matches!(*self, BlockType::IntValue | BlockType::UintValue | BlockType::DoubleValue)
98    }
99
100    pub fn is_valid_for_array(&self) -> bool {
101        matches!(*self, BlockType::StringReference) || self.is_numeric_value()
102    }
103
104    /// Returns an array of all the types.
105    #[cfg(test)]
106    pub fn all() -> [BlockType; 15] {
107        [
108            BlockType::Free,
109            BlockType::Reserved,
110            BlockType::Header,
111            BlockType::NodeValue,
112            BlockType::IntValue,
113            BlockType::UintValue,
114            BlockType::DoubleValue,
115            BlockType::BufferValue,
116            BlockType::Extent,
117            BlockType::Name,
118            BlockType::Tombstone,
119            BlockType::ArrayValue,
120            BlockType::LinkValue,
121            BlockType::BoolValue,
122            BlockType::StringReference,
123        ]
124    }
125}