|
19 | 19 |
|
20 | 20 | use arrow::error::ArrowError;
|
21 | 21 |
|
| 22 | +/// Basic variant type enumeration for the first 2 bits of header |
| 23 | +#[derive(Debug, Clone, PartialEq)] |
| 24 | +pub enum VariantBasicType { |
| 25 | + Primitive = 0, |
| 26 | + ShortString = 1, |
| 27 | + Object = 2, |
| 28 | + Array = 3, |
| 29 | +} |
| 30 | + |
22 | 31 | /// Variant type enumeration covering all possible types
|
23 | 32 | #[derive(Debug, Clone, PartialEq)]
|
24 | 33 | pub enum VariantType {
|
@@ -94,16 +103,13 @@ pub struct VariantParser;
|
94 | 103 | impl VariantParser {
|
95 | 104 | /// General dispatch function to parse any variant header
|
96 | 105 | pub fn parse_variant_header(header_byte: u8) -> Result<VariantType, ArrowError> {
|
97 |
| - let basic_type = header_byte & 0x03; |
| 106 | + let basic_type = Self::get_basic_type(header_byte); |
98 | 107 |
|
99 | 108 | match basic_type {
|
100 |
| - 0 => Ok(VariantType::Primitive(Self::parse_primitive_header(header_byte)?)), |
101 |
| - 1 => Ok(VariantType::ShortString(Self::parse_short_string_header(header_byte)?)), |
102 |
| - 2 => Ok(VariantType::Object(Self::parse_object_header(header_byte)?)), |
103 |
| - 3 => Ok(VariantType::Array(Self::parse_array_header(header_byte)?)), |
104 |
| - _ => Err(ArrowError::InvalidArgumentError( |
105 |
| - format!("Invalid basic type: {}", basic_type) |
106 |
| - )), |
| 109 | + VariantBasicType::Primitive => Ok(VariantType::Primitive(Self::parse_primitive_header(header_byte)?)), |
| 110 | + VariantBasicType::ShortString => Ok(VariantType::ShortString(Self::parse_short_string_header(header_byte)?)), |
| 111 | + VariantBasicType::Object => Ok(VariantType::Object(Self::parse_object_header(header_byte)?)), |
| 112 | + VariantBasicType::Array => Ok(VariantType::Array(Self::parse_array_header(header_byte)?)), |
107 | 113 | }
|
108 | 114 | }
|
109 | 115 |
|
@@ -236,40 +242,46 @@ impl VariantParser {
|
236 | 242 | }
|
237 | 243 |
|
238 | 244 | /// Get the basic type from header byte
|
239 |
| - pub fn get_basic_type(header_byte: u8) -> u8 { |
240 |
| - header_byte & 0x03 |
| 245 | + pub fn get_basic_type(header_byte: u8) -> VariantBasicType { |
| 246 | + match header_byte & 0x03 { |
| 247 | + 0 => VariantBasicType::Primitive, |
| 248 | + 1 => VariantBasicType::ShortString, |
| 249 | + 2 => VariantBasicType::Object, |
| 250 | + 3 => VariantBasicType::Array, |
| 251 | + _ => panic!("Invalid basic type: {}", header_byte & 0x03), |
| 252 | + } |
241 | 253 | }
|
242 | 254 |
|
243 | 255 | /// Check if value bytes represent a primitive
|
244 | 256 | pub fn is_primitive(value_bytes: &[u8]) -> bool {
|
245 | 257 | if value_bytes.is_empty() {
|
246 | 258 | return false;
|
247 | 259 | }
|
248 |
| - Self::get_basic_type(value_bytes[0]) == 0 |
| 260 | + Self::get_basic_type(value_bytes[0]) == VariantBasicType::Primitive |
249 | 261 | }
|
250 | 262 |
|
251 | 263 | /// Check if value bytes represent a short string
|
252 | 264 | pub fn is_short_string(value_bytes: &[u8]) -> bool {
|
253 | 265 | if value_bytes.is_empty() {
|
254 | 266 | return false;
|
255 | 267 | }
|
256 |
| - Self::get_basic_type(value_bytes[0]) == 1 |
| 268 | + Self::get_basic_type(value_bytes[0]) == VariantBasicType::ShortString |
257 | 269 | }
|
258 | 270 |
|
259 | 271 | /// Check if value bytes represent an object
|
260 | 272 | pub fn is_object(value_bytes: &[u8]) -> bool {
|
261 | 273 | if value_bytes.is_empty() {
|
262 | 274 | return false;
|
263 | 275 | }
|
264 |
| - Self::get_basic_type(value_bytes[0]) == 2 |
| 276 | + Self::get_basic_type(value_bytes[0]) == VariantBasicType::Object |
265 | 277 | }
|
266 | 278 |
|
267 | 279 | /// Check if value bytes represent an array
|
268 | 280 | pub fn is_array(value_bytes: &[u8]) -> bool {
|
269 | 281 | if value_bytes.is_empty() {
|
270 | 282 | return false;
|
271 | 283 | }
|
272 |
| - Self::get_basic_type(value_bytes[0]) == 3 |
| 284 | + Self::get_basic_type(value_bytes[0]) == VariantBasicType::Array |
273 | 285 | }
|
274 | 286 |
|
275 | 287 | /// Get the data length for a primitive type
|
|
0 commit comments