From f34897b198551dbaf10c0c3522376075054833ce Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 10 Dec 2020 20:45:30 +0100 Subject: [PATCH] xmpp-parsers: Prefer Bare/FromJid::new to FromStr::from_str. --- xmpp-parsers/src/delay.rs | 5 +++-- xmpp-parsers/src/disco.rs | 6 +++--- xmpp-parsers/src/jid_prep.rs | 4 ++-- xmpp-parsers/src/jingle_s5b.rs | 3 ++- xmpp-parsers/src/mam.rs | 12 +++--------- xmpp-parsers/src/message.rs | 7 ++++--- xmpp-parsers/src/pubsub/event.rs | 8 ++++---- xmpp-parsers/src/pubsub/pubsub.rs | 3 +-- xmpp-parsers/src/roster.rs | 25 +++++-------------------- xmpp-parsers/src/stanza_id.rs | 6 +++--- 10 files changed, 30 insertions(+), 49 deletions(-) diff --git a/xmpp-parsers/src/delay.rs b/xmpp-parsers/src/delay.rs index fcb94479..897b98c7 100644 --- a/xmpp-parsers/src/delay.rs +++ b/xmpp-parsers/src/delay.rs @@ -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 = "Reason".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")), }; diff --git a/xmpp-parsers/src/disco.rs b/xmpp-parsers/src/disco.rs index 50753f75..6ed2c13f 100644 --- a/xmpp-parsers/src/disco.rs +++ b/xmpp-parsers/src/disco.rs @@ -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"))); } diff --git a/xmpp-parsers/src/jid_prep.rs b/xmpp-parsers/src/jid_prep.rs index 5f596e89..a78cc0be 100644 --- a/xmpp-parsers/src/jid_prep.rs +++ b/xmpp-parsers/src/jid_prep.rs @@ -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") ); } } diff --git a/xmpp-parsers/src/jingle_s5b.rs b/xmpp-parsers/src/jingle_s5b.rs index 7c45c0cb..10634d0f 100644 --- a/xmpp-parsers/src/jingle_s5b.rs +++ b/xmpp-parsers/src/jingle_s5b.rs @@ -276,6 +276,7 @@ impl From 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, diff --git a/xmpp-parsers/src/mam.rs b/xmpp-parsers/src/mam.rs index e9fa12bf..b77aac35 100644 --- a/xmpp-parsers/src/mam.rs +++ b/xmpp-parsers/src/mam.rs @@ -190,7 +190,7 @@ impl From 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); diff --git a/xmpp-parsers/src/message.rs b/xmpp-parsers/src/message.rs index b502fc2e..f473010d 100644 --- a/xmpp-parsers/src/message.rs +++ b/xmpp-parsers/src/message.rs @@ -95,10 +95,10 @@ pub struct Message { impl Message { /// Creates a new `` stanza for the given recipient. - pub fn new(to: Option) -> Message { + pub fn new>>(to: J) -> Message { Message { from: None, - to, + to: to.into(), id: None, type_: MessageType::Chat, bodies: BTreeMap::new(), @@ -241,6 +241,7 @@ impl From 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 = "Hello world!".parse().unwrap(); #[cfg(feature = "component")] let elem: Element = "Hello world!".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()); diff --git a/xmpp-parsers/src/pubsub/event.rs b/xmpp-parsers/src/pubsub/event.rs index 54f3c2b3..386760eb 100644 --- a/xmpp-parsers/src/pubsub/event.rs +++ b/xmpp-parsers/src/pubsub/event.rs @@ -244,7 +244,7 @@ impl From 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!(), diff --git a/xmpp-parsers/src/pubsub/pubsub.rs b/xmpp-parsers/src/pubsub/pubsub.rs index f3e08da6..d4ad9f81 100644 --- a/xmpp-parsers/src/pubsub/pubsub.rs +++ b/xmpp-parsers/src/pubsub/pubsub.rs @@ -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), diff --git a/xmpp-parsers/src/roster.rs b/xmpp-parsers/src/roster.rs index fec25864..cdf83685 100644 --- a/xmpp-parsers/src/roster.rs +++ b/xmpp-parsers/src/roster.rs @@ -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); diff --git a/xmpp-parsers/src/stanza_id.rs b/xmpp-parsers/src/stanza_id.rs index a8696bfd..940db3f3 100644 --- a/xmpp-parsers/src/stanza_id.rs +++ b/xmpp-parsers/src/stanza_id.rs @@ -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 = "" .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);