presence: Make Show handle the None case, and rename PresenceType to Type.

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-28 17:10:12 +01:00
parent 32bfa84551
commit 9eb8f39a38

View file

@ -8,7 +8,7 @@ use std::convert::TryFrom;
use std::str::FromStr; use std::str::FromStr;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use minidom::{Element, IntoAttributeValue}; use minidom::{Element, IntoElements, IntoAttributeValue, ElementEmitter};
use jid::Jid; use jid::Jid;
@ -24,12 +24,19 @@ use ecaps2::ECaps2;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum Show { pub enum Show {
None,
Away, Away,
Chat, Chat,
Dnd, Dnd,
Xa, Xa,
} }
impl Default for Show {
fn default() -> Show {
Show::None
}
}
impl FromStr for Show { impl FromStr for Show {
type Err = Error; type Err = Error;
@ -45,16 +52,21 @@ impl FromStr for Show {
} }
} }
impl Into<Element> for Show { impl IntoElements for Show {
fn into(self) -> Element { fn into_elements(self, emitter: &mut ElementEmitter) {
if self == Show::None {
return;
}
emitter.append_child(
Element::builder("show") Element::builder("show")
.append(match self { .append(match self {
Show::Away => "away", Show::None => unreachable!(),
Show::Chat => "chat", Show::Away => Some("away"),
Show::Dnd => "dnd", Show::Chat => Some("chat"),
Show::Xa => "xa", Show::Dnd => Some("dnd"),
Show::Xa => Some("xa"),
}) })
.build() .build())
} }
} }
@ -114,7 +126,7 @@ impl Into<Element> for PresencePayload {
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum PresenceType { pub enum Type {
/// This value is not an acceptable 'type' attribute, it is only used /// This value is not an acceptable 'type' attribute, it is only used
/// internally to signal the absence of 'type'. /// internally to signal the absence of 'type'.
None, None,
@ -127,42 +139,42 @@ pub enum PresenceType {
Unsubscribed, Unsubscribed,
} }
impl Default for PresenceType { impl Default for Type {
fn default() -> PresenceType { fn default() -> Type {
PresenceType::None Type::None
} }
} }
impl FromStr for PresenceType { impl FromStr for Type {
type Err = Error; type Err = Error;
fn from_str(s: &str) -> Result<PresenceType, Error> { fn from_str(s: &str) -> Result<Type, Error> {
Ok(match s { Ok(match s {
"error" => PresenceType::Error, "error" => Type::Error,
"probe" => PresenceType::Probe, "probe" => Type::Probe,
"subscribe" => PresenceType::Subscribe, "subscribe" => Type::Subscribe,
"subscribed" => PresenceType::Subscribed, "subscribed" => Type::Subscribed,
"unavailable" => PresenceType::Unavailable, "unavailable" => Type::Unavailable,
"unsubscribe" => PresenceType::Unsubscribe, "unsubscribe" => Type::Unsubscribe,
"unsubscribed" => PresenceType::Unsubscribed, "unsubscribed" => Type::Unsubscribed,
_ => return Err(Error::ParseError("Invalid 'type' attribute on presence element.")), _ => return Err(Error::ParseError("Invalid 'type' attribute on presence element.")),
}) })
} }
} }
impl IntoAttributeValue for PresenceType { impl IntoAttributeValue for Type {
fn into_attribute_value(self) -> Option<String> { fn into_attribute_value(self) -> Option<String> {
Some(match self { Some(match self {
PresenceType::None => return None, Type::None => return None,
PresenceType::Error => "error", Type::Error => "error",
PresenceType::Probe => "probe", Type::Probe => "probe",
PresenceType::Subscribe => "subscribe", Type::Subscribe => "subscribe",
PresenceType::Subscribed => "subscribed", Type::Subscribed => "subscribed",
PresenceType::Unavailable => "unavailable", Type::Unavailable => "unavailable",
PresenceType::Unsubscribe => "unsubscribe", Type::Unsubscribe => "unsubscribe",
PresenceType::Unsubscribed => "unsubscribed", Type::Unsubscribed => "unsubscribed",
}.to_owned()) }.to_owned())
} }
} }
@ -172,8 +184,8 @@ pub struct Presence {
pub from: Option<Jid>, pub from: Option<Jid>,
pub to: Option<Jid>, pub to: Option<Jid>,
pub id: Option<String>, pub id: Option<String>,
pub type_: PresenceType, pub type_: Type,
pub show: Option<Show>, pub show: Show,
pub statuses: BTreeMap<Lang, Status>, pub statuses: BTreeMap<Lang, Status>,
pub priority: Priority, pub priority: Priority,
pub payloads: Vec<Element>, pub payloads: Vec<Element>,
@ -186,20 +198,21 @@ impl TryFrom<Element> for Presence {
if !root.is("presence", ns::JABBER_CLIENT) { if !root.is("presence", ns::JABBER_CLIENT) {
return Err(Error::ParseError("This is not a presence element.")); return Err(Error::ParseError("This is not a presence element."));
} }
let mut show = None;
let mut priority = None; let mut priority = None;
let mut presence = Presence { let mut presence = Presence {
from: get_attr!(root, "from", optional), from: get_attr!(root, "from", optional),
to: get_attr!(root, "to", optional), to: get_attr!(root, "to", optional),
id: get_attr!(root, "id", optional), id: get_attr!(root, "id", optional),
type_: get_attr!(root, "type", default), type_: get_attr!(root, "type", default),
show: None, show: Show::None,
statuses: BTreeMap::new(), statuses: BTreeMap::new(),
priority: 0i8, priority: 0i8,
payloads: vec!(), payloads: vec!(),
}; };
for elem in root.children() { for elem in root.children() {
if elem.is("show", ns::JABBER_CLIENT) { if elem.is("show", ns::JABBER_CLIENT) {
if presence.show.is_some() { if show.is_some() {
return Err(Error::ParseError("More than one show element in a presence.")); return Err(Error::ParseError("More than one show element in a presence."));
} }
for _ in elem.children() { for _ in elem.children() {
@ -208,7 +221,7 @@ impl TryFrom<Element> for Presence {
for _ in elem.attrs() { for _ in elem.attrs() {
return Err(Error::ParseError("Unknown attribute in show element.")); return Err(Error::ParseError("Unknown attribute in show element."));
} }
presence.show = Some(Show::from_str(elem.text().as_ref())?); show = Some(Show::from_str(elem.text().as_ref())?);
} else if elem.is("status", ns::JABBER_CLIENT) { } else if elem.is("status", ns::JABBER_CLIENT) {
for _ in elem.children() { for _ in elem.children() {
return Err(Error::ParseError("Unknown child in status element.")); return Err(Error::ParseError("Unknown child in status element."));
@ -237,6 +250,9 @@ impl TryFrom<Element> for Presence {
presence.payloads.push(elem.clone()); presence.payloads.push(elem.clone());
} }
} }
if let Some(show) = show {
presence.show = show;
}
if let Some(priority) = priority { if let Some(priority) = priority {
presence.priority = priority; presence.priority = priority;
} }
@ -252,10 +268,7 @@ impl Into<Element> for Presence {
.attr("to", self.to.and_then(|value| Some(String::from(value)))) .attr("to", self.to.and_then(|value| Some(String::from(value))))
.attr("id", self.id) .attr("id", self.id)
.attr("type", self.type_) .attr("type", self.type_)
.append(match self.show { .append(self.show)
Some(show) => Some({ let elem: Element = show.into(); elem }),
None => None
})
.append(self.statuses.iter().map(|(lang, status)| { .append(self.statuses.iter().map(|(lang, status)| {
Element::builder("status") Element::builder("status")
.attr("xml:lang", match lang.as_ref() { .attr("xml:lang", match lang.as_ref() {
@ -283,7 +296,7 @@ mod tests {
assert_eq!(presence.from, None); assert_eq!(presence.from, None);
assert_eq!(presence.to, None); assert_eq!(presence.to, None);
assert_eq!(presence.id, None); assert_eq!(presence.id, None);
assert_eq!(presence.type_, PresenceType::None); assert_eq!(presence.type_, Type::None);
assert!(presence.payloads.is_empty()); assert!(presence.payloads.is_empty());
} }
@ -294,8 +307,8 @@ mod tests {
from: None, from: None,
to: None, to: None,
id: None, id: None,
type_: PresenceType::Unavailable, type_: Type::Unavailable,
show: None, show: Show::None,
statuses: BTreeMap::new(), statuses: BTreeMap::new(),
priority: 0i8, priority: 0i8,
payloads: vec!(), payloads: vec!(),
@ -309,7 +322,7 @@ mod tests {
let elem: Element = "<presence xmlns='jabber:client'><show>chat</show></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><show>chat</show></presence>".parse().unwrap();
let presence = Presence::try_from(elem).unwrap(); let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.payloads.len(), 0); assert_eq!(presence.payloads.len(), 0);
assert_eq!(presence.show, Some(Show::Chat)); assert_eq!(presence.show, Show::Chat);
} }
#[test] #[test]
@ -432,8 +445,8 @@ mod tests {
from: None, from: None,
to: None, to: None,
id: None, id: None,
type_: PresenceType::Unavailable, type_: Type::Unavailable,
show: None, show: Show::None,
statuses: statuses, statuses: statuses,
priority: 0i8, priority: 0i8,
payloads: vec!(), payloads: vec!(),