diff --git a/src/protos/xmpp/disco.rs b/src/protos/xmpp/disco.rs index 04fb8d6..b5858c2 100644 --- a/src/protos/xmpp/disco.rs +++ b/src/protos/xmpp/disco.rs @@ -1,5 +1,6 @@ -use quick_xml::events::Event; -use quick_xml::name::ResolveResult; +use quick_xml::events::attributes::Attribute; +use quick_xml::events::{BytesEnd, BytesStart, Event}; +use quick_xml::name::{QName, ResolveResult}; use crate::prelude::*; use crate::util::xml::*; @@ -79,6 +80,56 @@ impl FromXmlTag for InfoQuery { const NS: &'static str = XMLNS_INFO; } +impl ToXml for InfoQuery { + fn serialize(&self, events: &mut Vec>) { + let mut bytes = BytesStart::new(format!(r#"query xmlns="{}""#, XMLNS_INFO)); + if let Some(node) = &self.node { + bytes.push_attribute(Attribute { + key: QName(b"node"), + value: node.as_bytes().into(), + }); + } + let empty = self.feature.is_empty() && self.identity.is_empty(); + if empty { + events.push(Event::Empty(bytes)); + } else { + events.push(Event::Start(bytes)); + } + + for i in &self.identity { + let mut bytes = BytesStart::new("identity"); + bytes.push_attribute(Attribute { + key: QName(b"category"), + value: i.category.as_bytes().into(), + }); + if let Some(name) = &i.name { + bytes.push_attribute(Attribute { + key: QName(b"name"), + value: name.as_bytes().into(), + }); + } + bytes.push_attribute(Attribute { + key: QName(b"type"), + value: i.r#type.as_bytes().into(), + }); + events.push(Event::Empty(bytes)); + } + + for f in &self.feature { + let mut bytes = BytesStart::new("feature"); + bytes.push_attribute(Attribute { + key: QName(b"var"), + value: f.var.as_bytes().into(), + }); + events.push(Event::Empty(bytes)); + } + + if !empty { + events.push(Event::End(BytesEnd::new("query"))); + } + } +} + #[derive(PartialEq, Eq, Debug)] pub struct Identity { category: String, @@ -241,6 +292,43 @@ impl FromXmlTag for ItemQuery { const NS: &'static str = XMLNS_ITEM; } +impl ToXml for ItemQuery { + fn serialize(&self, events: &mut Vec>) { + let mut bytes = BytesStart::new(format!(r#"query xmlns="{}""#, XMLNS_ITEM)); + let empty = self.item.is_empty(); + if empty { + events.push(Event::Empty(bytes)); + } else { + events.push(Event::Start(bytes)); + } + + for f in &self.item { + let mut bytes = BytesStart::new("item"); + bytes.push_attribute(Attribute { + key: QName(b"jid"), + value: f.jid.to_string().into_bytes().into(), + }); + if let Some(name) = &f.name { + bytes.push_attribute(Attribute { + key: QName(b"name"), + value: name.as_bytes().into(), + }); + } + if let Some(node) = &f.node { + bytes.push_attribute(Attribute { + key: QName(b"node"), + value: node.as_bytes().into(), + }); + } + events.push(Event::Empty(bytes)); + } + + if !empty { + events.push(Event::End(BytesEnd::new("query"))); + } + } +} + #[derive(PartialEq, Eq, Debug)] pub struct Item { pub jid: super::bind::Jid,