Debian Codegraph @

third_party/rust/webrtc-sdp/src/network.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use address::{Address, AddressType};
6 use error::SdpParserInternalError;
7 use std::net::IpAddr;
8 use std::str::FromStr;
9
10 pub fn ip_address_to_string(addr: IpAddr) -> String {
11 match addr {
12 IpAddr::V4(ipv4) => format!("IN IP4 {ipv4}"),
13 IpAddr::V6(ipv6) => format!("IN IP6 {ipv6}"),
14 }
15 }
16
17 pub fn parse_network_type(value: &str) -> Result<(), SdpParserInternalError> {
18 if value.to_uppercase() != "IN" {
19 return Err(SdpParserInternalError::Generic(
20 "nettype must be IN".to_string(),
21 ));
22 };
23 Ok(())
24 }
25
26 pub fn parse_address_type(value: &str) -> Result<AddressType, SdpParserInternalError> {
27 AddressType::from_str(value.to_uppercase().as_str())
28 .map_err(|_| SdpParserInternalError::Generic("address type must be IP4 or IP6".to_string()))
29 }
30
31 pub fn parse_unicast_address(value: &str) -> Result<Address, SdpParserInternalError> {
32 Address::from_str(value)
33 }
34
35 #[cfg(test)]
36 #[path = "./network_tests.rs"]
37 mod tests;