|  | 
|  | 1 | +// Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ | 
|  | 2 | +// SPDX-License-Identifier: Apache-2.0 | 
|  | 3 | + | 
|  | 4 | +pub mod error; | 
|  | 5 | +pub mod number; | 
|  | 6 | +pub mod span_link; | 
|  | 7 | +pub mod string; | 
|  | 8 | +use crate::msgpack_decoder::decode::error::DecodeError; | 
|  | 9 | +use crate::msgpack_decoder::decode::number::read_number_bytes; | 
|  | 10 | +use crate::msgpack_decoder::decode::string::{handle_null_marker, read_string_bytes}; | 
|  | 11 | +use rmp::{decode, decode::RmpRead, Marker}; | 
|  | 12 | +use std::{collections::HashMap, f64}; | 
|  | 13 | +use tinybytes::{Bytes, BytesString}; | 
|  | 14 | + | 
|  | 15 | +#[inline] | 
|  | 16 | +pub fn read_metric_pair(buf: &mut Bytes) -> Result<(BytesString, f64), DecodeError> { | 
|  | 17 | +    let key = read_string_bytes(buf)?; | 
|  | 18 | +    let v = read_number_bytes(buf)?; | 
|  | 19 | + | 
|  | 20 | +    Ok((key, v)) | 
|  | 21 | +} | 
|  | 22 | +#[inline] | 
|  | 23 | +pub fn read_metrics(buf: &mut Bytes) -> Result<HashMap<BytesString, f64>, DecodeError> { | 
|  | 24 | +    if let Some(empty_map) = handle_null_marker(buf, HashMap::default) { | 
|  | 25 | +        return Ok(empty_map); | 
|  | 26 | +    } | 
|  | 27 | + | 
|  | 28 | +    let len = read_map_len(unsafe { buf.as_mut_slice() })?; | 
|  | 29 | + | 
|  | 30 | +    read_map(len, buf, read_metric_pair) | 
|  | 31 | +} | 
|  | 32 | + | 
|  | 33 | +#[inline] | 
|  | 34 | +pub fn read_meta_struct(buf: &mut Bytes) -> Result<HashMap<BytesString, Vec<u8>>, DecodeError> { | 
|  | 35 | +    if let Some(empty_map) = handle_null_marker(buf, HashMap::default) { | 
|  | 36 | +        return Ok(empty_map); | 
|  | 37 | +    } | 
|  | 38 | + | 
|  | 39 | +    fn read_meta_struct_pair(buf: &mut Bytes) -> Result<(BytesString, Vec<u8>), DecodeError> { | 
|  | 40 | +        let key = read_string_bytes(buf)?; | 
|  | 41 | +        let array_len = decode::read_array_len(unsafe { buf.as_mut_slice() }).map_err(|_| { | 
|  | 42 | +            DecodeError::InvalidFormat("Unable to read array len for meta_struct".to_owned()) | 
|  | 43 | +        })?; | 
|  | 44 | + | 
|  | 45 | +        let mut v = Vec::with_capacity(array_len as usize); | 
|  | 46 | + | 
|  | 47 | +        for _ in 0..array_len { | 
|  | 48 | +            let value = read_number_bytes(buf)?; | 
|  | 49 | +            v.push(value); | 
|  | 50 | +        } | 
|  | 51 | +        Ok((key, v)) | 
|  | 52 | +    } | 
|  | 53 | + | 
|  | 54 | +    let len = read_map_len(unsafe { buf.as_mut_slice() })?; | 
|  | 55 | +    read_map(len, buf, read_meta_struct_pair) | 
|  | 56 | +} | 
|  | 57 | + | 
|  | 58 | +/// Reads a map from the buffer and returns it as a `HashMap`. | 
|  | 59 | +/// | 
|  | 60 | +/// This function is generic over the key and value types of the map, and it uses a provided | 
|  | 61 | +/// function to read key-value pairs from the buffer. | 
|  | 62 | +/// | 
|  | 63 | +/// # Arguments | 
|  | 64 | +/// | 
|  | 65 | +/// * `len` - The number of key-value pairs to read from the buffer. | 
|  | 66 | +/// * `buf` - A reference to the Bytes containing the encoded map data. | 
|  | 67 | +/// * `read_pair` - A function that reads a key-value pair from the buffer and returns it as a | 
|  | 68 | +///   `Result<(K, V), DecodeError>`. | 
|  | 69 | +/// | 
|  | 70 | +/// # Returns | 
|  | 71 | +/// | 
|  | 72 | +/// * `Ok(HashMap<K, V>)` - A `HashMap` containing the decoded key-value pairs if successful. | 
|  | 73 | +/// * `Err(DecodeError)` - An error if the decoding process fails. | 
|  | 74 | +/// | 
|  | 75 | +/// # Errors | 
|  | 76 | +/// | 
|  | 77 | +/// This function will return an error if: | 
|  | 78 | +/// - The `read_pair` function returns an error while reading a key-value pair. | 
|  | 79 | +/// | 
|  | 80 | +/// # Type Parameters | 
|  | 81 | +/// | 
|  | 82 | +/// * `K` - The type of the keys in the map. Must implement `std::hash::Hash` and `Eq`. | 
|  | 83 | +/// * `V` - The type of the values in the map. | 
|  | 84 | +/// * `F` - The type of the function used to read key-value pairs from the buffer. | 
|  | 85 | +#[inline] | 
|  | 86 | +pub fn read_map<K, V, F>( | 
|  | 87 | +    len: usize, | 
|  | 88 | +    buf: &mut Bytes, | 
|  | 89 | +    read_pair: F, | 
|  | 90 | +) -> Result<HashMap<K, V>, DecodeError> | 
|  | 91 | +where | 
|  | 92 | +    K: std::hash::Hash + Eq, | 
|  | 93 | +    F: Fn(&mut Bytes) -> Result<(K, V), DecodeError>, | 
|  | 94 | +{ | 
|  | 95 | +    let mut map = HashMap::with_capacity(len); | 
|  | 96 | +    for _ in 0..len { | 
|  | 97 | +        let (k, v) = read_pair(buf)?; | 
|  | 98 | +        map.insert(k, v); | 
|  | 99 | +    } | 
|  | 100 | +    Ok(map) | 
|  | 101 | +} | 
|  | 102 | + | 
|  | 103 | +#[inline] | 
|  | 104 | +pub fn read_map_len(buf: &mut &[u8]) -> Result<usize, DecodeError> { | 
|  | 105 | +    match decode::read_marker(buf) | 
|  | 106 | +        .map_err(|_| DecodeError::InvalidFormat("Unable to read marker for map".to_owned()))? | 
|  | 107 | +    { | 
|  | 108 | +        Marker::FixMap(len) => Ok(len as usize), | 
|  | 109 | +        Marker::Map16 => buf | 
|  | 110 | +            .read_data_u16() | 
|  | 111 | +            .map_err(|_| DecodeError::IOError) | 
|  | 112 | +            .map(|len| len as usize), | 
|  | 113 | +        Marker::Map32 => buf | 
|  | 114 | +            .read_data_u32() | 
|  | 115 | +            .map_err(|_| DecodeError::IOError) | 
|  | 116 | +            .map(|len| len as usize), | 
|  | 117 | +        _ => Err(DecodeError::InvalidType( | 
|  | 118 | +            "Unable to read map from buffer".to_owned(), | 
|  | 119 | +        )), | 
|  | 120 | +    } | 
|  | 121 | +} | 
0 commit comments