xmpp-parsers: Prefer Bare/FromJid::new to FromStr::from_str.

This commit is contained in:
Emmanuel Gil Peyrot 2020-12-10 20:45:30 +01:00
parent 3df447de97
commit f34897b198
10 changed files with 30 additions and 49 deletions

View file

@ -34,6 +34,7 @@ mod tests {
use super::*;
use crate::util::error::Error;
use crate::Element;
use jid::BareJid;
use std::convert::TryFrom;
use std::str::FromStr;
@ -56,7 +57,7 @@ mod tests {
.parse()
.unwrap();
let delay = Delay::try_from(elem).unwrap();
assert_eq!(delay.from, Some(Jid::from_str("capulet.com").unwrap()));
assert_eq!(delay.from.unwrap(), BareJid::domain("capulet.com"));
assert_eq!(
delay.stamp,
DateTime::from_str("2002-09-10T23:08:25Z").unwrap()
@ -108,7 +109,7 @@ mod tests {
fn test_serialise_data() {
let elem: Element = "<delay xmlns='urn:xmpp:delay' from='juliet@example.org' stamp='2002-09-10T23:08:25+00:00'>Reason</delay>".parse().unwrap();
let delay = Delay {
from: Some(Jid::from_str("juliet@example.org").unwrap()),
from: Some(Jid::Bare(BareJid::new("juliet", "example.org"))),
stamp: DateTime::from_str("2002-09-10T23:08:25Z").unwrap(),
data: Some(String::from("Reason")),
};

View file

@ -231,7 +231,7 @@ impl IqResultPayload for DiscoItemsResult {}
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use jid::BareJid;
#[cfg(target_pointer_width = "32")]
#[test]
@ -442,10 +442,10 @@ mod tests {
let elem2 = Element::from(query);
let query = DiscoItemsResult::try_from(elem2).unwrap();
assert_eq!(query.items.len(), 2);
assert_eq!(query.items[0].jid, Jid::from_str("component").unwrap());
assert_eq!(query.items[0].jid, BareJid::domain("component"));
assert_eq!(query.items[0].node, None);
assert_eq!(query.items[0].name, None);
assert_eq!(query.items[1].jid, Jid::from_str("component2").unwrap());
assert_eq!(query.items[1].jid, BareJid::domain("component2"));
assert_eq!(query.items[1].node, Some(String::from("test")));
assert_eq!(query.items[1].name, Some(String::from("A component")));
}

View file

@ -41,8 +41,8 @@ impl IqResultPayload for JidPrepResponse {}
mod tests {
use super::*;
use crate::Element;
use jid::FullJid;
use std::convert::TryFrom;
use std::str::FromStr;
#[cfg(target_pointer_width = "32")]
#[test]
@ -72,7 +72,7 @@ mod tests {
let response = JidPrepResponse::try_from(elem).unwrap();
assert_eq!(
response.jid,
Jid::from_str("romeo@montague.lit/orchard").unwrap()
FullJid::new("romeo", "montague.lit", "orchard")
);
}
}

View file

@ -276,6 +276,7 @@ impl From<Transport> for Element {
#[cfg(test)]
mod tests {
use super::*;
use jid::BareJid;
use std::str::FromStr;
#[cfg(target_pointer_width = "32")]
@ -340,7 +341,7 @@ mod tests {
payload: TransportPayload::Candidates(vec![Candidate {
cid: CandidateId(String::from("coucou")),
host: IpAddr::from_str("127.0.0.1").unwrap(),
jid: Jid::from_str("coucou@coucou").unwrap(),
jid: Jid::Bare(BareJid::new("coucou", "coucou")),
port: None,
priority: 0u32,
type_: Type::Direct,

View file

@ -190,7 +190,7 @@ impl From<Prefs> for Element {
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use jid::BareJid;
#[cfg(target_pointer_width = "32")]
#[test]
@ -344,14 +344,8 @@ mod tests {
.parse()
.unwrap();
let prefs = Prefs::try_from(elem).unwrap();
assert_eq!(
prefs.always,
vec!(Jid::from_str("romeo@montague.lit").unwrap())
);
assert_eq!(
prefs.never,
vec!(Jid::from_str("montague@montague.lit").unwrap())
);
assert_eq!(prefs.always, [BareJid::new("romeo", "montague.lit")]);
assert_eq!(prefs.never, [BareJid::new("montague", "montague.lit")]);
let elem2 = Element::from(prefs.clone());
println!("{:?}", elem2);

View file

@ -95,10 +95,10 @@ pub struct Message {
impl Message {
/// Creates a new `<message/>` stanza for the given recipient.
pub fn new(to: Option<Jid>) -> Message {
pub fn new<J: Into<Option<Jid>>>(to: J) -> Message {
Message {
from: None,
to,
to: to.into(),
id: None,
type_: MessageType::Chat,
bodies: BTreeMap::new(),
@ -241,6 +241,7 @@ impl From<Message> for Element {
#[cfg(test)]
mod tests {
use super::*;
use jid::BareJid;
use std::str::FromStr;
#[cfg(target_pointer_width = "32")]
@ -319,7 +320,7 @@ mod tests {
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<message xmlns='jabber:component:accept' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
let mut message = Message::new(Some(Jid::from_str("coucou@example.org").unwrap()));
let mut message = Message::new(Jid::Bare(BareJid::new("coucou", "example.org")));
message
.bodies
.insert(String::from(""), Body::from_str("Hello world!").unwrap());

View file

@ -244,7 +244,7 @@ impl From<PubSubEvent> for Element {
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use jid::BareJid;
#[test]
fn missing_items() {
@ -269,8 +269,8 @@ mod tests {
assert_eq!(node, NodeName(String::from("coucou")));
assert_eq!(items[0].id, Some(ItemId(String::from("test"))));
assert_eq!(
items[0].publisher,
Some(Jid::from_str("test@coucou").unwrap())
items[0].publisher.clone().unwrap(),
BareJid::new("test", "coucou")
);
assert_eq!(items[0].payload, None);
}
@ -401,7 +401,7 @@ mod tests {
)))
);
assert_eq!(subscription, Some(Subscription::Subscribed));
assert_eq!(jid, Some(Jid::from_str("francisco@denmark.lit").unwrap()));
assert_eq!(jid.unwrap(), BareJid::new("francisco", "denmark.lit"));
assert_eq!(expiry, Some("2006-02-28T23:59:59Z".parse().unwrap()));
}
_ => panic!(),

View file

@ -518,7 +518,6 @@ mod tests {
use super::*;
use crate::data_forms::{DataForm, DataFormType, Field, FieldType};
use jid::FullJid;
use std::str::FromStr;
#[test]
fn create() {
@ -701,7 +700,7 @@ mod tests {
let form = DataForm::try_from(elem).unwrap();
let options = Options {
jid: Jid::Full(FullJid::from_str("juliet@capulet.lit/balcony").unwrap()),
jid: Jid::Full(FullJid::new("juliet", "capulet.lit", "balcony")),
node: None,
subid: None,
form: Some(form),

View file

@ -169,10 +169,7 @@ mod tests {
let roster = Roster::try_from(elem).unwrap();
assert_eq!(roster.ver, Some(String::from("ver11")));
assert_eq!(roster.items.len(), 4);
assert_eq!(
roster.items[0].jid,
BareJid::from_str("romeo@example.net").unwrap()
);
assert_eq!(roster.items[0].jid, BareJid::new("romeo", "example.net"));
assert_eq!(roster.items[0].name, Some(String::from("Romeo")));
assert_eq!(roster.items[0].subscription, Subscription::Both);
assert_eq!(roster.items[0].ask, Ask::None);
@ -181,10 +178,7 @@ mod tests {
vec!(Group::from_str("Friends").unwrap())
);
assert_eq!(
roster.items[3].jid,
BareJid::from_str("contact@example.org").unwrap()
);
assert_eq!(roster.items[3].jid, BareJid::new("contact", "example.org"));
assert_eq!(roster.items[3].name, Some(String::from("MyContact")));
assert_eq!(roster.items[3].subscription, Subscription::None);
assert_eq!(roster.items[3].ask, Ask::Subscribe);
@ -203,10 +197,7 @@ mod tests {
let roster = Roster::try_from(elem).unwrap();
assert!(roster.ver.is_none());
assert_eq!(roster.items.len(), 1);
assert_eq!(
roster.items[0].jid,
BareJid::from_str("test@example.org").unwrap()
);
assert_eq!(roster.items[0].jid, BareJid::new("test", "example.org"));
assert_eq!(roster.items[0].name, None);
assert_eq!(roster.items[0].groups.len(), 2);
assert_eq!(roster.items[0].groups[0], Group::from_str("A").unwrap());
@ -238,10 +229,7 @@ mod tests {
let roster = Roster::try_from(elem).unwrap();
assert!(roster.ver.is_none());
assert_eq!(roster.items.len(), 1);
assert_eq!(
roster.items[0].jid,
BareJid::from_str("nurse@example.com").unwrap()
);
assert_eq!(roster.items[0].jid, BareJid::new("nurse", "example.com"));
assert_eq!(roster.items[0].name, Some(String::from("Nurse")));
assert_eq!(roster.items[0].groups.len(), 1);
assert_eq!(
@ -260,10 +248,7 @@ mod tests {
let roster = Roster::try_from(elem).unwrap();
assert!(roster.ver.is_none());
assert_eq!(roster.items.len(), 1);
assert_eq!(
roster.items[0].jid,
BareJid::from_str("nurse@example.com").unwrap()
);
assert_eq!(roster.items[0].jid, BareJid::new("nurse", "example.com"));
assert!(roster.items[0].name.is_none());
assert!(roster.items[0].groups.is_empty());
assert_eq!(roster.items[0].subscription, Subscription::Remove);

View file

@ -39,8 +39,8 @@ mod tests {
use super::*;
use crate::util::error::Error;
use crate::Element;
use jid::BareJid;
use std::convert::TryFrom;
use std::str::FromStr;
#[cfg(target_pointer_width = "32")]
#[test]
@ -63,7 +63,7 @@ mod tests {
.unwrap();
let stanza_id = StanzaId::try_from(elem).unwrap();
assert_eq!(stanza_id.id, String::from("coucou"));
assert_eq!(stanza_id.by, Jid::from_str("coucou@coucou").unwrap());
assert_eq!(stanza_id.by, BareJid::new("coucou", "coucou"));
let elem: Element = "<origin-id xmlns='urn:xmpp:sid:0' id='coucou'/>"
.parse()
@ -116,7 +116,7 @@ mod tests {
.unwrap();
let stanza_id = StanzaId {
id: String::from("coucou"),
by: Jid::from_str("coucou@coucou").unwrap(),
by: Jid::Bare(BareJid::new("coucou", "coucou")),
};
let elem2 = stanza_id.into();
assert_eq!(elem, elem2);