forked from lavina/lavina
feat(xmpp): iq body ADT with parsing
This commit is contained in:
parent
f131454cb2
commit
443f6a2c90
|
@ -95,6 +95,12 @@ version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "convert_case"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cpufeatures"
|
name = "cpufeatures"
|
||||||
version = "0.2.5"
|
version = "0.2.5"
|
||||||
|
@ -114,6 +120,19 @@ dependencies = [
|
||||||
"typenum",
|
"typenum",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "derive_more"
|
||||||
|
version = "0.99.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321"
|
||||||
|
dependencies = [
|
||||||
|
"convert_case",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"rustc_version",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "digest"
|
name = "digest"
|
||||||
version = "0.10.6"
|
version = "0.10.6"
|
||||||
|
@ -425,6 +444,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"assert_matches",
|
"assert_matches",
|
||||||
|
"derive_more",
|
||||||
"figment",
|
"figment",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"http-body-util",
|
"http-body-util",
|
||||||
|
@ -776,6 +796,15 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustc_version"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
|
||||||
|
dependencies = [
|
||||||
|
"semver",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustls"
|
name = "rustls"
|
||||||
version = "0.20.8"
|
version = "0.20.8"
|
||||||
|
@ -819,6 +848,12 @@ dependencies = [
|
||||||
"untrusted",
|
"untrusted",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "semver"
|
||||||
|
version = "1.0.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.152"
|
version = "1.0.152"
|
||||||
|
|
|
@ -21,6 +21,7 @@ nom = "7.1.3"
|
||||||
tokio-rustls = "0.23.4"
|
tokio-rustls = "0.23.4"
|
||||||
rustls-pemfile = "1.0.2"
|
rustls-pemfile = "1.0.2"
|
||||||
quick-xml = { version = "0.27.1", features = ["async-tokio"] }
|
quick-xml = { version = "0.27.1", features = ["async-tokio"] }
|
||||||
|
derive_more = "0.99.17"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
assert_matches = "1.5.0"
|
assert_matches = "1.5.0"
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
mod proto;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader as SyncBufReader;
|
use std::io::BufReader as SyncBufReader;
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
use derive_more::From;
|
||||||
|
use quick_xml::events::Event;
|
||||||
|
use quick_xml::name::{Namespace, ResolveResult};
|
||||||
|
|
||||||
|
use crate::protos::xmpp::bind::BindRequest;
|
||||||
|
use crate::util::xml::{Continuation, FromXml, Parser};
|
||||||
|
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, Debug, From)]
|
||||||
|
pub enum IqClientBody {
|
||||||
|
Bind(BindRequest),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(From)]
|
||||||
|
pub struct IqClientBodyParser(IqClientBodyParserInner);
|
||||||
|
|
||||||
|
#[derive(From)]
|
||||||
|
enum IqClientBodyParserInner {
|
||||||
|
Initial,
|
||||||
|
Bind(<BindRequest as FromXml>::P),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromXml for IqClientBody {
|
||||||
|
type P = IqClientBodyParser;
|
||||||
|
|
||||||
|
fn parse() -> Self::P {
|
||||||
|
IqClientBodyParserInner::Initial.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 Event::Start(bytes) = event else {
|
||||||
|
return Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}")));
|
||||||
|
};
|
||||||
|
if bytes.name().0 == BindRequest::NAME.as_bytes()
|
||||||
|
&& namespace == ResolveResult::Bound(Namespace(BindRequest::NS.as_bytes()))
|
||||||
|
{
|
||||||
|
match BindRequest::parse().consume(namespace, event) {
|
||||||
|
Continuation::Final(Ok(r)) => Continuation::Final(Ok(r.into())),
|
||||||
|
Continuation::Final(Err(e)) => Continuation::Final(Err(e)),
|
||||||
|
Continuation::Continue(s) => {
|
||||||
|
let inner: IqClientBodyParserInner = s.into();
|
||||||
|
Continuation::Continue(inner.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}")))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Bind(p) => match p.consume(namespace, event) {
|
||||||
|
Continuation::Final(Ok(r)) => Continuation::Final(Ok(r.into())),
|
||||||
|
Continuation::Final(Err(e)) => Continuation::Final(Err(e)),
|
||||||
|
Continuation::Continue(s) => {
|
||||||
|
let inner: IqClientBodyParserInner = s.into();
|
||||||
|
Continuation::Continue(inner.into())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -47,6 +47,11 @@ enum BindRequestParserInner {
|
||||||
InBindResourceEnd(String),
|
InBindResourceEnd(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl BindRequest {
|
||||||
|
pub const NS: &'static str = XMLNS;
|
||||||
|
pub const NAME: &'static str = "bind";
|
||||||
|
}
|
||||||
|
|
||||||
impl FromXml for BindRequest {
|
impl FromXml for BindRequest {
|
||||||
type P = BindRequestParser;
|
type P = BindRequestParser;
|
||||||
|
|
||||||
|
@ -70,7 +75,7 @@ impl Parser for BindRequestParser {
|
||||||
let Event::Start(bytes) = event else {
|
let Event::Start(bytes) = event else {
|
||||||
return Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}")));
|
return Continuation::Final(Err(ffail!("Unexpected XML event: {event:?}")));
|
||||||
};
|
};
|
||||||
if bytes.name().0 != b"bind" {
|
if bytes.name().0 != BindRequest::NAME.as_bytes() {
|
||||||
return Continuation::Final(Err(ffail!(
|
return Continuation::Final(Err(ffail!(
|
||||||
"Unexpected XML tag: {:?}",
|
"Unexpected XML tag: {:?}",
|
||||||
bytes.name()
|
bytes.name()
|
||||||
|
|
Loading…
Reference in New Issue