sdp_rs/lines/connection/
mod.rs1mod connection_address;
4
5pub use crate::tokenizers::connection::Tokenizer;
8pub use connection_address::ConnectionAddress;
9
10use crate::{
11 lines::common::{Addrtype, Nettype},
12 Error,
13};
14use std::convert::{TryFrom, TryInto};
15
16#[derive(Debug, PartialEq, Eq, Ord, PartialOrd, Clone)]
20pub struct Connection {
21 pub nettype: Nettype,
22 pub addrtype: Addrtype,
23 pub connection_address: ConnectionAddress,
24}
25
26impl<'a> TryFrom<Tokenizer<'a>> for Connection {
27 type Error = Error;
28
29 fn try_from(tokenizer: Tokenizer<'a>) -> Result<Self, Self::Error> {
30 Ok(Self {
31 nettype: tokenizer.nettype.into(),
32 addrtype: tokenizer.addrtype.into(),
33 connection_address: tokenizer.connection_address.try_into()?,
34 })
35 }
36}
37
38impl std::fmt::Display for Connection {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 write!(
41 f,
42 "c={} {} {}",
43 self.nettype, self.addrtype, self.connection_address
44 )
45 }
46}
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn from_tokenizer1() {
54 let tokenizer: Tokenizer = ("IN", "IP4", ("233.252.0.1")).into();
55
56 assert_eq!(
57 Connection::try_from(tokenizer),
58 Ok(Connection {
59 nettype: Nettype::In,
60 addrtype: Addrtype::Ip4,
61 connection_address: ConnectionAddress {
62 base: "233.252.0.1".parse().unwrap(),
63 ttl: None,
64 numaddr: None
65 }
66 })
67 );
68 }
69
70 #[test]
71 fn from_tokenizer2() {
72 let tokenizer: Tokenizer = ("In", "Ip4", ("233.252.0.1", "127", "2")).into();
73
74 assert_eq!(
75 Connection::try_from(tokenizer),
76 Ok(Connection {
77 nettype: Nettype::Other("In".into()),
78 addrtype: Addrtype::Other("Ip4".into()),
79 connection_address: ConnectionAddress {
80 base: "233.252.0.1".parse().unwrap(),
81 ttl: Some(127),
82 numaddr: Some(2)
83 }
84 })
85 );
86 }
87
88 #[test]
89 fn display1() {
90 let connection = Connection {
91 nettype: Nettype::In,
92 addrtype: Addrtype::Other("Ip4".into()),
93 connection_address: ConnectionAddress {
94 base: "233.252.0.1".parse().unwrap(),
95 ttl: Some(127),
96 numaddr: Some(2),
97 },
98 };
99
100 assert_eq!(connection.to_string(), "c=IN Ip4 233.252.0.1/127/2");
101 }
102}