forked from lavina/lavina
1
0
Fork 0

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>>; type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P { 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 { let bytes = match event {
Event::Start(bytes) => bytes, Event::Start(bytes) => bytes,
Event::Empty(bytes) => bytes, Event::Empty(bytes) => bytes,
@ -59,7 +59,7 @@ impl FromXml for ClientPacket {
type P = impl Parser<Output = Result<Self>>; type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P { 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 { match event {
Event::Start(bytes) | Event::Empty(bytes) => { Event::Start(bytes) | Event::Empty(bytes) => {
let name = bytes.name(); let name = bytes.name();

View File

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

View File

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

View File

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

View File

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

View File

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