xref: /webrtc/ice/src/priority/mod.rs (revision c40fea4f)
1 #[cfg(test)]
2 mod priority_test;
3 
4 use stun::attributes::ATTR_PRIORITY;
5 use stun::checks::*;
6 use stun::message::*;
7 
8 /// Represents PRIORITY attribute.
9 #[derive(Default, PartialEq, Eq, Debug, Copy, Clone)]
10 pub struct PriorityAttr(pub u32);
11 
12 const PRIORITY_SIZE: usize = 4; // 32 bit
13 
14 impl Setter for PriorityAttr {
15     // add_to adds PRIORITY attribute to message.
add_to(&self, m: &mut Message) -> Result<(), stun::Error>16     fn add_to(&self, m: &mut Message) -> Result<(), stun::Error> {
17         let mut v = vec![0_u8; PRIORITY_SIZE];
18         v.copy_from_slice(&self.0.to_be_bytes());
19         m.add(ATTR_PRIORITY, &v);
20         Ok(())
21     }
22 }
23 
24 impl PriorityAttr {
25     /// Decodes PRIORITY attribute from message.
get_from(&mut self, m: &Message) -> Result<(), stun::Error>26     pub fn get_from(&mut self, m: &Message) -> Result<(), stun::Error> {
27         let v = m.get(ATTR_PRIORITY)?;
28 
29         check_size(ATTR_PRIORITY, v.len(), PRIORITY_SIZE)?;
30 
31         let p = u32::from_be_bytes([v[0], v[1], v[2], v[3]]);
32         self.0 = p;
33 
34         Ok(())
35     }
36 }
37