use crate::msgpack_decoder::decode::buffer::Buffer;
use crate::msgpack_decoder::decode::error::DecodeError;
use crate::span::DeserializableTraceData;
use rmp::decode;
use std::collections::HashMap;
const NULL_MARKER: &u8 = &0xc0;
#[inline]
pub fn read_nullable_string<T: DeserializableTraceData>(
buf: &mut Buffer<T>,
) -> Result<T::Text, DecodeError> {
if handle_null_marker(buf) {
Ok(T::Text::default())
} else {
buf.read_string()
}
}
#[inline]
pub fn read_str_map_to_strings<T: DeserializableTraceData>(
buf: &mut Buffer<T>,
) -> Result<HashMap<T::Text, T::Text>, DecodeError> {
let len = decode::read_map_len(buf.as_mut_slice())
.map_err(|_| DecodeError::InvalidFormat("Unable to get map len for str map".to_owned()))?;
#[allow(clippy::expect_used)]
let mut map = HashMap::with_capacity(len.try_into().expect("Unable to cast map len to usize"));
for _ in 0..len {
let key = buf.read_string()?;
if !handle_null_marker(buf) {
let value = buf.read_string()?;
map.insert(key, value);
}
}
Ok(map)
}
#[inline]
pub fn read_nullable_str_map_to_strings<T: DeserializableTraceData>(
buf: &mut Buffer<T>,
) -> Result<HashMap<T::Text, T::Text>, DecodeError> {
if handle_null_marker(buf) {
return Ok(HashMap::default());
}
read_str_map_to_strings(buf)
}
#[inline]
pub fn handle_null_marker<T: DeserializableTraceData>(buf: &mut Buffer<T>) -> bool {
let slice = buf.as_mut_slice();
if slice.first() == Some(NULL_MARKER) {
*slice = &slice[1..];
true
} else {
false
}
}