xmpp-parsers: Update to the new jid crate

This commit is contained in:
Emmanuel Gil Peyrot 2023-06-20 14:07:50 +02:00
parent cdf4486e53
commit 022a920300
18 changed files with 61 additions and 50 deletions

View file

@ -170,11 +170,14 @@ mod tests {
//assert_eq!(bind.resource.unwrap(), "HelloTM");
assert_eq!(bind.resource.unwrap(), "Hello™");
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>coucou@linkmauve.fr/HelloTM</jid></bind>"
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>coucou@linkmauve.fr/Hello</jid></bind>"
.parse()
.unwrap();
let bind = BindResponse::try_from(elem).unwrap();
assert_eq!(bind.jid, FullJid::new("coucou", "linkmauve.fr", "HelloTM"));
assert_eq!(
bind.jid,
FullJid::new("coucou@linkmauve.fr/HelloTM").unwrap()
);
}
#[cfg(not(feature = "disable-validation"))]

View file

@ -98,7 +98,6 @@ generate_empty_element!(
#[cfg(test)]
mod tests {
use super::*;
use jid::BareJid;
#[cfg(target_pointer_width = "32")]
#[test]
@ -141,14 +140,8 @@ mod tests {
fn test_items() {
let elem: Element = "<blocklist xmlns='urn:xmpp:blocking'><item jid='coucou@coucou'/><item jid='domain'/></blocklist>".parse().unwrap();
let two_items = vec![
Jid::Bare(BareJid {
node: Some(String::from("coucou")),
domain: String::from("coucou"),
}),
Jid::Bare(BareJid {
node: None,
domain: String::from("domain"),
}),
Jid::new("coucou@coucou").unwrap(),
Jid::new("domain").unwrap(),
];
let result_elem = elem.clone();

View file

@ -112,7 +112,7 @@ mod tests {
assert_eq!(storage.conferences[0].autojoin, Autojoin::True);
assert_eq!(
storage.conferences[0].jid,
BareJid::new("test-muc", "muc.localhost")
BareJid::new("test-muc@muc.localhost").unwrap()
);
assert_eq!(storage.conferences[0].clone().name.unwrap(), "Test MUC");
assert_eq!(storage.conferences[0].clone().nick.unwrap(), "Coucou");

View file

@ -57,7 +57,7 @@ mod tests {
.parse()
.unwrap();
let delay = Delay::try_from(elem).unwrap();
assert_eq!(delay.from.unwrap(), BareJid::domain("capulet.com"));
assert_eq!(delay.from.unwrap(), BareJid::new("capulet.com").unwrap());
assert_eq!(
delay.stamp,
DateTime::from_str("2002-09-10T23:08:25Z").unwrap()
@ -109,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::Bare(BareJid::new("juliet", "example.org"))),
from: Some(Jid::new("juliet@example.org").unwrap()),
stamp: DateTime::from_str("2002-09-10T23:08:25Z").unwrap(),
data: Some(String::from("Reason")),
};

View file

@ -445,10 +445,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, BareJid::domain("component"));
assert_eq!(query.items[0].jid, BareJid::new("component").unwrap());
assert_eq!(query.items[0].node, None);
assert_eq!(query.items[0].name, None);
assert_eq!(query.items[1].jid, BareJid::domain("component2"));
assert_eq!(query.items[1].jid, BareJid::new("component2").unwrap());
assert_eq!(query.items[1].node, Some(String::from("test")));
assert_eq!(query.items[1].name, Some(String::from("A component")));
}

View file

@ -75,7 +75,7 @@ mod tests {
let response = JidPrepResponse::try_from(elem).unwrap();
assert_eq!(
response.jid,
FullJid::new("romeo", "montague.lit", "orchard")
FullJid::new("romeo@montague.lit/orchard").unwrap()
);
}
}

View file

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

View file

@ -24,7 +24,7 @@
#![warn(missing_docs)]
pub use crate::util::error::Error;
pub use jid::{BareJid, FullJid, Jid, JidParseError};
pub use jid::{BareJid, Error as JidParseError, FullJid, Jid};
pub use minidom::Element;
/// XML namespace definitions used through XMPP.

View file

@ -88,7 +88,7 @@ fn serialise_jid_list(name: &str, jids: Vec<Jid>) -> ::std::option::IntoIter<Nod
Element::builder(name, ns::MAM)
.append_all(
jids.into_iter()
.map(|jid| Element::builder("jid", ns::MAM).append(String::from(jid))),
.map(|jid| Element::builder("jid", ns::MAM).append(jid)),
)
.into(),
)
@ -160,8 +160,11 @@ mod tests {
.parse()
.unwrap();
let prefs = Prefs::try_from(elem).unwrap();
assert_eq!(prefs.always, [BareJid::new("romeo", "montague.lit")]);
assert_eq!(prefs.never, [BareJid::new("montague", "montague.lit")]);
assert_eq!(prefs.always, [BareJid::new("romeo@montague.lit").unwrap()]);
assert_eq!(
prefs.never,
[BareJid::new("montague@montague.lit").unwrap()]
);
let elem2 = Element::from(prefs.clone());
println!("{:?}", elem2);

View file

@ -299,7 +299,6 @@ impl From<Message> for Element {
#[cfg(test)]
mod tests {
use super::*;
use jid::BareJid;
use std::str::FromStr;
#[cfg(target_pointer_width = "32")]
@ -378,7 +377,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(Jid::Bare(BareJid::new("coucou", "example.org")));
let mut message = Message::new(Jid::new("coucou@example.org").unwrap());
message
.bodies
.insert(String::from(""), Body::from_str("Hello world!").unwrap());

View file

@ -301,7 +301,7 @@ mod tests {
use super::*;
use crate::message::Message;
use crate::presence::{Presence, Type as PresenceType};
use crate::{BareJid, Jid};
use crate::Jid;
#[test]
fn test_simple() {
@ -743,7 +743,7 @@ mod tests {
#[test]
fn message_payload() {
let jid: Jid = Jid::Bare(BareJid::new("louise", "example.com"));
let jid: Jid = Jid::new("louise@example.com").unwrap();
let elem: Element = "<x xmlns='http://jabber.org/protocol/muc#user'/>"
.parse()
.unwrap();

View file

@ -672,24 +672,21 @@ mod tests {
let elem: Element = presence.into();
assert_eq!(elem.attr("to"), None);
let presence = Presence::new(Type::None).with_to(Jid::Bare(BareJid::domain("localhost")));
let presence = Presence::new(Type::None).with_to(Jid::new("localhost").unwrap());
let elem: Element = presence.into();
assert_eq!(elem.attr("to"), Some("localhost"));
let presence = Presence::new(Type::None).with_to(BareJid::domain("localhost"));
let presence = Presence::new(Type::None).with_to(BareJid::new("localhost").unwrap());
let elem: Element = presence.into();
assert_eq!(elem.attr("to"), Some("localhost"));
let presence = Presence::new(Type::None).with_to(Jid::Full(FullJid::new(
"test",
"localhost",
"coucou",
)));
let presence =
Presence::new(Type::None).with_to(Jid::new("test@localhost/coucou").unwrap());
let elem: Element = presence.into();
assert_eq!(elem.attr("to"), Some("test@localhost/coucou"));
let presence =
Presence::new(Type::None).with_to(FullJid::new("test", "localhost", "coucou"));
Presence::new(Type::None).with_to(FullJid::new("test@localhost/coucou").unwrap());
let elem: Element = presence.into();
assert_eq!(elem.attr("to"), Some("test@localhost/coucou"));
}

View file

@ -273,7 +273,7 @@ mod tests {
assert_eq!(items[0].id, Some(ItemId(String::from("test"))));
assert_eq!(
items[0].publisher.clone().unwrap(),
BareJid::new("test", "coucou")
BareJid::new("test@coucou").unwrap()
);
assert_eq!(items[0].payload, None);
}
@ -404,7 +404,7 @@ mod tests {
)))
);
assert_eq!(subscription, Some(Subscription::Subscribed));
assert_eq!(jid.unwrap(), BareJid::new("francisco", "denmark.lit"));
assert_eq!(jid.unwrap(), BareJid::new("francisco@denmark.lit").unwrap());
assert_eq!(expiry, Some("2006-02-28T23:59:59Z".parse().unwrap()));
}
_ => panic!(),

View file

@ -546,7 +546,6 @@ impl From<PubSub> for Element {
mod tests {
use super::*;
use crate::data_forms::{DataForm, DataFormType, Field, FieldType};
use jid::FullJid;
#[test]
fn create() {
@ -746,7 +745,7 @@ mod tests {
let form = DataForm::try_from(elem).unwrap();
let options = Options {
jid: Jid::Full(FullJid::new("juliet", "capulet.lit", "balcony")),
jid: Jid::new("juliet@capulet.lit/balcony").unwrap(),
node: None,
subid: None,
form: Some(form),

View file

@ -167,7 +167,10 @@ 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::new("romeo", "example.net"));
assert_eq!(
roster.items[0].jid,
BareJid::new("romeo@example.net").unwrap()
);
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);
@ -176,7 +179,10 @@ mod tests {
vec!(Group::from_str("Friends").unwrap())
);
assert_eq!(roster.items[3].jid, BareJid::new("contact", "example.org"));
assert_eq!(
roster.items[3].jid,
BareJid::new("contact@example.org").unwrap()
);
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);
@ -195,7 +201,10 @@ 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::new("test", "example.org"));
assert_eq!(
roster.items[0].jid,
BareJid::new("test@example.org").unwrap()
);
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());
@ -225,7 +234,10 @@ 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::new("nurse", "example.com"));
assert_eq!(
roster.items[0].jid,
BareJid::new("nurse@example.com").unwrap()
);
assert_eq!(roster.items[0].name, Some(String::from("Nurse")));
assert_eq!(roster.items[0].groups.len(), 1);
assert_eq!(
@ -242,7 +254,10 @@ 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::new("nurse", "example.com"));
assert_eq!(
roster.items[0].jid,
BareJid::new("nurse@example.com").unwrap()
);
assert!(roster.items[0].name.is_none());
assert!(roster.items[0].groups.is_empty());
assert_eq!(roster.items[0].subscription, Subscription::Remove);

View file

@ -66,7 +66,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, BareJid::new("coucou", "coucou"));
assert_eq!(stanza_id.by, BareJid::new("coucou@coucou").unwrap());
let elem: Element = "<origin-id xmlns='urn:xmpp:sid:0' id='coucou'/>"
.parse()
@ -119,7 +119,7 @@ mod tests {
.unwrap();
let stanza_id = StanzaId {
id: String::from("coucou"),
by: Jid::Bare(BareJid::new("coucou", "coucou")),
by: Jid::new("coucou@coucou").unwrap(),
};
let elem2 = stanza_id.into();
assert_eq!(elem, elem2);

View file

@ -92,7 +92,10 @@ mod tests {
fn test_simple() {
let elem: Element = "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' xml:lang='en' version='1.0' id='abc' from='some-server.example'/>".parse().unwrap();
let stream = Stream::try_from(elem).unwrap();
assert_eq!(stream.from, Some(BareJid::domain("some-server.example")));
assert_eq!(
stream.from,
Some(BareJid::new("some-server.example").unwrap())
);
assert_eq!(stream.to, None);
assert_eq!(stream.id, Some(String::from("abc")));
assert_eq!(stream.version, Some(String::from("1.0")));

View file

@ -33,7 +33,7 @@ pub enum Error {
/// Generated when text which should be a [JID](../../jid/struct.Jid.html)
/// fails to parse.
JidParseError(jid::JidParseError),
JidParseError(jid::Error),
/// Generated when text which should be a
/// [DateTime](../date/struct.DateTime.html) fails to parse.
@ -92,8 +92,8 @@ impl From<std::net::AddrParseError> for Error {
}
}
impl From<jid::JidParseError> for Error {
fn from(err: jid::JidParseError) -> Error {
impl From<jid::Error> for Error {
fn from(err: jid::Error) -> Error {
Error::JidParseError(err)
}
}