use crate::core::{FromXml, IntoXml};
use crate::message::MessagePayload;
use crate::ns::OID;
use crate::presence::PresencePayload;
#[derive(FromXml, IntoXml, Debug, PartialEq, Clone)]
#[xml(namespace = OID, name = "occupant-id")]
pub struct OccupantId {
#[xml(attribute)]
id: String,
}
impl MessagePayload for OccupantId {}
impl PresencePayload for OccupantId {}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::error::Error;
use crate::Element;
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(OccupantId, 12);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(OccupantId, 24);
}
#[test]
fn test_simple() {
let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0' id='coucou'/>"
.parse()
.unwrap();
let origin_id = OccupantId::try_from(elem).unwrap();
assert_eq!(origin_id.id, "coucou");
}
#[test]
fn test_invalid_child() {
let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0'><coucou/></occupant-id>"
.parse()
.unwrap();
let error = OccupantId::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in OccupantId element.");
}
#[test]
fn test_invalid_id() {
let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0'/>"
.parse()
.unwrap();
let error = OccupantId::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(
message,
"Required attribute field 'id' on OccupantId element missing."
);
}
#[test]
fn test_serialise() {
let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0' id='coucou'/>"
.parse()
.unwrap();
let occupant_id = OccupantId {
id: String::from("coucou"),
};
let elem2 = occupant_id.into();
assert_eq!(elem, elem2);
}
}