Parse blobby data into an array. # Errors If data could not be parsed successfully.
(
mut data: &[u8],
)
| 80 | /// # Errors |
| 81 | /// If data could not be parsed successfully. |
| 82 | pub const fn parse_into_array<const ITEMS_LEN: usize, const DEDUP_LEN: usize>( |
| 83 | mut data: &[u8], |
| 84 | ) -> Result<[&[u8]; ITEMS_LEN], Error> { |
| 85 | match Header::parse(&mut data) { |
| 86 | Ok(header) => { |
| 87 | if header.items_len != ITEMS_LEN || header.dedup_len != DEDUP_LEN { |
| 88 | return Err(Error::BadArrayLen); |
| 89 | } |
| 90 | } |
| 91 | Err(err) => return Err(err), |
| 92 | } |
| 93 | |
| 94 | let mut dedup_index: [&[u8]; DEDUP_LEN] = [&[]; DEDUP_LEN]; |
| 95 | |
| 96 | let mut i = 0; |
| 97 | while i < dedup_index.len() { |
| 98 | let m = try_read_vlq!(data); |
| 99 | let split = data.split_at(m); |
| 100 | dedup_index[i] = split.0; |
| 101 | data = split.1; |
| 102 | i += 1; |
| 103 | } |
| 104 | |
| 105 | let mut res: [&[u8]; ITEMS_LEN] = [&[]; ITEMS_LEN]; |
| 106 | |
| 107 | let mut i = 0; |
| 108 | while i < res.len() { |
| 109 | let val = try_read_vlq!(data); |
| 110 | // the least significant bit is used as a flag |
| 111 | let is_ref = (val & 1) != 0; |
| 112 | let val = val >> 1; |
| 113 | res[i] = if is_ref { |
| 114 | if val >= dedup_index.len() { |
| 115 | return Err(Error::InvalidIndex); |
| 116 | } |
| 117 | dedup_index[val] |
| 118 | } else { |
| 119 | if val > data.len() { |
| 120 | return Err(Error::UnexpectedEnd); |
| 121 | } |
| 122 | let split = data.split_at(val); |
| 123 | data = split.1; |
| 124 | split.0 |
| 125 | }; |
| 126 | i += 1; |
| 127 | } |
| 128 | |
| 129 | if data.is_empty() { |
| 130 | Ok(res) |
| 131 | } else { |
| 132 | Err(Error::BadArrayLen) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /// Parse blobby data into a vector of slices. |
| 137 | /// |