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

58 lines
1.6 KiB
Rust
Raw Normal View History

2023-03-12 13:15:13 +00:00
use quick_xml::events::{BytesStart, Event};
use crate::xml::*;
use anyhow::{anyhow, Result};
2023-03-12 13:15:13 +00:00
pub const XMLNS: &'static str = "urn:ietf:params:xml:ns:xmpp-session";
#[derive(PartialEq, Eq, Debug)]
pub struct Session;
pub struct SessionParser(SessionParserInner);
enum SessionParserInner {
Initial,
InSession,
}
impl Parser for SessionParser {
type Output = Result<Session>;
fn consume<'a>(
self: Self,
namespace: quick_xml::name::ResolveResult,
event: &quick_xml::events::Event<'a>,
) -> Continuation<Self, Self::Output> {
match self.0 {
SessionParserInner::Initial => match event {
2024-03-20 18:59:15 +00:00
Event::Start(_) => Continuation::Continue(SessionParser(SessionParserInner::InSession)),
2023-03-12 13:15:13 +00:00
Event::Empty(_) => Continuation::Final(Ok(Session)),
_ => Continuation::Final(Err(anyhow!("Unexpected XML event: {event:?}"))),
2023-03-12 13:15:13 +00:00
},
SessionParserInner::InSession => match event {
Event::End(_) => Continuation::Final(Ok(Session)),
_ => Continuation::Final(Err(anyhow!("Unexpected XML event: {event:?}"))),
2023-03-12 13:15:13 +00:00
},
}
}
}
impl FromXml for Session {
type P = SessionParser;
fn parse() -> Self::P {
SessionParser(SessionParserInner::Initial)
}
}
impl FromXmlTag for Session {
const NAME: &'static str = "session";
const NS: &'static str = XMLNS;
}
impl ToXml for Session {
fn serialize(&self, events: &mut Vec<Event<'static>>) {
2024-03-20 18:59:15 +00:00
events.push(Event::Empty(BytesStart::new(format!(r#"session xmlns="{}""#, XMLNS))));
2023-03-12 13:15:13 +00:00
}
}