diameter_interface/modeling/avp/
group.rs

1use crate::errors::DiameterResult;
2use crate::modeling::avp::avp::{Avp, AvpFlags, AvpValue};
3use crate::modeling::avp::AvpData;
4use crate::modeling::message::dictionary::Dictionary;
5use std::io::{Read, Write};
6use std::sync::Arc;
7
8pub type Grouped = AvpData<Vec<Avp>>;
9
10impl Grouped {
11    pub(super) fn encode_to<W: Write>(&self, writer: &mut W) -> DiameterResult<()> {
12        for avp in &self.0 {
13            avp.encode_to(writer)?;
14        }
15        Ok(())
16    }
17
18    pub(super) fn decode_from<R: Read>(
19        reader: &mut R,
20        length: usize,
21        dict: Arc<Dictionary>,
22    ) -> DiameterResult<AvpData<Vec<Avp>>> {
23        let mut avps_length = length;
24        let mut avps: Vec<Avp> = Vec::new();
25        while avps_length > 0 {
26            let avp = Avp::decode_from(reader, Arc::clone(&dict))?;
27            avps_length -= avp.get_length() as usize;
28            avps.push(avp);
29        }
30        Ok(AvpData(avps))
31    }
32
33    pub(super) fn len(&self) -> u32 {
34        self.0.iter().map(|avp| avp.get_length()).sum()
35    }
36}
37
38impl Grouped {
39    pub fn avps(&self) -> &Vec<Avp> {
40        &self.0
41    }
42
43    pub fn add(&mut self, avp: Avp) {
44        self.0.push(avp);
45    }
46
47    pub fn add_avp(&mut self, code: u32, vendor_id: Option<u32>, flags: AvpFlags, value: AvpValue) {
48        let avp = Avp::new(code, flags, vendor_id, value);
49        self.add(avp);
50    }
51}
52
53impl Into<AvpValue> for Grouped {
54    fn into(self) -> AvpValue {
55        AvpValue::Grouped(self)
56    }
57}