1use core::num::NonZeroU8;
2
3use crate::{StorageError, StorageResult};
4
5#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
7pub enum ByteOrder {
8 LittleEndian,
10 BigEndian,
12}
13
14#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
16pub enum BitOrder {
17 MostSignificantFirst,
19 LeastSignificantFirst,
21}
22
23#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
25pub enum SampleClass {
26 UnsignedInteger,
28 SignedInteger,
30 FloatingPoint,
35}
36
37#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
39pub enum StorageUnit {
40 Packed(BitOrder),
42 Byte,
44 Word16(ByteOrder),
46 Word32(ByteOrder),
48 Word64(ByteOrder),
50}
51
52#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
58pub struct SampleStorage {
59 class: SampleClass,
60 significant_bits: NonZeroU8,
61 unit: StorageUnit,
62}
63
64impl SampleStorage {
65 pub fn new(class: SampleClass, significant_bits: u8, unit: StorageUnit) -> StorageResult<Self> {
73 let significant_bits =
74 NonZeroU8::new(significant_bits).ok_or(StorageError::ZeroSampleBits)?;
75 let bits = significant_bits.get();
76 let capacity = unit_capacity(unit, bits)?;
77 if bits > capacity {
78 return Err(StorageError::SampleBitsExceedStorage);
79 }
80 if class == SampleClass::SignedInteger && bits < 2 {
81 return Err(StorageError::SignedSampleNeedsValueBit);
82 }
83 if class == SampleClass::FloatingPoint
84 && (matches!(unit, StorageUnit::Packed(_) | StorageUnit::Byte) || bits != capacity)
85 {
86 return Err(StorageError::InvalidFloatingStorage);
87 }
88 Ok(Self {
89 class,
90 significant_bits,
91 unit,
92 })
93 }
94
95 #[must_use]
97 pub const fn class(self) -> SampleClass {
98 self.class
99 }
100
101 #[must_use]
103 pub const fn significant_bits(self) -> u8 {
104 self.significant_bits.get()
105 }
106
107 #[must_use]
109 pub const fn unit(self) -> StorageUnit {
110 self.unit
111 }
112
113 #[must_use]
115 pub const fn storage_bits(self) -> u8 {
116 match self.unit {
117 StorageUnit::Packed(_) => self.significant_bits.get(),
118 StorageUnit::Byte => 8,
119 StorageUnit::Word16(_) => 16,
120 StorageUnit::Word32(_) => 32,
121 StorageUnit::Word64(_) => 64,
122 }
123 }
124
125 #[must_use]
127 pub const fn byte_order(self) -> Option<ByteOrder> {
128 match self.unit {
129 StorageUnit::Word16(order)
130 | StorageUnit::Word32(order)
131 | StorageUnit::Word64(order) => Some(order),
132 StorageUnit::Packed(_) | StorageUnit::Byte => None,
133 }
134 }
135
136 #[must_use]
138 pub const fn bit_order(self) -> Option<BitOrder> {
139 match self.unit {
140 StorageUnit::Packed(order) => Some(order),
141 StorageUnit::Byte
142 | StorageUnit::Word16(_)
143 | StorageUnit::Word32(_)
144 | StorageUnit::Word64(_) => None,
145 }
146 }
147}
148
149const fn unit_capacity(unit: StorageUnit, packed_bits: u8) -> StorageResult<u8> {
150 match unit {
151 StorageUnit::Packed(_) => {
152 if packed_bits > 64 {
153 Err(StorageError::PackedSampleTooWide)
154 } else {
155 Ok(packed_bits)
156 }
157 }
158 StorageUnit::Byte => Ok(8),
159 StorageUnit::Word16(_) => Ok(16),
160 StorageUnit::Word32(_) => Ok(32),
161 StorageUnit::Word64(_) => Ok(64),
162 }
163}