1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::fmt;
use nom::{bytes::complete::tag, character::complete::line_ending, IResult};
use crate::{util, Parse};
#[derive(Clone, Debug, PartialEq)]
pub struct ConnectionData {
pub network_type: String,
pub address_type: String,
pub connection_address: String,
}
impl fmt::Display for ConnectionData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"c={} {} {}\r",
self.network_type, self.address_type, self.connection_address
)
}
}
impl Parse for ConnectionData {
fn parse(input: &str) -> IResult<&str, Self> {
let (rest, _) = tag("c=")(input)?;
let (rest, network_type) = util::parse_field(rest)?;
let (rest, _) = tag(" ")(rest)?;
let (rest, address_type) = util::parse_field(rest)?;
let (rest, _) = tag(" ")(rest)?;
let (rest, connection_address) = util::parse_field(rest)?;
let (rest, _) = line_ending(rest)?;
Ok((
rest,
Self {
network_type,
address_type,
connection_address,
},
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::{assert_err, assert_parse_display};
#[test]
fn test_valid() {
assert_parse_display(
"c=IN IP4 224.2.1.1/127/3\r\n rest\n",
" rest\n",
&ConnectionData {
network_type: "IN".to_owned(),
address_type: "IP4".to_owned(),
connection_address: "224.2.1.1/127/3".to_owned(),
},
"c=IN IP4 224.2.1.1/127/3\r\n",
);
}
#[test]
fn test_invalid() {
assert_err::<ConnectionData>("c=IN IP4\r\n");
assert_err::<ConnectionData>("c=IN IP4 224.2.1.1/127/3 foo\r\n");
}
}