forked from lavina/lavina
1
0
Fork 0

Compare commits

..

No commits in common. "b23c18a5ec40eba62923b75ce4668f4eb7398591" and "2d493814b990d8390fc49ffae7ee00aa4a0d0b78" have entirely different histories.

3 changed files with 35 additions and 65 deletions

View File

@ -328,7 +328,9 @@ impl<'a> Player<'a> {
} }
} }
PlayerCommand::Cmd(cmd, connection_id) => self.handle_cmd(cmd, connection_id).await, PlayerCommand::Cmd(cmd, connection_id) => self.handle_cmd(cmd, connection_id).await,
PlayerCommand::Stop => break, PlayerCommand::Stop => {
break;
}
} }
} }
log::debug!("Shutting down player actor #{:?}", self.player_id); log::debug!("Shutting down player actor #{:?}", self.player_id);

View File

@ -103,22 +103,14 @@ impl<T: FromXml> Parser for MessageParser<T> {
Continuation::Final(Err(ffail!("Expected start"))) Continuation::Final(Err(ffail!("Expected start")))
} }
} }
Outer(mut state) => match event { Outer(state) => match event {
Event::Start(ref bytes) => { Event::Start(ref bytes) => {
if bytes.name().0 == b"subject" { if bytes.name().0 == b"subject" {
Continuation::Continue(InSubject(state).into()) Continuation::Continue(InSubject(state).into())
} else if bytes.name().0 == b"body" { } else if bytes.name().0 == b"body" {
Continuation::Continue(InBody(state).into()) Continuation::Continue(InBody(state).into())
} else { } else {
let parser = T::parse(); Continuation::Continue(InCustom(state, T::parse()).into())
match parser.consume(namespace, event) {
Continuation::Final(Ok(e)) => {
state.custom.push(e);
Continuation::Continue(Outer(state).into())
}
Continuation::Final(Err(e)) => Continuation::Final(Err(e)),
Continuation::Continue(p) => Continuation::Continue(InCustom(state, p).into()),
}
} }
} }
Event::End(_) => { Event::End(_) => {
@ -137,17 +129,6 @@ impl<T: FromXml> Parser for MessageParser<T> {
Continuation::Final(Err(ffail!("Body not found"))) Continuation::Final(Err(ffail!("Body not found")))
} }
} }
Event::Empty(_) => {
let parser = T::parse();
match parser.consume(namespace, event) {
Continuation::Final(Ok(e)) => {
state.custom.push(e);
Continuation::Continue(Outer(state).into())
}
Continuation::Final(Err(e)) => Continuation::Final(Err(e)),
Continuation::Continue(p) => Continuation::Continue(InCustom(state, p).into()),
}
}
_ => Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}"))), _ => Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}"))),
}, },
InSubject(mut state) => match event { InSubject(mut state) => match event {
@ -170,7 +151,7 @@ impl<T: FromXml> Parser for MessageParser<T> {
Event::End(_) => Continuation::Continue(Outer(state).into()), Event::End(_) => Continuation::Continue(Outer(state).into()),
_ => Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}"))), _ => Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}"))),
}, },
InCustom(mut state, custom) => match custom.consume(namespace, event) { InCustom(mut state, mut custom) => match custom.consume(namespace, event) {
Continuation::Final(Ok(e)) => { Continuation::Final(Ok(e)) => {
state.custom.push(e); state.custom.push(e);
Continuation::Continue(Outer(state).into()) Continuation::Continue(Outer(state).into())
@ -613,34 +594,25 @@ mod tests {
use crate::bind::{BindRequest, Name, Resource, Server}; use crate::bind::{BindRequest, Name, Resource, Server};
use super::*; use super::*;
use quick_xml::NsReader;
#[tokio::test] #[tokio::test]
async fn parse_message() { async fn parse_message() {
let input = r#"<message id="aacea" type="chat" to="nikita@vlnv.dev"><subject>daa</subject><body>bbb</body><unknown-stuff></unknown-stuff></message>"#; let input = r#"<message id="aacea" type="chat" to="nikita@vlnv.dev"><subject>daa</subject><body>bbb</body><unknown-stuff></unknown-stuff></message>"#;
let result: Message<Ignore> = crate::xml::parse(input).unwrap(); let mut reader = NsReader::from_reader(input.as_bytes());
assert_eq!( let mut buf = vec![];
result, let (ns, event) = reader.read_resolved_event_into_async(&mut buf).await.unwrap();
Message::<Ignore> { let mut parser = Message::parse().consume(ns, &event);
from: None, let result = loop {
id: Some("aacea".to_string()), match parser {
to: Some(Jid { Continuation::Final(res) => break res,
name: Some(Name("nikita".into())), Continuation::Continue(next) => {
server: Server("vlnv.dev".into()), let (ns, event) = reader.read_resolved_event_into_async(&mut buf).await.unwrap();
resource: None parser = next.consume(ns, &event);
}),
r#type: MessageType::Chat,
lang: None,
subject: Some("daa".into()),
body: "bbb".into(),
custom: vec![Ignore],
} }
)
} }
}
#[tokio::test] .unwrap();
async fn parse_message_empty_custom() {
let input = r#"<message id="aacea" type="chat" to="nikita@vlnv.dev"><subject>daa</subject><body>bbb</body><unknown-stuff/></message>"#;
let result: Message<Ignore> = crate::xml::parse(input).unwrap();
assert_eq!( assert_eq!(
result, result,
Message::<Ignore> { Message::<Ignore> {
@ -663,7 +635,20 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn parse_iq() { async fn parse_iq() {
let input = r#"<iq id="bind_1" type="set"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>mobile</resource></bind></iq>"#; let input = r#"<iq id="bind_1" type="set"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>mobile</resource></bind></iq>"#;
let result: Iq<BindRequest> = crate::xml::parse(input).unwrap(); let mut reader = NsReader::from_reader(input.as_bytes());
let mut buf = vec![];
let (ns, event) = reader.read_resolved_event_into_async(&mut buf).await.unwrap();
let mut parser = Iq::<BindRequest>::parse().consume(ns, &event);
let result = loop {
match parser {
Continuation::Final(res) => break res,
Continuation::Continue(next) => {
let (ns, event) = reader.read_resolved_event_into_async(&mut buf).await.unwrap();
parser = next.consume(ns, &event);
}
}
}
.unwrap();
assert_eq!( assert_eq!(
result, result,
Iq { Iq {

View File

@ -1,11 +1,10 @@
use std::ops::Generator; use std::ops::Generator;
use std::pin::Pin; use std::pin::Pin;
use quick_xml::NsReader;
use quick_xml::events::Event; use quick_xml::events::Event;
use quick_xml::name::ResolveResult; use quick_xml::name::ResolveResult;
use anyhow::Result; use anyhow::{anyhow, Result};
mod ignore; mod ignore;
pub use ignore::Ignore; pub use ignore::Ignore;
@ -64,22 +63,6 @@ pub enum Continuation<Parser, Res> {
Continue(Parser), Continue(Parser),
} }
pub fn parse<T: FromXml>(input: &str) -> Result<T> {
let mut reader = NsReader::from_reader(input.as_bytes());
let mut buf = vec![];
let (ns, event) = reader.read_resolved_event_into(&mut buf)?;
let mut parser: Continuation<_, std::result::Result<T, anyhow::Error>> = T::parse().consume(ns, &event);
loop {
match parser {
Continuation::Final(res) => break res,
Continuation::Continue(next) => {
let (ns, event) = reader.read_resolved_event_into(&mut buf)?;
parser = next.consume(ns, &event);
}
}
}
}
macro_rules! fail_fast { macro_rules! fail_fast {
($errorable: expr) => { ($errorable: expr) => {
match $errorable { match $errorable {