sdp_rs/lines/bandwidth/
mod.rs

1//! Types related to the bandwidth line (`b=`).
2
3mod bwtype;
4
5pub use bwtype::Bwtype;
6
7use crate::Error;
8use std::convert::TryFrom;
9
10/// The bandwidth line (`b=`) tokenizer. This is low level stuff and you shouldn't interact directly
11/// with it, unless you know what you are doing.
12pub use crate::tokenizers::key_value::Tokenizer;
13
14/// A bandwidth line (`b=`) of SDP, which could appear in the main SDP session or in a Media
15/// description.
16#[derive(Debug, PartialEq, Eq, Ord, PartialOrd, Clone)]
17pub struct Bandwidth {
18    pub bwtype: Bwtype,
19    pub bandwidth: u32,
20}
21
22impl<'a> TryFrom<Tokenizer<'a, 'b'>> for Bandwidth {
23    type Error = Error;
24
25    fn try_from(tokenizer: Tokenizer<'a, 'b'>) -> Result<Self, Self::Error> {
26        Ok(Self {
27            bwtype: tokenizer.key.into(),
28            bandwidth: tokenizer.value.parse().map_err(|e| {
29                Self::Error::parser_with_error("bandwidth value", tokenizer.value, e)
30            })?,
31        })
32    }
33}
34
35impl std::fmt::Display for Bandwidth {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        write!(f, "b={}:{}", self.bwtype, self.bandwidth)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn from_tokenizer1() {
47        let tokenizer: Tokenizer<'b'> = ("AS", "80").into();
48
49        assert_eq!(
50            Bandwidth::try_from(tokenizer),
51            Ok(Bandwidth {
52                bwtype: Bwtype::As,
53                bandwidth: 80,
54            })
55        );
56    }
57
58    #[test]
59    fn display1() {
60        let bandwidth = Bandwidth {
61            bwtype: Bwtype::As,
62            bandwidth: 80,
63        };
64
65        assert_eq!(bandwidth.to_string(), "b=AS:80");
66    }
67}