inspect_format/
block_type.rs
1use num_derive::{FromPrimitive, ToPrimitive};
8use std::fmt;
9
10#[derive(Debug, Clone, Copy, Eq, Ord, PartialEq, PartialOrd, FromPrimitive, ToPrimitive)]
12#[repr(u8)]
13pub enum BlockType {
14 Free = 0,
16
17 Reserved = 1,
19
20 Header = 2,
22
23 NodeValue = 3,
26
27 IntValue = 4,
29 UintValue = 5,
30 DoubleValue = 6,
31
32 BufferValue = 7,
34
35 Extent = 8,
37
38 Name = 9,
40
41 Tombstone = 10,
43
44 ArrayValue = 11,
46
47 LinkValue = 12,
49
50 BoolValue = 13,
52
53 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 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 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 #[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}