stanza_error: Generate DefinedCondition automatically.

This commit is contained in:
Emmanuel Gil Peyrot 2017-11-24 04:30:33 +00:00
parent 2661259e9a
commit ef04d2e524

View file

@ -8,7 +8,7 @@ use try_from::TryFrom;
use std::str::FromStr; use std::str::FromStr;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use minidom::{Element, IntoElements, IntoAttributeValue, ElementEmitter}; use minidom::{Element, IntoAttributeValue};
use error::Error; use error::Error;
use jid::Jid; use jid::Jid;
@ -22,93 +22,30 @@ generate_attribute!(ErrorType, "type", {
Wait => "wait", Wait => "wait",
}); });
#[derive(Debug, Clone, PartialEq)] generate_element_enum!(DefinedCondition, "condition", ns::XMPP_STANZAS, {
pub enum DefinedCondition { BadRequest => "bad-request",
BadRequest, Conflict => "conflict",
Conflict, FeatureNotImplemented => "feature-not-implemented",
FeatureNotImplemented, Forbidden => "forbidden",
Forbidden, Gone => "gone",
Gone, InternalServerError => "internal-server-error",
InternalServerError, ItemNotFound => "item-not-found",
ItemNotFound, JidMalformed => "jid-malformed",
JidMalformed, NotAcceptable => "not-acceptable",
NotAcceptable, NotAllowed => "not-allowed",
NotAllowed, NotAuthorized => "not-authorized",
NotAuthorized, PolicyViolation => "policy-violation",
PolicyViolation, RecipientUnavailable => "recipient-unavailable",
RecipientUnavailable, Redirect => "redirect",
Redirect, RegistrationRequired => "registration-required",
RegistrationRequired, RemoteServerNotFound => "remote-server-not-found",
RemoteServerNotFound, RemoteServerTimeout => "remote-server-timeout",
RemoteServerTimeout, ResourceConstraint => "resource-constraint",
ResourceConstraint, ServiceUnavailable => "service-unavailable",
ServiceUnavailable, SubscriptionRequired => "subscription-required",
SubscriptionRequired, UndefinedCondition => "undefined-condition",
UndefinedCondition, UnexpectedRequest => "unexpected-request",
UnexpectedRequest, });
}
impl FromStr for DefinedCondition {
type Err = Error;
fn from_str(s: &str) -> Result<DefinedCondition, Error> {
Ok(match s {
"bad-request" => DefinedCondition::BadRequest,
"conflict" => DefinedCondition::Conflict,
"feature-not-implemented" => DefinedCondition::FeatureNotImplemented,
"forbidden" => DefinedCondition::Forbidden,
"gone" => DefinedCondition::Gone,
"internal-server-error" => DefinedCondition::InternalServerError,
"item-not-found" => DefinedCondition::ItemNotFound,
"jid-malformed" => DefinedCondition::JidMalformed,
"not-acceptable" => DefinedCondition::NotAcceptable,
"not-allowed" => DefinedCondition::NotAllowed,
"not-authorized" => DefinedCondition::NotAuthorized,
"policy-violation" => DefinedCondition::PolicyViolation,
"recipient-unavailable" => DefinedCondition::RecipientUnavailable,
"redirect" => DefinedCondition::Redirect,
"registration-required" => DefinedCondition::RegistrationRequired,
"remote-server-not-found" => DefinedCondition::RemoteServerNotFound,
"remote-server-timeout" => DefinedCondition::RemoteServerTimeout,
"resource-constraint" => DefinedCondition::ResourceConstraint,
"service-unavailable" => DefinedCondition::ServiceUnavailable,
"subscription-required" => DefinedCondition::SubscriptionRequired,
"undefined-condition" => DefinedCondition::UndefinedCondition,
"unexpected-request" => DefinedCondition::UnexpectedRequest,
_ => return Err(Error::ParseError("Unknown defined-condition.")),
})
}
}
impl IntoElements for DefinedCondition {
fn into_elements(self, emitter: &mut ElementEmitter) {
emitter.append_child(Element::builder(match self {
DefinedCondition::BadRequest => "bad-request",
DefinedCondition::Conflict => "conflict",
DefinedCondition::FeatureNotImplemented => "feature-not-implemented",
DefinedCondition::Forbidden => "forbidden",
DefinedCondition::Gone => "gone",
DefinedCondition::InternalServerError => "internal-server-error",
DefinedCondition::ItemNotFound => "item-not-found",
DefinedCondition::JidMalformed => "jid-malformed",
DefinedCondition::NotAcceptable => "not-acceptable",
DefinedCondition::NotAllowed => "not-allowed",
DefinedCondition::NotAuthorized => "not-authorized",
DefinedCondition::PolicyViolation => "policy-violation",
DefinedCondition::RecipientUnavailable => "recipient-unavailable",
DefinedCondition::Redirect => "redirect",
DefinedCondition::RegistrationRequired => "registration-required",
DefinedCondition::RemoteServerNotFound => "remote-server-not-found",
DefinedCondition::RemoteServerTimeout => "remote-server-timeout",
DefinedCondition::ResourceConstraint => "resource-constraint",
DefinedCondition::ServiceUnavailable => "service-unavailable",
DefinedCondition::SubscriptionRequired => "subscription-required",
DefinedCondition::UndefinedCondition => "undefined-condition",
DefinedCondition::UnexpectedRequest => "unexpected-request",
}).ns(ns::XMPP_STANZAS).build());
}
}
pub type Lang = String; pub type Lang = String;
@ -151,7 +88,7 @@ impl TryFrom<Element> for StanzaError {
for _ in child.children() { for _ in child.children() {
return Err(Error::ParseError("Unknown element in defined-condition.")); return Err(Error::ParseError("Unknown element in defined-condition."));
} }
let condition = DefinedCondition::from_str(child.name())?; let condition = DefinedCondition::try_from(child.clone())?;
defined_condition = Some(condition); defined_condition = Some(condition);
} else { } else {
if other.is_some() { if other.is_some() {