forked from lavina/lavina
72 lines
2.4 KiB
Rust
72 lines
2.4 KiB
Rust
use derive_more::From;
|
|
use quick_xml::events::Event;
|
|
use quick_xml::name::{Namespace, ResolveResult};
|
|
|
|
use crate::protos::xmpp::bind::BindRequest;
|
|
use crate::util::xml::{Continuation, FromXml, Parser};
|
|
|
|
use crate::prelude::*;
|
|
|
|
#[derive(PartialEq, Eq, Debug, From)]
|
|
pub enum IqClientBody {
|
|
Bind(BindRequest),
|
|
}
|
|
|
|
#[derive(From)]
|
|
pub struct IqClientBodyParser(IqClientBodyParserInner);
|
|
|
|
#[derive(From)]
|
|
enum IqClientBodyParserInner {
|
|
Initial,
|
|
Bind(<BindRequest as FromXml>::P),
|
|
}
|
|
|
|
impl FromXml for IqClientBody {
|
|
type P = IqClientBodyParser;
|
|
|
|
fn parse() -> Self::P {
|
|
IqClientBodyParserInner::Initial.into()
|
|
}
|
|
}
|
|
|
|
impl Parser for IqClientBodyParser {
|
|
type Output = Result<IqClientBody>;
|
|
|
|
fn consume<'a>(
|
|
self: Self,
|
|
namespace: quick_xml::name::ResolveResult,
|
|
event: &quick_xml::events::Event<'a>,
|
|
) -> crate::util::xml::Continuation<Self, Self::Output> {
|
|
use IqClientBodyParserInner::*;
|
|
match self.0 {
|
|
Initial => {
|
|
let Event::Start(bytes) = event else {
|
|
return Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}")));
|
|
};
|
|
if bytes.name().0 == BindRequest::NAME.as_bytes()
|
|
&& namespace == ResolveResult::Bound(Namespace(BindRequest::NS.as_bytes()))
|
|
{
|
|
match BindRequest::parse().consume(namespace, event) {
|
|
Continuation::Final(Ok(r)) => Continuation::Final(Ok(r.into())),
|
|
Continuation::Final(Err(e)) => Continuation::Final(Err(e)),
|
|
Continuation::Continue(s) => {
|
|
let inner: IqClientBodyParserInner = s.into();
|
|
Continuation::Continue(inner.into())
|
|
}
|
|
}
|
|
} else {
|
|
Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}")))
|
|
}
|
|
}
|
|
Bind(p) => match p.consume(namespace, event) {
|
|
Continuation::Final(Ok(r)) => Continuation::Final(Ok(r.into())),
|
|
Continuation::Final(Err(e)) => Continuation::Final(Err(e)),
|
|
Continuation::Continue(s) => {
|
|
let inner: IqClientBodyParserInner = s.into();
|
|
Continuation::Continue(inner.into())
|
|
}
|
|
},
|
|
}
|
|
}
|
|
}
|