#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(i8)]
pub enum Resolution {
Int8 = 0,
Int16 = 1,
Int32 = 2,
Float = 3,
#[default]
Ignore = 4,
}
impl core::fmt::Display for Resolution {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Resolution::Int8 => write!(f, "int8"),
Resolution::Int16 => write!(f, "int16"),
Resolution::Int32 => write!(f, "int32"),
Resolution::Float => write!(f, "float"),
Resolution::Ignore => write!(f, "ignore"),
}
}
}
impl Resolution {
#[inline]
pub const fn size(self) -> usize {
match self {
Resolution::Int8 => 1,
Resolution::Int16 => 2,
Resolution::Int32 => 4,
Resolution::Float => 4,
Resolution::Ignore => 0,
}
}
#[inline]
pub const fn type_code(self) -> u8 {
match self {
Resolution::Int8 => 0x00,
Resolution::Int16 => 0x04,
Resolution::Int32 => 0x08,
Resolution::Float => 0x0c,
Resolution::Ignore => 0x00, }
}
#[inline]
pub const fn from_type_code(code: u8) -> Option<Resolution> {
match code & 0x0c {
0x00 => Some(Resolution::Int8),
0x04 => Some(Resolution::Int16),
0x08 => Some(Resolution::Int32),
0x0c => Some(Resolution::Float),
_ => None,
}
}
#[inline]
pub const fn nan_value(self) -> i32 {
match self {
Resolution::Int8 => -128,
Resolution::Int16 => -32768,
Resolution::Int32 => i32::MIN,
Resolution::Float => 0, Resolution::Ignore => 0,
}
}
#[inline]
pub const fn max_value(self) -> i32 {
match self {
Resolution::Int8 => 127,
Resolution::Int16 => 32767,
Resolution::Int32 => i32::MAX,
Resolution::Float => 0, Resolution::Ignore => 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_resolution_size() {
assert_eq!(Resolution::Int8.size(), 1);
assert_eq!(Resolution::Int16.size(), 2);
assert_eq!(Resolution::Int32.size(), 4);
assert_eq!(Resolution::Float.size(), 4);
assert_eq!(Resolution::Ignore.size(), 0);
}
#[test]
fn test_type_code_roundtrip() {
for res in [
Resolution::Int8,
Resolution::Int16,
Resolution::Int32,
Resolution::Float,
] {
let code = res.type_code();
assert_eq!(Resolution::from_type_code(code), Some(res));
}
}
}