forked from lavina/lavina
1
0
Fork 0
lavina/crates/proto-xmpp/src/session.rs

63 lines
1.7 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 {
Event::Start(_) => {
Continuation::Continue(SessionParser(SessionParserInner::InSession))
}
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>>) {
events.push(Event::Empty(BytesStart::new(format!(
r#"session xmlns="{}""#,
XMLNS
))));
}
}