sdp_rs/lines/
phone.rs

1//! Types related to the phone line (`p=`).
2
3/// The phone line (`p=`) tokenizer. This is low level stuff and you shouldn't interact directly
4/// with it, unless you know what you are doing.
5pub use crate::tokenizers::value::Tokenizer;
6
7/// A phone number line (`p=`) of SDP. Note that more than one such line could exist in an SDP
8/// message, that's why [crate::SessionDescription] has a `Vec<Phone>` defined.
9#[derive(Debug, PartialEq, Eq, Ord, PartialOrd, Clone)]
10pub struct Phone(String);
11
12impl Phone {
13    pub fn new(phone: String) -> Self {
14        Self(phone)
15    }
16
17    pub fn value(&self) -> &str {
18        &self.0
19    }
20}
21
22impl From<Phone> for String {
23    fn from(phone: Phone) -> Self {
24        phone.0
25    }
26}
27
28impl From<String> for Phone {
29    fn from(phone: String) -> Self {
30        Self(phone)
31    }
32}
33
34impl<'a> From<Tokenizer<'a, 'p'>> for Phone {
35    fn from(tokenizer: Tokenizer<'a, 'p'>) -> Self {
36        Self(tokenizer.value.into())
37    }
38}
39
40impl std::fmt::Display for Phone {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        write!(f, "p={}", self.value())
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn from_tokenizer1() {
52        let tokenizer: Tokenizer<'p'> = "+1234567890".into();
53
54        assert_eq!(Phone::from(tokenizer), Phone("+1234567890".into()));
55    }
56
57    #[test]
58    fn display1() {
59        let phone = Phone::new("+1234567890".into());
60
61        assert_eq!(phone.to_string(), "p=+1234567890");
62    }
63}