forked from lavina/lavina
63 lines
1.7 KiB
Rust
63 lines
1.7 KiB
Rust
use quick_xml::events::{BytesStart, Event};
|
|
|
|
use crate::xml::*;
|
|
use anyhow::{anyhow, Result};
|
|
|
|
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:?}"))),
|
|
},
|
|
SessionParserInner::InSession => match event {
|
|
Event::End(_) => Continuation::Final(Ok(Session)),
|
|
_ => Continuation::Final(Err(anyhow!("Unexpected XML event: {event:?}"))),
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
))));
|
|
}
|
|
}
|