feat(xmpp): serialization of disco iqs

This commit is contained in:
Nikita Vilunov 2023-03-29 00:34:12 +02:00
parent 4ce97f8e13
commit fb2cbf8a8c
1 changed files with 90 additions and 2 deletions

View File

@ -1,5 +1,6 @@
use quick_xml::events::Event; use quick_xml::events::attributes::Attribute;
use quick_xml::name::ResolveResult; use quick_xml::events::{BytesEnd, BytesStart, Event};
use quick_xml::name::{QName, ResolveResult};
use crate::prelude::*; use crate::prelude::*;
use crate::util::xml::*; use crate::util::xml::*;
@ -79,6 +80,56 @@ impl FromXmlTag for InfoQuery {
const NS: &'static str = XMLNS_INFO; const NS: &'static str = XMLNS_INFO;
} }
impl ToXml for InfoQuery {
fn serialize(&self, events: &mut Vec<Event<'static>>) {
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)] #[derive(PartialEq, Eq, Debug)]
pub struct Identity { pub struct Identity {
category: String, category: String,
@ -241,6 +292,43 @@ impl FromXmlTag for ItemQuery {
const NS: &'static str = XMLNS_ITEM; const NS: &'static str = XMLNS_ITEM;
} }
impl ToXml for ItemQuery {
fn serialize(&self, events: &mut Vec<Event<'static>>) {
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)] #[derive(PartialEq, Eq, Debug)]
pub struct Item { pub struct Item {
pub jid: super::bind::Jid, pub jid: super::bind::Jid,