inspect_format/
block_type.rsuse num_derive::{FromPrimitive, ToPrimitive};
use std::fmt;
#[derive(Debug, Clone, Copy, Eq, Ord, PartialEq, PartialOrd, FromPrimitive, ToPrimitive)]
#[repr(u8)]
pub enum BlockType {
Free = 0,
Reserved = 1,
Header = 2,
NodeValue = 3,
IntValue = 4,
UintValue = 5,
DoubleValue = 6,
BufferValue = 7,
Extent = 8,
Name = 9,
Tombstone = 10,
ArrayValue = 11,
LinkValue = 12,
BoolValue = 13,
StringReference = 14,
}
impl fmt::Display for BlockType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
BlockType::Free => write!(f, "FREE"),
BlockType::Reserved => write!(f, "RESERVED"),
BlockType::Header => write!(f, "HEADER"),
BlockType::NodeValue => write!(f, "NODE_VALUE"),
BlockType::IntValue => write!(f, "INT_VALUE"),
BlockType::UintValue => write!(f, "UINT_VALUE"),
BlockType::DoubleValue => write!(f, "DOUBLE_VALUE"),
BlockType::BufferValue => write!(f, "BUFFER_VALUE"),
BlockType::Extent => write!(f, "EXTENT"),
BlockType::Name => write!(f, "NAME"),
BlockType::Tombstone => write!(f, "TOMBSTONE"),
BlockType::ArrayValue => write!(f, "ARRAY_VALUE"),
BlockType::LinkValue => write!(f, "LINK_VALUE"),
BlockType::BoolValue => write!(f, "BOOL_VALUE"),
BlockType::StringReference => write!(f, "STRING_REFERENCE"),
}
}
}
impl BlockType {
pub fn is_any_value(&self) -> bool {
matches!(
*self,
BlockType::NodeValue
| BlockType::IntValue
| BlockType::UintValue
| BlockType::DoubleValue
| BlockType::BufferValue
| BlockType::ArrayValue
| BlockType::LinkValue
| BlockType::BoolValue
)
}
pub fn is_numeric_value(&self) -> bool {
matches!(*self, BlockType::IntValue | BlockType::UintValue | BlockType::DoubleValue)
}
pub fn is_valid_for_array(&self) -> bool {
matches!(*self, BlockType::StringReference) || self.is_numeric_value()
}
pub fn array_element_size(&self) -> Option<usize> {
if self.is_numeric_value() {
Some(std::mem::size_of::<u64>())
} else if matches!(self, BlockType::StringReference) {
Some(std::mem::size_of::<u32>())
} else {
None
}
}
#[cfg(test)]
pub fn all() -> [BlockType; 15] {
[
BlockType::Free,
BlockType::Reserved,
BlockType::Header,
BlockType::NodeValue,
BlockType::IntValue,
BlockType::UintValue,
BlockType::DoubleValue,
BlockType::BufferValue,
BlockType::Extent,
BlockType::Name,
BlockType::Tombstone,
BlockType::ArrayValue,
BlockType::LinkValue,
BlockType::BoolValue,
BlockType::StringReference,
]
}
}