1//! Fixed size parameters list with optional subparameters.
2
3use core::fmt::{self, Debug, Formatter};
4
5pub(crate) const MAX_PARAMS: usize = 32;
6
7#[derive(Default, Clone, PartialEq, Eq)]
8pub struct Params {
9 /// Number of subparameters for each parameter.
10 ///
11 /// For each entry in the `params` slice, this stores the length of the param as number of
12 /// subparams at the same index as the param in the `params` slice.
13 ///
14 /// At the subparam positions the length will always be `0`.
15 subparams: [u8; MAX_PARAMS],
16
17 /// All parameters and subparameters.
18 params: [u16; MAX_PARAMS],
19
20 /// Number of suparameters in the current parameter.
21 current_subparams: u8,
22
23 /// Total number of parameters and subparameters.
24 len: usize,
25}
26
27impl Params {
28 /// Returns the number of parameters.
29 #[inline]
30 pub fn len(&self) -> usize {
31 self.len
32 }
33
34 /// Returns `true` if there are no parameters present.
35 #[inline]
36 pub fn is_empty(&self) -> bool {
37 self.len == 0
38 }
39
40 /// Returns an iterator over all parameters and subparameters.
41 #[inline]
42 pub fn iter(&self) -> ParamsIter<'_> {
43 ParamsIter::new(self)
44 }
45
46 /// Returns `true` if there is no more space for additional parameters.
47 #[inline]
48 pub(crate) fn is_full(&self) -> bool {
49 self.len == MAX_PARAMS
50 }
51
52 /// Clear all parameters.
53 #[inline]
54 pub(crate) fn clear(&mut self) {
55 self.current_subparams = 0;
56 self.len = 0;
57 }
58
59 /// Add an additional parameter.
60 #[inline]
61 pub(crate) fn push(&mut self, item: u16) {
62 self.subparams[self.len - self.current_subparams as usize] = self.current_subparams + 1;
63 self.params[self.len] = item;
64 self.current_subparams = 0;
65 self.len += 1;
66 }
67
68 /// Add an additional subparameter to the current parameter.
69 #[inline]
70 pub(crate) fn extend(&mut self, item: u16) {
71 self.subparams[self.len - self.current_subparams as usize] = self.current_subparams + 1;
72 self.params[self.len] = item;
73 self.current_subparams += 1;
74 self.len += 1;
75 }
76}
77
78impl<'a> IntoIterator for &'a Params {
79 type IntoIter = ParamsIter<'a>;
80 type Item = &'a [u16];
81
82 fn into_iter(self) -> Self::IntoIter {
83 self.iter()
84 }
85}
86
87/// Immutable subparameter iterator.
88pub struct ParamsIter<'a> {
89 params: &'a Params,
90 index: usize,
91}
92
93impl<'a> ParamsIter<'a> {
94 fn new(params: &'a Params) -> Self {
95 Self { params, index: 0 }
96 }
97}
98
99impl<'a> Iterator for ParamsIter<'a> {
100 type Item = &'a [u16];
101
102 fn next(&mut self) -> Option<Self::Item> {
103 if self.index >= self.params.len() {
104 return None;
105 }
106
107 // Get all subparameters for the current parameter.
108 let num_subparams: u8 = self.params.subparams[self.index];
109 let param: &[u16] = &self.params.params[self.index..self.index + num_subparams as usize];
110
111 // Jump to the next parameter.
112 self.index += num_subparams as usize;
113
114 Some(param)
115 }
116
117 fn size_hint(&self) -> (usize, Option<usize>) {
118 let remaining: usize = self.params.len() - self.index;
119 (remaining, Some(remaining))
120 }
121}
122
123impl Debug for Params {
124 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
125 write!(f, "[")?;
126
127 for (i: usize, param: &[u16]) in self.iter().enumerate() {
128 if i != 0 {
129 write!(f, ";")?;
130 }
131
132 for (i: usize, subparam: &u16) in param.iter().enumerate() {
133 if i != 0 {
134 write!(f, ":")?;
135 }
136
137 subparam.fmt(f)?;
138 }
139 }
140
141 write!(f, "]")
142 }
143}
144