Skip to main content

rvf_types/
quant_type.rs

1//! Quantization type discriminator for QUANT_SEG payloads.
2
3/// Identifies the quantization method stored in a QUANT_SEG.
4#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[repr(u8)]
7pub enum QuantType {
8    /// Scalar quantization (min-max per dimension).
9    Scalar = 0,
10    /// Product quantization (codebook per subspace).
11    Product = 1,
12    /// Binary threshold quantization (sign bit per dimension).
13    BinaryThreshold = 2,
14    /// Residual product quantization.
15    ResidualPq = 3,
16}
17
18impl TryFrom<u8> for QuantType {
19    type Error = u8;
20
21    fn try_from(value: u8) -> Result<Self, Self::Error> {
22        match value {
23            0 => Ok(Self::Scalar),
24            1 => Ok(Self::Product),
25            2 => Ok(Self::BinaryThreshold),
26            3 => Ok(Self::ResidualPq),
27            other => Err(other),
28        }
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn round_trip() {
38        for raw in 0..=3u8 {
39            let qt = QuantType::try_from(raw).unwrap();
40            assert_eq!(qt as u8, raw);
41        }
42    }
43
44    #[test]
45    fn invalid_value() {
46        assert_eq!(QuantType::try_from(4), Err(4));
47        assert_eq!(QuantType::try_from(255), Err(255));
48    }
49}