use xso::{AsXml, FromXml};
use crate::mam;
use crate::ns;
use minidom::Element;
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::BIND2, name = "bind")]
pub struct BindFeature {
#[xml(extract(default, name = "inline", fields(extract(n = .., name = "feature", fields(attribute(name = "var", type_ = String))))))]
pub inline_features: Vec<String>,
}
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::BIND2, name = "bind")]
pub struct BindQuery {
#[xml(extract(default, fields(text(type_ = String))))]
pub tag: Option<String>,
#[xml(element(n = ..))]
pub payloads: Vec<Element>,
}
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::BIND2, name = "bound")]
pub struct Bound {
#[xml(child(default))]
pub mam_metadata: Option<mam::MetadataResponse>,
#[xml(element(n = ..))]
pub payloads: Vec<Element>,
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(BindFeature, 12);
assert_size!(BindQuery, 24);
assert_size!(Bound, 68);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(BindFeature, 24);
assert_size!(BindQuery, 48);
assert_size!(Bound, 104);
}
#[test]
fn test_empty() {
let elem: Element = "<bind xmlns='urn:xmpp:bind:0'/>".parse().unwrap();
let bind = BindQuery::try_from(elem).unwrap();
assert_eq!(bind.tag, None);
assert_eq!(bind.payloads.len(), 0);
}
#[test]
fn test_simple() {
let elem: Element =
"<bind xmlns='urn:xmpp:bind:0'><inline><feature var='urn:xmpp:carbons:2'/><feature var='urn:xmpp:csi:0'/><feature var='urn:xmpp:sm:3'/></inline></bind>"
.parse()
.unwrap();
let bind = BindFeature::try_from(elem.clone()).unwrap();
assert_eq!(bind.inline_features.len(), 3);
assert_eq!(bind.inline_features[0], "urn:xmpp:carbons:2");
assert_eq!(bind.inline_features[1], "urn:xmpp:csi:0");
assert_eq!(bind.inline_features[2], "urn:xmpp:sm:3");
let elem2 = bind.into();
assert_eq!(elem, elem2);
let elem: Element = "<bind xmlns='urn:xmpp:bind:0'><tag>AwesomeXMPP</tag></bind>"
.parse()
.unwrap();
let bind = BindQuery::try_from(elem).unwrap();
assert_eq!(bind.tag.unwrap(), "AwesomeXMPP");
assert_eq!(bind.payloads.len(), 0);
let elem: Element = "<bind xmlns='urn:xmpp:bind:0'><tag>AwesomeXMPP</tag><enable xmlns='urn:xmpp:carbons:2'/><enable xmlns='urn:xmpp:sm:3'/><inactive xmlns='urn:xmpp:csi:0'/></bind>".parse().unwrap();
let bind = BindQuery::try_from(elem).unwrap();
assert_eq!(bind.tag.unwrap(), "AwesomeXMPP");
assert_eq!(bind.payloads.len(), 3);
let elem: Element = "<bound xmlns='urn:xmpp:bind:0'><metadata xmlns='urn:xmpp:mam:2'><start id='YWxwaGEg' timestamp='2008-08-22T21:09:04Z'/><end id='b21lZ2Eg' timestamp='2020-04-20T14:34:21Z'/></metadata></bound>".parse().unwrap();
let bound = Bound::try_from(elem).unwrap();
assert!(bound.mam_metadata.is_some());
assert_eq!(bound.payloads.len(), 0);
}
}