use std::collections::HashMap;
use super::{Error, Result};
pub fn from_bytes(input: &[u8]) -> Result<HashMap<String, Vec<u8>>> {
tracing::trace!(input_length = input.len(), "Parsing bytes to Sigul fields");
let mut map = HashMap::new();
let mut input = input.iter();
let field_count = *input.next().unwrap_or(&0);
tracing::trace!("Expecting to parse {} fields", field_count);
for field_id in 0..field_count {
let key_length = *input.next().ok_or(Error::MissingBytes)?;
if key_length == 0 {
return Err(Error::Message("key length was 0, which is invalid".into()));
}
tracing::trace!(field_id, "Expecting key to be {} bytes long", key_length);
let mut key = vec![];
while key.len() < key_length.into() {
key.push(*input.next().ok_or(Error::MissingBytes)?);
}
let key = String::from_utf8(key)
.map_err(|err| Error::Message(format!("Invalid UTF-8 used in key: {err:?}")))?;
let mut value = vec![];
let value_length = *input.next().ok_or(Error::MissingBytes)?;
tracing::trace!(
field_id,
"Expecting value to be {} bytes long",
value_length
);
while value.len() < value_length.into() {
value.push(*input.next().ok_or(Error::MissingBytes)?);
}
map.insert(key, value);
}
Ok(map)
}