From 03a1d915a4e33d7be62a867f0e0e1a4a5ac0c7c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 26 Jun 2019 01:50:52 +0200 Subject: [PATCH] presence: Remove Show::None and make presence.show Option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This should make it easier to upgrade to minidom 0.11, to avoid having to implement an Iterator for Show, and just implement Into. It also makes a bit more sense to me semantically. Signed-off-by: Maxime “pep” Buquet --- src/presence.rs | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/presence.rs b/src/presence.rs index b53b6714..ffb734c5 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -19,9 +19,6 @@ pub trait PresencePayload: TryFrom + Into {} /// Specifies the availability of an entity or resource. #[derive(Debug, Clone, PartialEq)] pub enum Show { - /// Not an actual show value, but an indication there is no show set. - None, - /// The entity or resource is temporarily away. Away, @@ -36,12 +33,6 @@ pub enum Show { Xa, } -impl Default for Show { - fn default() -> Show { - Show::None - } -} - impl FromStr for Show { type Err = Error; @@ -59,13 +50,9 @@ impl FromStr for Show { impl IntoElements for Show { fn into_elements(self, emitter: &mut ElementEmitter) { - if self == Show::None { - return; - } emitter.append_child( Element::builder("show") .append(match self { - Show::None => unreachable!(), Show::Away => Some("away"), Show::Chat => Some("chat"), Show::Dnd => Some("dnd"), @@ -177,7 +164,7 @@ pub struct Presence { pub type_: Type, /// The availability of the sender of this presence. - pub show: Show, + pub show: Option, /// A localised list of statuses defined in this presence. pub statuses: BTreeMap, @@ -198,7 +185,7 @@ impl Presence { to: None, id: None, type_, - show: Show::None, + show: None, statuses: BTreeMap::new(), priority: 0i8, payloads: vec![], @@ -228,7 +215,7 @@ impl Presence { /// Set the availability information of this presence. pub fn with_show(mut self, show: Show) -> Presence { - self.show = show; + self.show = Some(show); self } @@ -270,7 +257,7 @@ impl TryFrom for Presence { to: get_attr!(root, "to", Option), id: get_attr!(root, "id", Option), type_: get_attr!(root, "type", Default), - show: Show::None, + show: None, statuses: BTreeMap::new(), priority: 0i8, payloads: vec![], @@ -307,9 +294,7 @@ impl TryFrom for Presence { presence.payloads.push(elem.clone()); } } - if let Some(show) = show { - presence.show = show; - } + presence.show = show; if let Some(priority) = priority { presence.priority = priority; } @@ -423,7 +408,21 @@ mod tests { .unwrap(); let presence = Presence::try_from(elem).unwrap(); assert_eq!(presence.payloads.len(), 0); - assert_eq!(presence.show, Show::Chat); + assert_eq!(presence.show, Some(Show::Chat)); + } + + #[test] + fn test_empty_show_value() { + #[cfg(not(feature = "component"))] + let elem: Element = "" + .parse() + .unwrap(); + #[cfg(feature = "component")] + let elem: Element = "" + .parse() + .unwrap(); + let presence = Presence::try_from(elem).unwrap(); + assert_eq!(presence.show, None); } #[test]