Skip to content

Commit

Permalink
Refactor: Update PatternKey enum to use named fields (#5133)
Browse files Browse the repository at this point in the history
  • Loading branch information
younies authored Jun 27, 2024
1 parent d72a1c3 commit e298617
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions components/experimental/src/dimension/provider/pattern_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ pub enum PowerValue {
pub enum PatternKey {
Binary(u8),
Decimal(i8),
Power(PowerValue, CompoundCount),
Power {
power: PowerValue,
count: CompoundCount,
},
}

/// [`PatternKeyULE`] is a type optimized for efficient storage and
Expand Down Expand Up @@ -122,7 +125,7 @@ impl AsULE for PatternKey {
debug_assert!(value > -32 && value < 32);
(0b01 << 6) | sign | (value as u8 & 0b0001_1111)
}
PatternKey::Power(power, count) => {
PatternKey::Power { power, count } => {
let power_bits = {
match power {
PowerValue::Two => 0b10 << 4,
Expand Down Expand Up @@ -157,7 +160,10 @@ impl AsULE for PatternKey {
_ => unreachable!(),
};
let count = value & 0b0000_1111;
PatternKey::Power(power, count.into())
PatternKey::Power {
power,
count: count.into(),
}
}
_ => unreachable!(),
}
Expand Down Expand Up @@ -185,12 +191,18 @@ fn test_pattern_key_ule() {
PatternKeyULE::validate_byte_slice(&[decimal_ule.0]).unwrap();
assert_eq!(decimal_ule.0, 0b0100_1111);

let power2 = PatternKey::Power(Two, CompoundCount::Two);
let power2 = PatternKey::Power {
power: Two,
count: CompoundCount::Two,
};
let power2_ule = power2.to_unaligned();
PatternKeyULE::validate_byte_slice(&[power2_ule.0]).unwrap();
assert_eq!(power2_ule.0, 0b1010_0010);

let power3 = PatternKey::Power(Three, CompoundCount::Two);
let power3 = PatternKey::Power {
power: Three,
count: CompoundCount::Two,
};
let power3_ule = power3.to_unaligned();
PatternKeyULE::validate_byte_slice(&[power3_ule.0]).unwrap();
assert_eq!(power3_ule.0, 0b1011_0010);
Expand All @@ -202,12 +214,23 @@ fn test_pattern_key_ule() {
assert_eq!(decimal, PatternKey::Decimal(0b0000_1111));

let power2 = PatternKey::from_unaligned(power2_ule);
assert_eq!(power2, PatternKey::Power(Two, CompoundCount::Two));
assert_eq!(
power2,
PatternKey::Power {
power: Two,
count: CompoundCount::Two,
}
);

let power3 = PatternKey::from_unaligned(power3_ule);
assert_eq!(power3, PatternKey::Power(Three, CompoundCount::Two));

// Test unvalidated bytes
assert_eq!(
power3,
PatternKey::Power {
power: Three,
count: CompoundCount::Two,
}
);
// Test invalid bytes
let unvalidated_bytes = [0b1100_0000];
assert_eq!(
PatternKeyULE::validate_byte_slice(&unvalidated_bytes),
Expand Down

0 comments on commit e298617

Please sign in to comment.