use quick_xml::events::{BytesStart, Event}; use crate::xml::*; use anyhow::{anyhow, Result}; use quick_xml::name::ResolveResult; pub const XMLNS: &'static str = "jabber:iq:roster"; #[derive(PartialEq, Eq, Debug)] pub struct RosterQuery; impl FromXml for RosterQuery { type P = impl Parser>; fn parse() -> Self::P { |(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result { match event { Event::Start(_) => (), Event::Empty(_) => return Ok(RosterQuery), _ => return Err(anyhow!("Unexpected XML event: {event:?}")), } (namespace, event) = yield; match event { Event::End(_) => return Ok(RosterQuery), _ => return Err(anyhow!("Unexpected XML event: {event:?}")), } } } } impl FromXmlTag for RosterQuery { const NAME: &'static str = "query"; const NS: &'static str = XMLNS; } impl ToXml for RosterQuery { fn serialize(&self, events: &mut Vec>) { events.push(Event::Empty(BytesStart::new(format!(r#"query xmlns="{}""#, XMLNS)))); } } #[cfg(test)] mod tests { use super::*; use crate::bind::{Jid, Name, Resource, Server}; use crate::client::{Iq, IqType}; #[test] fn test_parse() { let input = r#""#; let result: Iq = parse(input).unwrap(); assert_eq!( result, Iq { from: Option::from(Jid { name: Option::from(Name("juliet".into())), server: Server("example.com".into()), resource: Option::from(Resource("balcony".into())) }), id: "bv1bs71f".to_string(), to: None, r#type: IqType::Get, body: RosterQuery, } ) } }