sdp_rs/lines/
email.rs

1//! Types related to the email line (`e=`).
2
3/// The email line (`e=`) 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/// An email (`e=`) of SDP. Note that more than one such line could exist in an SDP
8/// message, that's why [crate::SessionDescription] has a `Vec<Email>` defined.
9#[derive(Debug, PartialEq, Eq, Ord, PartialOrd, Clone)]
10pub struct Email(String);
11
12impl Email {
13    pub fn new(email: String) -> Self {
14        Self(email)
15    }
16
17    pub fn value(&self) -> &str {
18        &self.0
19    }
20}
21
22impl From<Email> for String {
23    fn from(email: Email) -> Self {
24        email.0
25    }
26}
27
28impl From<String> for Email {
29    fn from(email: String) -> Self {
30        Self(email)
31    }
32}
33
34impl<'a> From<Tokenizer<'a, 'e'>> for Email {
35    fn from(tokenizer: Tokenizer<'a, 'e'>) -> Self {
36        Self(tokenizer.value.into())
37    }
38}
39
40impl std::fmt::Display for Email {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        write!(f, "e={}", self.value())
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn from_tokenizer1() {
52        let tokenizer: Tokenizer<'e'> = "hello@televiska.com".into();
53
54        assert_eq!(Email::from(tokenizer), Email("hello@televiska.com".into()));
55    }
56
57    #[test]
58    fn display1() {
59        let email = Email::new("hello@televiska.com".into());
60
61        assert_eq!(email.to_string(), "e=hello@televiska.com");
62    }
63}