connection: Adapt C2S and Component2S impl for quickxml; Thanks eijebong

This commit is contained in:
Maxime “pep” Buquet 2017-12-28 04:31:40 +01:00
parent 5e5086e2ef
commit 4b322cc62b

View file

@ -2,7 +2,7 @@ use transport::Transport;
use error::Error; use error::Error;
use ns; use ns;
use xml::writer::XmlEvent as WriterEvent; use quick_xml::events::{Event as WriterEvent, BytesStart, BytesEnd};
pub trait Connection { pub trait Connection {
type InitError; type InitError;
@ -23,17 +23,19 @@ impl Connection for C2S {
fn namespace() -> &'static str { ns::CLIENT } fn namespace() -> &'static str { ns::CLIENT }
fn init<T: Transport>(transport: &mut T, domain: &str, id: &str) -> Result<(), Error> { fn init<T: Transport>(transport: &mut T, domain: &str, id: &str) -> Result<(), Error> {
transport.write_event(WriterEvent::start_element("stream:stream") let name = "stream:stream";
.attr("to", domain) let mut elem = BytesStart::borrowed(name.as_bytes(), name.len());
.attr("id", id) elem.push_attribute(("to", domain));
.default_ns(ns::CLIENT) elem.push_attribute(("id", id));
.ns("stream", ns::STREAM))?; elem.push_attribute(("xmlns", ns::CLIENT));
Ok(()) elem.push_attribute(("xmlns:stream", ns::STREAM));
transport.write_event(WriterEvent::Start(elem))
} }
fn close<T: Transport>(transport: &mut T) -> Result<(), Error> { fn close<T: Transport>(transport: &mut T) -> Result<(), Error> {
transport.write_event(WriterEvent::end_element())?; let name = "stream:stream";
Ok(()) let elem = BytesEnd::borrowed(name.as_bytes());
transport.write_event(WriterEvent::End(elem))
} }
} }
@ -46,16 +48,18 @@ impl Connection for Component2S {
fn namespace() -> &'static str { ns::COMPONENT_ACCEPT } fn namespace() -> &'static str { ns::COMPONENT_ACCEPT }
fn init<T: Transport>(transport: &mut T, domain: &str, id: &str) -> Result<(), Error> { fn init<T: Transport>(transport: &mut T, domain: &str, id: &str) -> Result<(), Error> {
transport.write_event(WriterEvent::start_element("stream:stream") let name = "stream:stream";
.attr("to", domain) let mut elem = BytesStart::borrowed(name.as_bytes(), name.len());
.attr("id", id) elem.push_attribute(("to", domain));
.default_ns(ns::COMPONENT_ACCEPT) elem.push_attribute(("id", id));
.ns("stream", ns::STREAM))?; elem.push_attribute(("xmlns", ns::COMPONENT_ACCEPT));
Ok(()) elem.push_attribute(("xmlns:stream", ns::STREAM));
transport.write_event(WriterEvent::Start(elem))
} }
fn close<T: Transport>(transport: &mut T) -> Result<(), Error> { fn close<T: Transport>(transport: &mut T) -> Result<(), Error> {
transport.write_event(WriterEvent::end_element())?; let name = "stream:stream";
Ok(()) let elem = BytesEnd::borrowed(name.as_bytes());
transport.write_event(WriterEvent::End(elem))
} }
} }