mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
message: Add a simpler way to create a Message.
This commit is contained in:
parent
96fe2d200e
commit
ad1d4adcec
1 changed files with 19 additions and 22 deletions
|
@ -127,6 +127,21 @@ pub struct Message {
|
||||||
pub payloads: Vec<Element>,
|
pub payloads: Vec<Element>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Message {
|
||||||
|
pub fn new(to: Option<Jid>) -> Message {
|
||||||
|
Message {
|
||||||
|
from: None,
|
||||||
|
to: to,
|
||||||
|
id: None,
|
||||||
|
type_: MessageType::Chat,
|
||||||
|
bodies: BTreeMap::new(),
|
||||||
|
subjects: BTreeMap::new(),
|
||||||
|
thread: None,
|
||||||
|
payloads: vec!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl TryFrom<Element> for Message {
|
impl TryFrom<Element> for Message {
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
|
@ -237,16 +252,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialise() {
|
fn test_serialise() {
|
||||||
let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap();
|
let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap();
|
||||||
let message = Message {
|
let mut message = Message::new(None);
|
||||||
from: None,
|
message.type_ = MessageType::Normal;
|
||||||
to: None,
|
|
||||||
id: None,
|
|
||||||
type_: MessageType::Normal,
|
|
||||||
bodies: BTreeMap::new(),
|
|
||||||
subjects: BTreeMap::new(),
|
|
||||||
thread: None,
|
|
||||||
payloads: vec!(),
|
|
||||||
};
|
|
||||||
let elem2 = message.into();
|
let elem2 = message.into();
|
||||||
assert_eq!(elem, elem2);
|
assert_eq!(elem, elem2);
|
||||||
}
|
}
|
||||||
|
@ -265,18 +272,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialise_body() {
|
fn test_serialise_body() {
|
||||||
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
||||||
let mut bodies = BTreeMap::new();
|
let mut message = Message::new(Some(Jid::from_str("coucou@example.org").unwrap()));
|
||||||
bodies.insert(String::from(""), String::from("Hello world!"));
|
message.bodies.insert(String::from(""), String::from("Hello world!"));
|
||||||
let message = Message {
|
|
||||||
from: None,
|
|
||||||
to: Some(Jid::from_str("coucou@example.org").unwrap()),
|
|
||||||
id: None,
|
|
||||||
type_: MessageType::Chat,
|
|
||||||
bodies: bodies,
|
|
||||||
subjects: BTreeMap::new(),
|
|
||||||
thread: None,
|
|
||||||
payloads: vec!(),
|
|
||||||
};
|
|
||||||
let elem2 = message.into();
|
let elem2 = message.into();
|
||||||
assert_eq!(elem, elem2);
|
assert_eq!(elem, elem2);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue