lavina/crates/proto-xmpp/src/mam.rs

72 lines
2.1 KiB
Rust
Raw Normal View History

2024-04-20 18:14:12 +00:00
use anyhow::{anyhow, Result};
use quick_xml::events::Event;
use quick_xml::name::{Namespace, ResolveResult};
use crate::xml::*;
pub const XMLNS: &'static str = "urn:xmpp:mam:2";
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct MessageArchiveRequest;
impl FromXmlTag for MessageArchiveRequest {
const NAME: &'static str = "query";
const NS: &'static str = XMLNS;
}
impl FromXml for MessageArchiveRequest {
type P = impl Parser<Output = Result<Self>>;
fn parse() -> Self::P {
|(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> {
let Event::Start(bytes) = event else {
return Err(anyhow!("Unexpected XML event: {event:?}"));
};
if bytes.name().0 != MessageArchiveRequest::NAME.as_bytes() {
return Err(anyhow!("Unexpected XML tag: {:?}", bytes.name()));
}
let ResolveResult::Bound(Namespace(ns)) = namespace else {
return Err(anyhow!("No namespace provided"));
};
if ns != XMLNS.as_bytes() {
return Err(anyhow!("Incorrect namespace"));
}
loop {
let (namespace, event) = yield;
match event {
Event::End(bytes) if bytes.name().0 == MessageArchiveRequest::NAME.as_bytes() => {
break;
}
_ => return Err(anyhow!("Unexpected XML event: {event:?}")),
}
}
Ok(MessageArchiveRequest)
}
}
}
impl MessageArchiveRequest {}
#[cfg(test)]
mod tests {
use super::*;
use crate::client::{Iq, IqType};
#[test]
fn test_parse() {
let input = r#"<query xmlns="urn:xmpp:mam:2"></query>"#;
let result: Iq<MessageArchiveRequest> = parse(input).unwrap();
assert_eq!(
result,
Iq {
from: None,
id: "mam_1".to_string(),
to: None,
r#type: IqType::Get,
body: MessageArchiveRequest,
}
);
}
}