Skip to content

Commit d9257ff

Browse files
committed
Add new enums to convert from strings
Also write new methods to access the parsed SDP members as enums
1 parent 41e37f8 commit d9257ff

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

src/lib.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
//! are currently parsed as a plain string and not according to the SDP
4444
//! grammar.
4545
46+
use std::str::FromStr;
47+
4648
use bstr::*;
4749
use fallible_iterator::FallibleIterator;
4850

@@ -51,6 +53,65 @@ mod writer;
5153

5254
pub use parser::ParserError;
5355

56+
#[derive(Debug, PartialEq, Eq, Clone)]
57+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
58+
pub enum NetType {
59+
In,
60+
}
61+
62+
#[derive(Debug, PartialEq, Eq, Clone)]
63+
pub enum ParseEnumError {
64+
Invalid(String),
65+
}
66+
67+
68+
impl std::str::FromStr for NetType {
69+
type Err = ParseEnumError;
70+
71+
fn from_str(s: &str) -> Result<Self, Self::Err> {
72+
match s {
73+
"IN" => Ok(NetType::In),
74+
_ => Err(ParseEnumError::Invalid(s.to_string())),
75+
}
76+
}
77+
}
78+
79+
impl Into<String> for NetType {
80+
fn into(self) -> String {
81+
match self {
82+
NetType::In => format!("IN")
83+
}
84+
}
85+
}
86+
87+
#[derive(Debug, PartialEq, Eq, Clone)]
88+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
89+
pub enum AddrType {
90+
Ip4,
91+
Ip6,
92+
}
93+
94+
impl std::str::FromStr for AddrType {
95+
type Err = ParseEnumError;
96+
97+
fn from_str(s: &str) -> Result<Self, Self::Err> {
98+
match s {
99+
"IP4" => Ok(AddrType::Ip4),
100+
"IP6" => Ok(AddrType::Ip6),
101+
_ => Err(ParseEnumError::Invalid(s.to_string())),
102+
}
103+
}
104+
}
105+
106+
impl Into<String> for AddrType {
107+
fn into(self) -> String {
108+
match self {
109+
AddrType::Ip4 => format!("IP4"),
110+
AddrType::Ip6 => format!("IP6")
111+
}
112+
}
113+
}
114+
54115
/// Originator of the session.
55116
///
56117
/// See [RFC 4566 Section 5.2](https://tools.ietf.org/html/rfc4566#section-5.2) for more details.
@@ -311,6 +372,26 @@ impl Session {
311372
}
312373
}
313374

375+
376+
impl Origin {
377+
378+
pub fn try_addrtype(&self) -> Result<AddrType, ParseEnumError> {
379+
AddrType::from_str(self.addrtype.as_str())
380+
}
381+
382+
pub fn set_from_addrtype(&mut self, addrtype: AddrType) {
383+
self.addrtype = addrtype.into();
384+
}
385+
386+
pub fn try_nettype(&self) -> Result<NetType, ParseEnumError> {
387+
NetType::from_str(self.nettype.as_str())
388+
}
389+
390+
pub fn set_from_nettype(&mut self, nettype: NetType) {
391+
self.nettype = nettype.into();
392+
}
393+
}
394+
314395
#[cfg(test)]
315396
mod tests {
316397
use super::*;
@@ -472,5 +553,6 @@ a=fingerprint:sha-256 3A:96:6D:57:B2:C2:C7:61:A0:46:3E:1C:97:39:D3:F7:0A:88:A0:B
472553
&[None]
473554
);
474555
assert!(session.get_attribute_values("foo").is_err());
556+
assert_eq!(session.origin.try_addrtype(), Ok(AddrType::Ip4));
475557
}
476558
}

src/parser.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,8 @@ a=control:track1\r
862862
a=rtpmap:8 PCMA/8000/1\r
863863
864864
";
865-
let _parsed = Session::parse(&sdp[..]).unwrap();
865+
let parsed = Session::parse(&sdp[..]).unwrap();
866+
assert_eq!(parsed.origin.try_nettype(), Ok(NetType::In));
866867
}
867868

868869
/// Parses SDP from a Geovision camera which (incorrectly) omits the "t="

0 commit comments

Comments
 (0)