From f83fe922446ef55cfd688f4c10d674253d18b31d Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 29 Jul 2017 02:47:33 +0100 Subject: [PATCH] jingle: Wrap the disposition in a struct, to get better type safety. --- src/jingle.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/jingle.rs b/src/jingle.rs index 62fb4b5..364ee4b 100644 --- a/src/jingle.rs +++ b/src/jingle.rs @@ -45,10 +45,19 @@ generate_attribute!(Senders, "senders", { generate_id!(ContentId); +// TODO: the list of values is defined, use an enum! +generate_id!(Disposition); + +impl Default for Disposition { + fn default() -> Disposition { + Disposition(String::from("session")) + } +} + #[derive(Debug, Clone)] pub struct Content { pub creator: Creator, - pub disposition: String, // TODO: the list of values is defined, use an enum! + pub disposition: Disposition, pub name: ContentId, pub senders: Senders, pub description: Option, @@ -66,7 +75,7 @@ impl TryFrom for Content { let mut content = Content { creator: get_attr!(elem, "creator", required), - disposition: get_attr!(elem, "disposition", optional).unwrap_or(String::from("session")), + disposition: get_attr!(elem, "disposition", default), name: get_attr!(elem, "name", required), senders: get_attr!(elem, "senders", default), description: None, @@ -344,7 +353,7 @@ mod tests { assert_eq!(jingle.contents[0].creator, Creator::Initiator); assert_eq!(jingle.contents[0].name, ContentId(String::from("coucou"))); assert_eq!(jingle.contents[0].senders, Senders::Both); - assert_eq!(jingle.contents[0].disposition, "session"); + assert_eq!(jingle.contents[0].disposition, Disposition(String::from("session"))); let elem: Element = "".parse().unwrap(); let jingle = Jingle::try_from(elem).unwrap(); @@ -352,7 +361,7 @@ mod tests { let elem: Element = "".parse().unwrap(); let jingle = Jingle::try_from(elem).unwrap(); - assert_eq!(jingle.contents[0].disposition, "early-session"); + assert_eq!(jingle.contents[0].disposition, Disposition(String::from("early-session"))); } #[test]