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
use crate::parse::slice_to_string;

use std::fmt;

use crate::SdpNetworkType;
use crate::parse_network_type;
use crate::SdpAddressType;
use crate::parse_address_type;
use crate::attributes::SdpOptionalAttribute;

#[derive(Debug, PartialEq, Clone)]
pub struct SdpConnection {
    pub network_type: SdpNetworkType,
    pub address_type: SdpAddressType,
    pub address: String
}

impl SdpConnection {

    pub fn new<S: Into<String>>(address: S) -> SdpConnection {
        SdpConnection {
            network_type: SdpNetworkType::Internet,
            address_type: SdpAddressType::Ipv4,
            address: address.into()
        }
    }
}

use nom::{
    IResult,
    character::complete::char,
    combinator::map_res,
    bytes::complete::{take_until, tag}
};

pub fn parse_connection(input: &[u8]) -> IResult<&[u8], SdpConnection> {
    let (input, network_type) = parse_network_type(input)?;
    let (input, _) = char(' ')(input)?;
    let (input, address_type) = parse_address_type(input)?;
    let (input, _) = char(' ')(input)?;
    let (input, address) = map_res(take_until("\r"), slice_to_string)(input)?;
    Ok((input, SdpConnection { network_type, address_type, address }))
}

impl fmt::Display for SdpConnection {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} {} {}", self.network_type, self.address_type, self.address)
    }
}

pub fn parse_connection_name(input: &[u8]) -> IResult<&[u8], SdpOptionalAttribute> {
    let (input, _) = tag("c=")(input)?;
    let (input, conn) = parse_connection(input)?;
    let (input, _) = tag("\r\n")(input)?;
    Ok((input, SdpOptionalAttribute::Connection(conn)))
}