ibb: Split the stupid enum into three different structs.

This commit is contained in:
Emmanuel Gil Peyrot 2017-07-29 03:44:35 +01:00
parent dfb736a973
commit db35d28c9c
2 changed files with 113 additions and 110 deletions

View file

@ -20,90 +20,104 @@ generate_attribute!(Stanza, "stanza", {
}, Default = Iq); }, Default = Iq);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum IBB { pub struct Open {
Open { pub block_size: u16,
block_size: u16, pub sid: String,
sid: String, pub stanza: Stanza,
stanza: Stanza,
},
Data {
seq: u16,
sid: String,
data: Vec<u8>,
},
Close {
sid: String,
},
} }
impl TryFrom<Element> for IBB { impl TryFrom<Element> for Open {
type Err = Error; type Err = Error;
fn try_from(elem: Element) -> Result<IBB, Error> { fn try_from(elem: Element) -> Result<Open, Error> {
if elem.is("open", ns::IBB) { if !elem.is("open", ns::IBB) {
return Err(Error::ParseError("This is not an open element."));
}
for _ in elem.children() { for _ in elem.children() {
return Err(Error::ParseError("Unknown child in open element.")); return Err(Error::ParseError("Unknown child in open element."));
} }
let block_size = get_attr!(elem, "block-size", required); Ok(Open {
let sid = get_attr!(elem, "sid", required); block_size: get_attr!(elem, "block-size", required),
let stanza = get_attr!(elem, "stanza", default); sid: get_attr!(elem, "sid", required),
Ok(IBB::Open { stanza: get_attr!(elem, "stanza", default),
block_size: block_size,
sid: sid,
stanza: stanza
}) })
} else if elem.is("data", ns::IBB) {
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in data element."));
}
let seq = get_attr!(elem, "seq", required);
let sid = get_attr!(elem, "sid", required);
let data = base64::decode(&elem.text())?;
Ok(IBB::Data {
seq: seq,
sid: sid,
data: data
})
} else if elem.is("close", ns::IBB) {
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in close element."));
}
let sid = get_attr!(elem, "sid", required);
Ok(IBB::Close {
sid: sid,
})
} else {
Err(Error::ParseError("This is not an ibb element."))
}
} }
} }
impl From<IBB> for Element { impl From<Open> for Element {
fn from(ibb: IBB) -> Element { fn from(open: Open) -> Element {
match ibb {
IBB::Open { block_size, sid, stanza } => {
Element::builder("open") Element::builder("open")
.ns(ns::IBB) .ns(ns::IBB)
.attr("block-size", block_size) .attr("block-size", open.block_size)
.attr("sid", sid) .attr("sid", open.sid)
.attr("stanza", stanza) .attr("stanza", open.stanza)
.build() .build()
}, }
IBB::Data { seq, sid, data } => { }
#[derive(Debug, Clone)]
pub struct Data {
pub seq: u16,
pub sid: String,
pub data: Vec<u8>,
}
impl TryFrom<Element> for Data {
type Err = Error;
fn try_from(elem: Element) -> Result<Data, Error> {
if !elem.is("data", ns::IBB) {
return Err(Error::ParseError("This is not a data element."));
}
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in data element."));
}
Ok(Data {
seq: get_attr!(elem, "seq", required),
sid: get_attr!(elem, "sid", required),
data: base64::decode(&elem.text())?,
})
}
}
impl From<Data> for Element {
fn from(data: Data) -> Element {
Element::builder("data") Element::builder("data")
.ns(ns::IBB) .ns(ns::IBB)
.attr("seq", seq) .attr("seq", data.seq)
.attr("sid", sid) .attr("sid", data.sid)
.append(base64::encode(&data)) .append(base64::encode(&data.data))
.build() .build()
}, }
IBB::Close { sid } => { }
#[derive(Debug, Clone)]
pub struct Close {
pub sid: String,
}
impl TryFrom<Element> for Close {
type Err = Error;
fn try_from(elem: Element) -> Result<Close, Error> {
if !elem.is("close", ns::IBB) {
return Err(Error::ParseError("This is not a close element."));
}
for _ in elem.children() {
return Err(Error::ParseError("Unknown child in close element."));
}
Ok(Close {
sid: get_attr!(elem, "sid", required),
})
}
}
impl From<Close> for Element {
fn from(close: Close) -> Element {
Element::builder("close") Element::builder("close")
.ns(ns::IBB) .ns(ns::IBB)
.attr("sid", sid) .attr("sid", close.sid)
.build() .build()
},
}
} }
} }
@ -115,41 +129,26 @@ mod tests {
#[test] #[test]
fn test_simple() { fn test_simple() {
let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='3' sid='coucou'/>".parse().unwrap(); let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='3' sid='coucou'/>".parse().unwrap();
let open = IBB::try_from(elem).unwrap(); let open = Open::try_from(elem).unwrap();
match open { assert_eq!(open.block_size, 3);
IBB::Open { block_size, sid, stanza } => { assert_eq!(open.sid, "coucou");
assert_eq!(block_size, 3); assert_eq!(open.stanza, Stanza::Iq);
assert_eq!(sid, "coucou");
assert_eq!(stanza, Stanza::Iq);
},
_ => panic!(),
}
let elem: Element = "<data xmlns='http://jabber.org/protocol/ibb' seq='0' sid='coucou'>AAAA</data>".parse().unwrap(); let elem: Element = "<data xmlns='http://jabber.org/protocol/ibb' seq='0' sid='coucou'>AAAA</data>".parse().unwrap();
let data = IBB::try_from(elem).unwrap(); let data = Data::try_from(elem).unwrap();
match data { assert_eq!(data.seq, 0);
IBB::Data { seq, sid, data } => { assert_eq!(data.sid, "coucou");
assert_eq!(seq, 0); assert_eq!(data.data, vec!(0, 0, 0));
assert_eq!(sid, "coucou");
assert_eq!(data, vec!(0, 0, 0));
},
_ => panic!(),
}
let elem: Element = "<close xmlns='http://jabber.org/protocol/ibb' sid='coucou'/>".parse().unwrap(); let elem: Element = "<close xmlns='http://jabber.org/protocol/ibb' sid='coucou'/>".parse().unwrap();
let close = IBB::try_from(elem).unwrap(); let close = Close::try_from(elem).unwrap();
match close { assert_eq!(close.sid, "coucou");
IBB::Close { sid } => {
assert_eq!(sid, "coucou");
},
_ => panic!(),
}
} }
#[test] #[test]
fn test_invalid() { fn test_invalid() {
let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb'/>".parse().unwrap(); let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb'/>".parse().unwrap();
let error = IBB::try_from(elem).unwrap_err(); let error = Open::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -157,7 +156,7 @@ mod tests {
assert_eq!(message, "Required attribute 'block-size' missing."); assert_eq!(message, "Required attribute 'block-size' missing.");
let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='-5'/>".parse().unwrap(); let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='-5'/>".parse().unwrap();
let error = IBB::try_from(elem).unwrap_err(); let error = Open::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseIntError(error) => error, Error::ParseIntError(error) => error,
_ => panic!(), _ => panic!(),
@ -165,7 +164,7 @@ mod tests {
assert_eq!(message.description(), "invalid digit found in string"); assert_eq!(message.description(), "invalid digit found in string");
let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128'/>".parse().unwrap(); let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128'/>".parse().unwrap();
let error = IBB::try_from(elem).unwrap_err(); let error = Open::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(error) => error, Error::ParseError(error) => error,
_ => panic!(), _ => panic!(),
@ -176,7 +175,7 @@ mod tests {
#[test] #[test]
fn test_invalid_stanza() { fn test_invalid_stanza() {
let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128' sid='coucou' stanza='fdsq'/>".parse().unwrap(); let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128' sid='coucou' stanza='fdsq'/>".parse().unwrap();
let error = IBB::try_from(elem).unwrap_err(); let error = Open::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),

View file

@ -19,7 +19,7 @@ use ns;
use stanza_error::StanzaError; use stanza_error::StanzaError;
use roster::Roster; use roster::Roster;
use disco::{DiscoInfoResult, DiscoInfoQuery}; use disco::{DiscoInfoResult, DiscoInfoQuery};
use ibb::IBB; use ibb::{Open as IbbOpen, Data as IbbData, Close as IbbClose};
use jingle::Jingle; use jingle::Jingle;
use ping::Ping; use ping::Ping;
use mam::{Query as MamQuery, Fin as MamFin, Prefs as MamPrefs}; use mam::{Query as MamQuery, Fin as MamFin, Prefs as MamPrefs};
@ -40,7 +40,9 @@ pub enum IqGetPayload {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum IqSetPayload { pub enum IqSetPayload {
Roster(Roster), Roster(Roster),
IBB(IBB), IbbOpen(IbbOpen),
IbbData(IbbData),
IbbClose(IbbClose),
Jingle(Jingle), Jingle(Jingle),
MamQuery(MamQuery), MamQuery(MamQuery),
MamPrefs(MamPrefs), MamPrefs(MamPrefs),
@ -106,9 +108,9 @@ impl TryFrom<Element> for IqSetPayload {
("query", ns::ROSTER) => IqSetPayload::Roster(Roster::try_from(elem)?), ("query", ns::ROSTER) => IqSetPayload::Roster(Roster::try_from(elem)?),
// XEP-0047 // XEP-0047
("open", ns::IBB) ("open", ns::IBB) => IqSetPayload::IbbOpen(IbbOpen::try_from(elem)?),
| ("data", ns::IBB) ("data", ns::IBB) => IqSetPayload::IbbData(IbbData::try_from(elem)?),
| ("close", ns::IBB) => IqSetPayload::IBB(IBB::try_from(elem)?), ("close", ns::IBB) => IqSetPayload::IbbClose(IbbClose::try_from(elem)?),
// XEP-0166 // XEP-0166
("jingle", ns::JINGLE) => IqSetPayload::Jingle(Jingle::try_from(elem)?), ("jingle", ns::JINGLE) => IqSetPayload::Jingle(Jingle::try_from(elem)?),
@ -126,7 +128,9 @@ impl From<IqSetPayload> for Element {
fn from(payload: IqSetPayload) -> Element { fn from(payload: IqSetPayload) -> Element {
match payload { match payload {
IqSetPayload::Roster(roster) => roster.into(), IqSetPayload::Roster(roster) => roster.into(),
IqSetPayload::IBB(ibb) => ibb.into(), IqSetPayload::IbbOpen(open) => open.into(),
IqSetPayload::IbbData(data) => data.into(),
IqSetPayload::IbbClose(close) => close.into(),
IqSetPayload::Jingle(jingle) => jingle.into(), IqSetPayload::Jingle(jingle) => jingle.into(),
IqSetPayload::MamQuery(query) => query.into(), IqSetPayload::MamQuery(query) => query.into(),
IqSetPayload::MamPrefs(prefs) => prefs.into(), IqSetPayload::MamPrefs(prefs) => prefs.into(),