lavina/src/projections/xmpp/proto.rs

93 lines
2.8 KiB
Rust
Raw Normal View History

2023-03-11 17:36:38 +00:00
use derive_more::From;
use quick_xml::events::Event;
use quick_xml::name::{Namespace, ResolveResult};
use crate::protos::xmpp::bind::BindRequest;
2023-03-15 14:27:48 +00:00
use crate::protos::xmpp::client::{Iq, Message, Presence};
2023-03-27 21:52:31 +00:00
use crate::protos::xmpp::disco::{InfoQuery, ItemQuery};
2023-03-12 21:50:28 +00:00
use crate::protos::xmpp::roster::RosterQuery;
2023-03-12 13:15:13 +00:00
use crate::protos::xmpp::session::Session;
2023-03-12 21:50:28 +00:00
use crate::util::xml::*;
2023-03-11 17:36:38 +00:00
use crate::prelude::*;
#[derive(PartialEq, Eq, Debug, From)]
pub enum IqClientBody {
Bind(BindRequest),
2023-03-12 13:15:13 +00:00
Session(Session),
2023-03-12 21:50:28 +00:00
Roster(RosterQuery),
2023-03-27 21:52:31 +00:00
DiscoInfo(InfoQuery),
DiscoItem(ItemQuery),
Unknown(Ignore),
2023-03-11 17:36:38 +00:00
}
impl FromXml for IqClientBody {
type P = impl Parser<Output = Result<Self>>;
2023-03-11 17:36:38 +00:00
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let bytes = match event {
Event::Start(bytes) => bytes,
Event::Empty(bytes) => bytes,
_ => return Err(ffail!("Unexpected XML event: {event:?}")),
};
let name = bytes.name();
match_parser!(name, namespace, event;
BindRequest,
Session,
RosterQuery,
2023-03-27 21:52:31 +00:00
InfoQuery,
ItemQuery,
{
delegate_parsing!(Ignore, namespace, event).into()
}
)
2023-03-11 17:36:38 +00:00
}
}
}
#[derive(PartialEq, Eq, Debug, From)]
pub enum ClientPacket {
Iq(Iq<IqClientBody>),
Message(Message),
Presence(Presence<Ignore>),
StreamEnd,
}
impl FromXml for ClientPacket {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
match event {
Event::Start(bytes) => {
let name = bytes.name();
match_parser!(name, namespace, event;
Iq::<IqClientBody>,
Presence::<Ignore>,
Message,
{
Err(ffail!(
"Unexpected XML event of name {:?} in namespace {:?}",
name,
namespace
))
}
)
}
Event::End(bytes) => {
let name = bytes.name();
if name.local_name().as_ref() == b"stream" {
return Ok(ClientPacket::StreamEnd);
} else {
return Err(ffail!("Unexpected XML event: {event:?}"));
}
}
_ => {
return Err(ffail!("Unexpected XML event: {event:?}"));
}
}
}
}
}