Add a generate_attribute! macro, and use it for the common case.

This commit is contained in:
Emmanuel Gil Peyrot 2017-06-14 00:50:57 +01:00
commit 0f297d2d2d
10 changed files with 158 additions and 499 deletions

View file

@ -102,14 +102,13 @@ impl Into<Element> for MessagePayload {
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum MessageType {
Chat,
Error,
Groupchat,
Headline,
Normal,
}
generate_attribute!(MessageType, "type", {
Chat => "chat",
Error => "error",
Groupchat => "groupchat",
Headline => "headline",
Normal => "normal",
});
impl Default for MessageType {
fn default() -> MessageType {
@ -117,34 +116,6 @@ impl Default for MessageType {
}
}
impl FromStr for MessageType {
type Err = Error;
fn from_str(s: &str) -> Result<MessageType, Error> {
Ok(match s {
"chat" => MessageType::Chat,
"error" => MessageType::Error,
"groupchat" => MessageType::Groupchat,
"headline" => MessageType::Headline,
"normal" => MessageType::Normal,
_ => return Err(Error::ParseError("Invalid 'type' attribute on message element.")),
})
}
}
impl IntoAttributeValue for MessageType {
fn into_attribute_value(self) -> Option<String> {
Some(match self {
MessageType::Chat => "chat",
MessageType::Error => "error",
MessageType::Groupchat => "groupchat",
MessageType::Headline => "headline",
MessageType::Normal => "normal",
}.to_owned())
}
}
type Lang = String;
type Body = String;
type Subject = String;