use xso::{AsXml, FromXml};
use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
use crate::ns;
use jid::Jid;
generate_attribute!(
DefaultPrefs, "default", {
Always => "always",
Never => "never",
Roster => "roster",
}
);
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
#[xml(namespace = ns::MAM, name = "prefs")]
pub struct Prefs {
#[xml(attribute = "default")]
pub default_: DefaultPrefs,
#[xml(extract(default, fields(extract(n = .., name = "jid", fields(text(type_ = Jid))))))]
pub always: Vec<Jid>,
#[xml(extract(default, fields(extract(n = .., name = "jid", fields(text(type_ = Jid))))))]
pub never: Vec<Jid>,
}
impl IqGetPayload for Prefs {}
impl IqSetPayload for Prefs {}
impl IqResultPayload for Prefs {}
#[cfg(test)]
mod tests {
use super::*;
use jid::BareJid;
use minidom::Element;
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(DefaultPrefs, 1);
assert_size!(Prefs, 28);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(DefaultPrefs, 1);
assert_size!(Prefs, 56);
}
#[test]
fn test_prefs_get() {
let elem: Element = "<prefs xmlns='urn:xmpp:mam:2' default='always'/>"
.parse()
.unwrap();
let prefs = Prefs::try_from(elem).unwrap();
assert!(prefs.always.is_empty());
assert!(prefs.never.is_empty());
let elem: Element = r#"<prefs xmlns='urn:xmpp:mam:2' default='roster'>
<always/>
<never/>
</prefs>
"#
.parse()
.unwrap();
let prefs = Prefs::try_from(elem).unwrap();
assert!(prefs.always.is_empty());
assert!(prefs.never.is_empty());
}
#[test]
fn test_prefs_result() {
let elem: Element = r#"<prefs xmlns='urn:xmpp:mam:2' default='roster'>
<always>
<jid>romeo@montague.lit</jid>
</always>
<never>
<jid>montague@montague.lit</jid>
</never>
</prefs>
"#
.parse()
.unwrap();
let prefs = Prefs::try_from(elem).unwrap();
assert_eq!(prefs.always, [BareJid::new("romeo@montague.lit").unwrap()]);
assert_eq!(
prefs.never,
[BareJid::new("montague@montague.lit").unwrap()]
);
let elem2 = Element::from(prefs.clone());
println!("{:?}", elem2);
let prefs2 = Prefs::try_from(elem2).unwrap();
assert_eq!(prefs.default_, prefs2.default_);
assert_eq!(prefs.always, prefs2.always);
assert_eq!(prefs.never, prefs2.never);
}
}