sunspec_models/
utils.rs

1// All function of this module were copied from a different crate, available at:
2// https://github.com/lukaskirner/tokio-sunspec/blob/main/src/utils.rs
3
4pub fn apply_scale_factor(value: u16, scale_factor: u16) -> u16 {
5    value * u16::pow(10, scale_factor as u32)
6}
7
8pub(crate) fn to_be_bytes(data: Vec<u16>) -> Vec<u8> {
9    return data
10        .iter()
11        .flat_map(|v| v.to_be_bytes())
12        .collect::<Vec<u8>>();
13}
14
15pub(crate) fn to_u16_vector(data: &[u8]) -> Vec<u16> {
16    let chunks = data.chunks_exact(2);
17    let remainder = chunks.remainder();
18
19    let mut result: Vec<u16> = chunks
20        .into_iter()
21        .map(|a| u16::from_be_bytes([a[0], a[1]]))
22        .collect();
23
24    if !remainder.is_empty() {
25        result.push(u16::from_be_bytes([remainder[0], 0]));
26    }
27
28    result
29}