diff --git a/crates/proto-xmpp/src/roster.rs b/crates/proto-xmpp/src/roster.rs index 4e89981..f7d0305 100644 --- a/crates/proto-xmpp/src/roster.rs +++ b/crates/proto-xmpp/src/roster.rs @@ -1,47 +1,30 @@ use quick_xml::events::{BytesStart, Event}; use crate::xml::*; -use anyhow::{anyhow as ffail, Result}; +use anyhow::{anyhow, Result}; +use quick_xml::name::ResolveResult; pub const XMLNS: &'static str = "jabber:iq:roster"; #[derive(PartialEq, Eq, Debug)] pub struct RosterQuery; -pub struct QueryParser(QueryParserInner); - -enum QueryParserInner { - Initial, - InQuery, -} - -impl Parser for QueryParser { - type Output = Result; - - fn consume<'a>( - self: Self, - namespace: quick_xml::name::ResolveResult, - event: &quick_xml::events::Event<'a>, - ) -> Continuation { - match self.0 { - QueryParserInner::Initial => match event { - Event::Start(_) => Continuation::Continue(QueryParser(QueryParserInner::InQuery)), - Event::Empty(_) => Continuation::Final(Ok(RosterQuery)), - _ => Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}"))), - }, - QueryParserInner::InQuery => match event { - Event::End(_) => Continuation::Final(Ok(RosterQuery)), - _ => Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}"))), - }, - } - } -} - impl FromXml for RosterQuery { - type P = QueryParser; + type P = impl Parser>; fn parse() -> Self::P { - QueryParser(QueryParserInner::Initial) + |(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result { + match event { + Event::Start(_) => (), + Event::Empty(_) => return Ok(RosterQuery), + _ => return Err(anyhow!("Unexpected XML event: {event:?}")), + } + (namespace, event) = yield; + match event { + Event::End(_) => return Ok(RosterQuery), + _ => return Err(anyhow!("Unexpected XML event: {event:?}")), + } + } } } diff --git a/crates/proto-xmpp/src/session.rs b/crates/proto-xmpp/src/session.rs index 569742d..59d394d 100644 --- a/crates/proto-xmpp/src/session.rs +++ b/crates/proto-xmpp/src/session.rs @@ -2,46 +2,29 @@ use quick_xml::events::{BytesStart, Event}; use crate::xml::*; use anyhow::{anyhow, Result}; +use quick_xml::name::ResolveResult; pub const XMLNS: &'static str = "urn:ietf:params:xml:ns:xmpp-session"; #[derive(PartialEq, Eq, Debug)] pub struct Session; -pub struct SessionParser(SessionParserInner); - -enum SessionParserInner { - Initial, - InSession, -} - -impl Parser for SessionParser { - type Output = Result; - - fn consume<'a>( - self: Self, - namespace: quick_xml::name::ResolveResult, - event: &quick_xml::events::Event<'a>, - ) -> Continuation { - match self.0 { - SessionParserInner::Initial => match event { - Event::Start(_) => Continuation::Continue(SessionParser(SessionParserInner::InSession)), - Event::Empty(_) => Continuation::Final(Ok(Session)), - _ => Continuation::Final(Err(anyhow!("Unexpected XML event: {event:?}"))), - }, - SessionParserInner::InSession => match event { - Event::End(_) => Continuation::Final(Ok(Session)), - _ => Continuation::Final(Err(anyhow!("Unexpected XML event: {event:?}"))), - }, - } - } -} - impl FromXml for Session { - type P = SessionParser; + type P = impl Parser>; fn parse() -> Self::P { - SessionParser(SessionParserInner::Initial) + |(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result { + match event { + Event::Start(_) => (), + Event::Empty(_) => return Ok(Session), + _ => return Err(anyhow!("Unexpected XML event: {event:?}")), + } + (namespace, event) = yield; + match event { + Event::End(_) => return Ok(Session), + _ => return Err(anyhow!("Unexpected XML event: {event:?}")), + } + } } } diff --git a/crates/proto-xmpp/src/xml/ignore.rs b/crates/proto-xmpp/src/xml/ignore.rs index f4af358..fc89410 100644 --- a/crates/proto-xmpp/src/xml/ignore.rs +++ b/crates/proto-xmpp/src/xml/ignore.rs @@ -1,49 +1,35 @@ use super::*; -use derive_more::From; #[derive(Default, Debug, PartialEq, Eq)] pub struct Ignore; -#[derive(From)] -pub struct IgnoreParser(IgnoreParserInner); - -enum IgnoreParserInner { - Initial, - InTag { name: Vec, depth: u8 }, -} - -impl Parser for IgnoreParser { - type Output = Result; - - fn consume<'a>(self: Self, _: ResolveResult, event: &Event<'a>) -> Continuation { - match self.0 { - IgnoreParserInner::Initial => match event { - Event::Start(bytes) => { - let name = bytes.name().0.to_owned(); - Continuation::Continue(IgnoreParserInner::InTag { name, depth: 0 }.into()) - } - Event::Empty(_) => Continuation::Final(Ok(Ignore)), - _ => Continuation::Final(Ok(Ignore)), - }, - IgnoreParserInner::InTag { name, depth } => match event { - Event::End(bytes) if name == bytes.name().0 => { - if depth == 0 { - Continuation::Final(Ok(Ignore)) - } else { - Continuation::Continue(IgnoreParserInner::InTag { name, depth: depth - 1 }.into()) - } - } - _ => Continuation::Continue(IgnoreParserInner::InTag { name, depth }.into()), - }, - } - } -} - impl FromXml for Ignore { - type P = IgnoreParser; + type P = impl Parser>; fn parse() -> Self::P { - IgnoreParserInner::Initial.into() + |(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result { + let mut depth = match event { + Event::Start(bytes) => 0, + Event::Empty(_) => return Ok(Ignore), + _ => return Ok(Ignore), + }; + loop { + (namespace, event) = yield; + match event { + Event::End(_) => { + if depth == 0 { + return Ok(Ignore); + } else { + depth -= 1; + } + } + Event::Start(_) => { + depth += 1; + } + _ => (), + } + } + } } }