xmpp: use mutable namespace and event in parser coroutines

This commit is contained in:
Nikita Vilunov 2024-04-28 17:19:31 +02:00
parent 8ec9ecfe2c
commit c69513f38b
7 changed files with 40 additions and 40 deletions

View File

@ -25,7 +25,7 @@ impl FromXml for IqClientBody {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let bytes = match event {
Event::Start(bytes) => bytes,
Event::Empty(bytes) => bytes,
@ -59,7 +59,7 @@ impl FromXml for ClientPacket {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
match event {
Event::Start(bytes) | Event::Empty(bytes) => {
let name = bytes.name();

View File

@ -82,7 +82,7 @@ impl FromXml for BindRequest {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let mut resource: Option<Str> = None;
let Event::Start(bytes) = event else {
return Err(anyhow!("Unexpected XML event: {event:?}"));
@ -97,15 +97,15 @@ impl FromXml for BindRequest {
return Err(anyhow!("Incorrect namespace"));
}
loop {
let (namespace, event) = yield;
(namespace, event) = yield;
match event {
Event::Start(bytes) if bytes.name().0 == b"resource" => {
let (namespace, event) = yield;
(namespace, event) = yield;
let Event::Text(text) = event else {
return Err(anyhow!("Unexpected XML event: {event:?}"));
};
resource = Some(std::str::from_utf8(&*text)?.into());
let (namespace, event) = yield;
(namespace, event) = yield;
let Event::End(bytes) = event else {
return Err(anyhow!("Unexpected XML event: {event:?}"));
};

View File

@ -378,7 +378,7 @@ impl<T: FromXml> Parser for IqParser<T> {
}
},
IqParserInner::Final(state) => {
if let Event::End(ref bytes) = event {
if let Event::End(_) = event {
let id = fail_fast!(state.id.ok_or_else(|| ffail!("No id provided")));
let r#type = fail_fast!(state.r#type.ok_or_else(|| ffail!("No type provided")));
let body = fail_fast!(state.body.ok_or_else(|| ffail!("No body provided")));
@ -528,7 +528,7 @@ impl<T: FromXml> FromXml for Presence<T> {
type P = impl Parser<Output = Result<Presence<T>>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let (bytes, end) = match event {
Event::Start(bytes) => (bytes, false),
Event::Empty(bytes) => (bytes, true),
@ -557,37 +557,37 @@ impl<T: FromXml> FromXml for Presence<T> {
return Ok(p);
}
loop {
let (namespace, event) = yield;
(namespace, event) = yield;
match event {
Event::Start(bytes) => match bytes.name().0 {
b"show" => {
let (_, event) = yield;
(namespace, event) = yield;
let Event::Text(bytes) = event else {
return Err(ffail!("Unexpected XML event: {event:?}"));
};
let i = PresenceShow::from_str(bytes)?;
p.show = Some(i);
let (_, event) = yield;
(namespace, event) = yield;
let Event::End(_) = event else {
return Err(ffail!("Unexpected XML event: {event:?}"));
};
}
b"status" => {
let (_, event) = yield;
(namespace, event) = yield;
let Event::Text(bytes) = event else {
return Err(ffail!("Unexpected XML event: {event:?}"));
};
let s = std::str::from_utf8(bytes)?;
p.status.push(s.to_string());
let (_, event) = yield;
(namespace, event) = yield;
let Event::End(_) = event else {
return Err(ffail!("Unexpected XML event: {event:?}"));
};
}
b"priority" => {
let (_, event) = yield;
(namespace, event) = yield;
let Event::Text(bytes) = event else {
return Err(ffail!("Unexpected XML event: {event:?}"));
};
@ -595,7 +595,7 @@ impl<T: FromXml> FromXml for Presence<T> {
let i = s.parse()?;
p.priority = Some(PresencePriority(i));
let (_, event) = yield;
(namespace, event) = yield;
let Event::End(_) = event else {
return Err(ffail!("Unexpected XML event: {event:?}"));
};

View File

@ -21,7 +21,7 @@ impl FromXml for InfoQuery {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let mut node = None;
let mut identity = vec![];
let mut feature = vec![];
@ -48,7 +48,7 @@ impl FromXml for InfoQuery {
});
}
loop {
let (namespace, event) = yield;
(namespace, event) = yield;
let bytes = match event {
Event::Start(bytes) => bytes,
Event::Empty(bytes) => bytes,
@ -141,7 +141,7 @@ impl FromXml for Identity {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let mut category = None;
let mut name = None;
let mut r#type = None;
@ -179,8 +179,8 @@ impl FromXml for Identity {
return Ok(item);
}
let (namespace, event) = yield;
let Event::End(bytes) = event else {
(namespace, event) = yield;
let Event::End(_) = event else {
return Err(ffail!("Unexpected XML event: {event:?}"));
};
Ok(item)
@ -209,7 +209,7 @@ impl FromXml for Feature {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let mut var = None;
let (bytes, end) = match event {
Event::Start(bytes) => (bytes, false),
@ -234,8 +234,8 @@ impl FromXml for Feature {
return Ok(item);
}
let (namespace, event) = yield;
let Event::End(bytes) = event else {
(namespace, event) = yield;
let Event::End(_) = event else {
return Err(ffail!("Unexpected XML event: {event:?}"));
};
Ok(item)
@ -258,9 +258,9 @@ impl FromXml for ItemQuery {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let mut item = vec![];
let (bytes, end) = match event {
let (_, end) = match event {
Event::Start(bytes) => (bytes, false),
Event::Empty(bytes) => (bytes, true),
_ => return Err(ffail!("Unexpected XML event: {event:?}")),
@ -269,7 +269,7 @@ impl FromXml for ItemQuery {
return Ok(ItemQuery { item });
}
loop {
let (namespace, event) = yield;
(namespace, event) = yield;
let bytes = match event {
Event::Start(bytes) => bytes,
Event::Empty(bytes) => bytes,
@ -296,7 +296,7 @@ impl FromXmlTag for ItemQuery {
impl ToXml for ItemQuery {
fn serialize(&self, events: &mut Vec<Event<'static>>) {
let mut bytes = BytesStart::new(format!(r#"query xmlns="{}""#, XMLNS_ITEM));
let bytes = BytesStart::new(format!(r#"query xmlns="{}""#, XMLNS_ITEM));
let empty = self.item.is_empty();
if empty {
events.push(Event::Empty(bytes));
@ -342,7 +342,7 @@ impl FromXml for Item {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|(_, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let mut jid = None;
let mut name = None;
let mut node = None;
@ -378,8 +378,8 @@ impl FromXml for Item {
return Ok(item);
}
let (namespace, event) = yield;
let Event::End(bytes) = event else {
(_, event) = yield;
let Event::End(_) = event else {
return Err(ffail!("Unexpected XML event: {event:?}"));
};
Ok(item)

View File

@ -19,7 +19,7 @@ impl FromXml for History {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let mut history = History::default();
let (bytes, end) = match event {
Event::Start(bytes) if bytes.name().0 == Self::NAME.as_bytes() => (bytes, false),
@ -51,7 +51,7 @@ impl FromXml for History {
return Ok(history);
}
let (namespace, event) = yield;
(namespace, event) = yield;
let Event::End(bytes) = event else {
return Err(anyhow!("Unexpected XML event: {event:?}"));
};
@ -73,17 +73,17 @@ impl FromXml for Password {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let bytes = match event {
Event::Start(bytes) if bytes.name().0 == Self::NAME.as_bytes() => bytes,
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
};
let (namespace, event) = yield;
(namespace, event) = yield;
let Event::Text(bytes) = event else {
return Err(anyhow!("Unexpected XML event: {event:?}"));
};
let s = std::str::from_utf8(bytes)?.to_string();
let (namespace, event) = yield;
(namespace, event) = yield;
let Event::End(bytes) = event else {
return Err(anyhow!("Unexpected XML event: {event:?}"));
};
@ -108,7 +108,7 @@ impl FromXml for X {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
|(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let mut res = X::default();
let (_, end) = match event {
Event::Start(bytes) => (bytes, false),
@ -120,7 +120,7 @@ impl FromXml for X {
}
loop {
let (namespace, event) = yield;
(namespace, event) = yield;
let bytes = match event {
Event::Start(bytes) => bytes,
Event::Empty(bytes) => bytes,

View File

@ -89,8 +89,8 @@ macro_rules! delegate_parsing {
Continuation::Final(Ok(res)) => break Ok(res.into()),
Continuation::Final(Err(err)) => break Err(err),
Continuation::Continue(p) => {
let (namespace, event) = yield;
parser = p.consume(namespace, event);
($namespace, $event) = yield;
parser = p.consume($namespace, $event);
}
}
}

View File

@ -109,7 +109,7 @@ fn set_up_logging(tracing_config: &Option<TracingConfig>) -> Result<()> {
let targets = {
use std::{env, str::FromStr};
use tracing_subscriber::{filter::Targets, layer::SubscriberExt};
use tracing_subscriber::filter::Targets;
match env::var("RUST_LOG") {
Ok(var) => Targets::from_str(&var)
.map_err(|e| {