From 3ea0c45337111b9664fe80ae4de760e157e9b3ac Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 25 Jun 2017 21:38:58 +0100 Subject: [PATCH] jingle_s5b: Strengthen type safety for ids. --- src/jingle_s5b.rs | 54 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/src/jingle_s5b.rs b/src/jingle_s5b.rs index 089d160..b585cf1 100644 --- a/src/jingle_s5b.rs +++ b/src/jingle_s5b.rs @@ -20,9 +20,46 @@ generate_attribute!(Type, "type", { Tunnel => "tunnel", }, Default = Direct); +generate_attribute!(Mode, "mode", { + Tcp => "tcp", + Udp => "udp", +}, Default = Tcp); + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct CandidateId(String); + +impl FromStr for CandidateId { + type Err = Error; + fn from_str(s: &str) -> Result { + Ok(CandidateId(String::from(s))) + } +} + +impl IntoAttributeValue for CandidateId { + fn into_attribute_value(self) -> Option { + return Some(self.0); + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct StreamId(String); + +impl FromStr for StreamId { + type Err = Error; + fn from_str(s: &str) -> Result { + Ok(StreamId(String::from(s))) + } +} + +impl IntoAttributeValue for StreamId { + fn into_attribute_value(self) -> Option { + return Some(self.0); + } +} + #[derive(Debug, Clone)] pub struct Candidate { - pub cid: String, + pub cid: CandidateId, pub host: String, pub jid: String, pub port: Option, @@ -44,11 +81,6 @@ impl Into for Candidate { } } -generate_attribute!(Mode, "mode", { - Tcp => "tcp", - Udp => "udp", -}, Default = Tcp); - #[derive(Debug, Clone)] pub enum TransportPayload { Activated(String), @@ -61,7 +93,7 @@ pub enum TransportPayload { #[derive(Debug, Clone)] pub struct Transport { - pub sid: String, + pub sid: StreamId, pub dstaddr: Option, pub mode: Mode, pub payload: TransportPayload, @@ -181,7 +213,7 @@ mod tests { fn test_simple() { let elem: Element = "".parse().unwrap(); let transport = Transport::try_from(elem).unwrap(); - assert_eq!(transport.sid, "coucou"); + assert_eq!(transport.sid, StreamId(String::from("coucou"))); assert_eq!(transport.dstaddr, None); assert_eq!(transport.mode, Mode::Tcp); match transport.payload { @@ -194,7 +226,7 @@ mod tests { fn test_serialise_activated() { let elem: Element = "".parse().unwrap(); let transport = Transport { - sid: String::from("coucou"), + sid: StreamId(String::from("coucou")), dstaddr: None, mode: Mode::Tcp, payload: TransportPayload::Activated(String::from("coucou")), @@ -207,11 +239,11 @@ mod tests { fn test_serialise_candidate() { let elem: Element = "".parse().unwrap(); let transport = Transport { - sid: String::from("coucou"), + sid: StreamId(String::from("coucou")), dstaddr: None, mode: Mode::Tcp, payload: TransportPayload::Candidates(vec!(Candidate { - cid: String::from("coucou"), + cid: CandidateId(String::from("coucou")), host: String::from("coucou"), jid: String::from("coucou@coucou"), port: None,