generator-based parsing of xmpp stanzas

This commit is contained in:
Nikita Vilunov 2023-03-23 01:37:02 +01:00
parent bba1ea107d
commit d0f807841c
4 changed files with 80 additions and 105 deletions

1
rust-toolchain Normal file
View File

@ -0,0 +1 @@
nightly

View File

@ -1,3 +1,5 @@
#![feature(generators, generator_trait, type_alias_impl_trait)]
mod core; mod core;
mod prelude; mod prelude;
mod projections; mod projections;

View File

@ -18,59 +18,26 @@ pub enum IqClientBody {
Unknown(Ignore), Unknown(Ignore),
} }
#[derive(From)]
pub struct IqClientBodyParser(IqClientBodyParserInner);
#[derive(From)]
enum IqClientBodyParserInner {
Initial,
Bind(<BindRequest as FromXml>::P),
SessionV(<Session as FromXml>::P),
RosterV(<RosterQuery as FromXml>::P),
UnknownV(<Ignore as FromXml>::P),
}
impl FromXml for IqClientBody { impl FromXml for IqClientBody {
type P = IqClientBodyParser; type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P { fn parse() -> Self::P {
IqClientBodyParserInner::Initial.into() |(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
}
}
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 bytes = match event { let bytes = match event {
Event::Start(bytes) => bytes, Event::Start(bytes) => bytes,
Event::Empty(bytes) => bytes, Event::Empty(bytes) => bytes,
_ => { _ => return Err(ffail!("Unexpected XML event: {event:?}")),
return Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}")))
}
}; };
let name = bytes.name(); let name = bytes.name();
match_parser!(IqClientBodyParser, name, namespace, event; match_parser!(name, namespace, event;
BindRequest, BindRequest,
Session, Session,
RosterQuery, RosterQuery,
{ {
IqClientBodyParser(Ignore::parse().into()).consume(namespace, event) delegate_parsing!(Ignore, namespace, event).into()
} }
) )
} }
Bind(p) => delegate_parsing!(p, IqClientBodyParserInner, namespace, event),
SessionV(p) => delegate_parsing!(p, IqClientBodyParserInner, namespace, event),
RosterV(p) => delegate_parsing!(p, IqClientBodyParserInner, namespace, event),
UnknownV(p) => delegate_parsing!(p, IqClientBodyParserInner, namespace, event),
}
} }
} }
@ -81,52 +48,27 @@ pub enum ClientPacket {
Presence(Presence<Ignore>), Presence(Presence<Ignore>),
} }
#[derive(From)]
pub struct ClientPacketParser(ClientPacketParserInner);
impl FromXml for ClientPacket { impl FromXml for ClientPacket {
type P = ClientPacketParser; type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P { fn parse() -> Self::P {
ClientPacketParserInner::Initial.into() |(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
}
}
#[derive(From)]
enum ClientPacketParserInner {
Initial,
IqV(<Iq<IqClientBody> as FromXml>::P),
MessageV(<Message as FromXml>::P),
PresenceV(<Presence<Ignore> as FromXml>::P),
}
impl Parser for ClientPacketParser {
type Output = Result<ClientPacket>;
fn consume<'a>(
self: Self,
namespace: ResolveResult,
event: &Event<'a>,
) -> Continuation<Self, Self::Output> {
use ClientPacketParserInner::*;
match self.0 {
Initial => {
let Event::Start(bytes) = event else { let Event::Start(bytes) = event else {
return Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}"))); return Err(ffail!("Unexpected XML event: {event:?}"));
}; };
let name = bytes.name(); let name = bytes.name();
match_parser!(ClientPacketParser, name, namespace, event; match_parser!(name, namespace, event;
Iq::<IqClientBody>, Iq::<IqClientBody>,
Presence::<Ignore>, Presence::<Ignore>,
Message, Message,
{ {
Continuation::Final(Err(ffail!("Unexpected XML event of name {:?} in namespace {:?}", name, namespace))) Err(ffail!(
"Unexpected XML event of name {:?} in namespace {:?}",
name,
namespace
))
} }
) )
} }
IqV(p) => delegate_parsing!(p, ClientPacketParserInner, namespace, event),
MessageV(p) => delegate_parsing!(p, ClientPacketParserInner, namespace, event),
PresenceV(p) => delegate_parsing!(p, ClientPacketParserInner, namespace, event),
}
} }
} }

View File

@ -1,3 +1,6 @@
use std::ops::Generator;
use std::pin::Pin;
use quick_xml::events::Event; use quick_xml::events::Event;
use quick_xml::name::ResolveResult; use quick_xml::name::ResolveResult;
@ -31,6 +34,30 @@ pub trait Parser: Sized {
) -> Continuation<Self, Self::Output>; ) -> Continuation<Self, Self::Output>;
} }
impl<T, Out> Parser for T
where
T: Generator<(ResolveResult<'static>, &'static Event<'static>), Yield = (), Return = Out>
+ Unpin,
{
type Output = Out;
fn consume<'a>(
mut self: Self,
namespace: ResolveResult,
event: &Event<'a>,
) -> Continuation<Self, Self::Output> {
let s = Pin::new(&mut self);
// this is a very rude workaround fixing the fact that rust generators
// 1. don't support higher-kinded lifetimes (i.e. no `impl for <'a> Generator<Event<'a>>)
// 2. don't track borrows across yield points and lack thereof
// implementors of Parser should manually check that inputs are not used across yields
match s.resume(unsafe { std::mem::transmute((namespace, event)) }) {
std::ops::GeneratorState::Yielded(()) => Continuation::Continue(self),
std::ops::GeneratorState::Complete(res) => Continuation::Final(res),
}
}
}
pub enum Continuation<Parser, Res> { pub enum Continuation<Parser, Res> {
Final(Res), Final(Res),
Continue(Parser), Continue(Parser),
@ -46,31 +73,34 @@ macro_rules! fail_fast {
} }
macro_rules! delegate_parsing { macro_rules! delegate_parsing {
($parser: ident, $intermediate: ident, $namespace: expr, $event: expr) => { ($parser: ty, $namespace: expr, $event: expr) => {{
match $parser.consume($namespace, $event) { let mut parser = <$parser as FromXml>::parse().consume($namespace, $event);
Continuation::Final(Ok(r)) => Continuation::Final(Ok(r.into())), loop {
Continuation::Final(Err(e)) => Continuation::Final(Err(e)), match parser {
Continuation::Continue(s) => { Continuation::Final(Ok(res)) => break Ok(res.into()),
let inner: $intermediate = s.into(); Continuation::Final(Err(err)) => break Err(err),
Continuation::Continue(inner.into()) Continuation::Continue(p) => {
let (namespace, event) = yield;
parser = p.consume(namespace, event);
} }
} }
}; }
}};
} }
macro_rules! match_parser { macro_rules! match_parser {
($outer: ident, $name: expr, $ns: expr, $event: expr; $subtype: ty, $fin: block) => { ($name: expr, $ns: expr, $event: expr; $subtype: ty, $fin: block) => {
if $name.0 == <$subtype as FromXmlTag>::NAME.as_bytes() && $ns == ResolveResult::Bound(Namespace(<$subtype as FromXmlTag>::NS.as_bytes())) { if $name.0 == <$subtype as FromXmlTag>::NAME.as_bytes() && $ns == ResolveResult::Bound(Namespace(<$subtype as FromXmlTag>::NS.as_bytes())) {
$outer(<$subtype as FromXml>::parse().into()).consume($ns, $event) delegate_parsing!($subtype, $ns, $event)
} else { } else {
$fin $fin
} }
}; };
($outer: ident, $name: expr, $ns: expr, $event: expr; $subtype: ty, $($rest: ty),+, $fin: block) => { ($name: expr, $ns: expr, $event: expr; $subtype: ty, $($rest: ty),+, $fin: block) => {
if $name.0 == <$subtype as FromXmlTag>::NAME.as_bytes() && $ns == ResolveResult::Bound(Namespace(<$subtype as FromXmlTag>::NS.as_bytes())) { if $name.0 == <$subtype as FromXmlTag>::NAME.as_bytes() && $ns == ResolveResult::Bound(Namespace(<$subtype as FromXmlTag>::NS.as_bytes())) {
$outer(<$subtype as FromXml>::parse().into()).consume($ns, $event) delegate_parsing!($subtype, $ns, $event)
} else { } else {
match_parser!($outer, $name, $ns, $event; $($rest),*, $fin) match_parser!($name, $ns, $event; $($rest),*, $fin)
} }
}; };
} }