1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::parser::bmp::error::ParserBmpError;
use crate::parser::DataBytes;

#[derive(Debug)]
pub struct StatsReport {
    pub stats_count: u32,
    pub counters: Vec<StatCounter>
}

#[derive(Debug)]
pub struct StatCounter {
    pub stat_type: u16,
    pub stat_len: u16,
    pub stat_data: StatsData
}

#[derive(Debug)]
pub enum StatsData {
    Counter(u32),
    Gauge(u64),
}

pub fn parse_stats_report(reader: &mut DataBytes) -> Result<StatsReport, ParserBmpError> {
    let stats_count = reader.read_32b()?;
    let mut counters = vec![];
    for _ in 0..stats_count {
        let stat_type = reader.read_16b()?;
        let stat_len = reader.read_16b()?;
        let stat_data = match stat_len {
            4 => {
                StatsData::Counter(reader.read_32b()?)
            }
            8 => {
                StatsData::Gauge(reader.read_64b()?)
            }
            _ => {
                return Err(ParserBmpError::CorruptedBmpMessage)
            }
        };
        counters.push(StatCounter{
            stat_type,
            stat_len,
            stat_data
        })
    }

    Ok(
        StatsReport{
            stats_count,
            counters
        }
    )
}