use xso::{AsXml, FromXml};
use crate::ns;
use crate::stanza_error::DefinedCondition;
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::SM, name = "a")]
pub struct A {
#[xml(attribute)]
pub h: u32,
}
impl A {
pub fn new(h: u32) -> A {
A { h }
}
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone, Default)]
#[xml(namespace = ns::SM, name = "enable")]
pub struct Enable {
#[xml(attribute(default))]
pub max: Option<u32>,
#[xml(attribute(default))]
pub resume: bool,
}
impl Enable {
pub fn new() -> Self {
Enable::default()
}
pub fn with_max(mut self, max: u32) -> Self {
self.max = Some(max);
self
}
pub fn with_resume(mut self) -> Self {
self.resume = true;
self
}
}
generate_id!(
StreamId
);
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::SM, name = "enabled")]
pub struct Enabled {
#[xml(attribute(default))]
pub id: Option<StreamId>,
#[xml(attribute(default))]
pub location: Option<String>,
#[xml(attribute(default))]
pub max: Option<u32>,
#[xml(attribute(default))]
pub resume: bool,
}
#[derive(FromXml, AsXml, Debug, PartialEq, Clone)]
#[xml(namespace = ns::SM, name = "failed")]
pub struct Failed {
#[xml(attribute)]
pub h: Option<u32>,
#[xml(child(default))]
pub error: Option<DefinedCondition>,
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::SM, name = "r")]
pub struct R;
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::SM, name = "resume")]
pub struct Resume {
#[xml(attribute)]
pub h: u32,
#[xml(attribute)]
pub previd: StreamId,
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::SM, name = "resumed")]
pub struct Resumed {
#[xml(attribute)]
pub h: u32,
#[xml(attribute)]
pub previd: StreamId,
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::SM, name = "sm")]
pub struct StreamManagement {
#[xml(flag)]
pub optional: bool,
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::SM, name = "handled-count-too-high")]
pub struct HandledCountTooHigh {
#[xml(attribute)]
pub h: u32,
#[xml(attribute = "send-count")]
pub send_count: u32,
}
impl From<HandledCountTooHigh> for crate::stream_error::StreamError {
fn from(other: HandledCountTooHigh) -> Self {
Self {
condition: crate::stream_error::DefinedCondition::UndefinedCondition,
text: Some((
None,
format!(
"You acknowledged {} stanza(s), while I only sent {} so far.",
other.h, other.send_count
),
)),
application_specific: vec![other.into()],
}
}
}
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
#[xml()]
pub enum Nonza {
#[xml(transparent)]
Enable(Enable),
#[xml(transparent)]
Enabled(Enabled),
#[xml(transparent)]
Resume(Resume),
#[xml(transparent)]
Resumed(Resumed),
#[xml(transparent)]
Failed(Failed),
#[xml(transparent)]
Ack(A),
#[xml(transparent)]
Req(R),
}
#[cfg(test)]
mod tests {
use super::*;
use minidom::Element;
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(A, 4);
assert_size!(Enable, 12);
assert_size!(StreamId, 12);
assert_size!(Enabled, 36);
assert_size!(Failed, 24);
assert_size!(R, 0);
assert_size!(Resume, 16);
assert_size!(Resumed, 16);
assert_size!(StreamManagement, 1);
assert_size!(HandledCountTooHigh, 8);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(A, 4);
assert_size!(Enable, 12);
assert_size!(StreamId, 24);
assert_size!(Enabled, 64);
assert_size!(Failed, 40);
assert_size!(R, 0);
assert_size!(Resume, 32);
assert_size!(Resumed, 32);
assert_size!(StreamManagement, 1);
assert_size!(HandledCountTooHigh, 8);
}
#[test]
fn a() {
let elem: Element = "<a xmlns='urn:xmpp:sm:3' h='5'/>".parse().unwrap();
let a = A::try_from(elem).unwrap();
assert_eq!(a.h, 5);
}
#[test]
fn stream_feature() {
let elem: Element = "<sm xmlns='urn:xmpp:sm:3'/>".parse().unwrap();
StreamManagement::try_from(elem).unwrap();
}
#[test]
fn handle_count_too_high() {
let elem: Element = "<handled-count-too-high xmlns='urn:xmpp:sm:3' h='10' send-count='8'/>"
.parse()
.unwrap();
let elem = HandledCountTooHigh::try_from(elem).unwrap();
assert_eq!(elem.h, 10);
assert_eq!(elem.send_count, 8);
}
#[test]
fn resume() {
let elem: Element = "<enable xmlns='urn:xmpp:sm:3' resume='true'/>"
.parse()
.unwrap();
let enable = Enable::try_from(elem).unwrap();
assert_eq!(enable.max, None);
assert_eq!(enable.resume, true);
let elem: Element = "<enabled xmlns='urn:xmpp:sm:3' resume='true' id='coucou' max='600'/>"
.parse()
.unwrap();
let enabled = Enabled::try_from(elem).unwrap();
let previd = enabled.id.unwrap();
assert_eq!(enabled.resume, true);
assert_eq!(previd, StreamId(String::from("coucou")));
assert_eq!(enabled.max, Some(600));
assert_eq!(enabled.location, None);
let elem: Element = "<resume xmlns='urn:xmpp:sm:3' h='5' previd='coucou'/>"
.parse()
.unwrap();
let resume = Resume::try_from(elem).unwrap();
assert_eq!(resume.h, 5);
assert_eq!(resume.previd, previd);
let elem: Element = "<resumed xmlns='urn:xmpp:sm:3' h='5' previd='coucou'/>"
.parse()
.unwrap();
let resumed = Resumed::try_from(elem).unwrap();
assert_eq!(resumed.h, 5);
assert_eq!(resumed.previd, previd);
}
#[test]
fn test_serialize_failed() {
let reference: Element = "<failed xmlns='urn:xmpp:sm:3'><unexpected-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></failed>"
.parse()
.unwrap();
let elem: Element = "<unexpected-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
.parse()
.unwrap();
let error = DefinedCondition::try_from(elem).unwrap();
let failed = Failed {
h: None,
error: Some(error),
};
let serialized: Element = failed.into();
assert_eq!(serialized, reference);
}
}