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
#[cfg(test)]
mod priority_test;

use stun::attributes::ATTR_PRIORITY;
use stun::checks::*;
use stun::message::*;

/// Represents PRIORITY attribute.
#[derive(Default, PartialEq, Eq, Debug, Copy, Clone)]
pub struct PriorityAttr(pub u32);

const PRIORITY_SIZE: usize = 4; // 32 bit

impl Setter for PriorityAttr {
    // add_to adds PRIORITY attribute to message.
    fn add_to(&self, m: &mut Message) -> Result<(), stun::Error> {
        let mut v = vec![0_u8; PRIORITY_SIZE];
        v.copy_from_slice(&self.0.to_be_bytes());
        m.add(ATTR_PRIORITY, &v);
        Ok(())
    }
}

impl PriorityAttr {
    /// Decodes PRIORITY attribute from message.
    pub fn get_from(&mut self, m: &Message) -> Result<(), stun::Error> {
        let v = m.get(ATTR_PRIORITY)?;

        check_size(ATTR_PRIORITY, v.len(), PRIORITY_SIZE)?;

        let p = u32::from_be_bytes([v[0], v[1], v[2], v[3]]);
        self.0 = p;

        Ok(())
    }
}