1
0
mirror of https://gitlab.com/xmpp-rs/xmpp-rs.git synced 2024-06-16 21:05:26 +02:00
xmpp-rs/parsers/src/jid_prep.rs

77 lines
2.0 KiB
Rust
Raw Normal View History

2019-09-08 16:09:49 +02:00
// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::iq::{IqGetPayload, IqResultPayload};
use crate::util::text_node_codecs::{Codec, JidCodec, Text};
2019-09-08 16:09:49 +02:00
generate_element!(
/// Request from a client to stringprep/PRECIS a string into a JID.
2019-09-08 16:09:49 +02:00
JidPrepQuery, "jid", JID_PREP,
text: (
/// The potential JID.
data: Text
2019-09-08 16:09:49 +02:00
)
);
impl IqGetPayload for JidPrepQuery {}
impl JidPrepQuery {
/// Create a new JID Prep query.
pub fn new<J: Into<String>>(jid: J) -> JidPrepQuery {
JidPrepQuery { data: jid.into() }
}
}
2019-09-08 16:09:49 +02:00
generate_element!(
/// Response from the server with the stringprepd/PRECISd JID.
2019-09-08 16:09:49 +02:00
JidPrepResponse, "jid", JID_PREP,
text: (
/// The JID.
jid: JidCodec
2019-09-08 16:09:49 +02:00
)
);
impl IqResultPayload for JidPrepResponse {}
#[cfg(test)]
mod tests {
use super::*;
use crate::Element;
use jid::FullJid;
2019-09-08 16:09:49 +02:00
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(JidPrepQuery, 12);
assert_size!(JidPrepResponse, 16);
}
#[cfg(target_pointer_width = "64")]
2019-09-08 16:09:49 +02:00
#[test]
fn test_size() {
assert_size!(JidPrepQuery, 24);
assert_size!(JidPrepResponse, 32);
2019-09-08 16:09:49 +02:00
}
#[test]
fn simple() {
let elem: Element = "<jid xmlns='urn:xmpp:jidprep:0'>ROMeo@montague.lit/orchard</jid>"
.parse()
.unwrap();
2019-09-08 16:09:49 +02:00
let query = JidPrepQuery::try_from(elem).unwrap();
assert_eq!(query.data, "ROMeo@montague.lit/orchard");
2019-09-08 16:09:49 +02:00
let elem: Element = "<jid xmlns='urn:xmpp:jidprep:0'>romeo@montague.lit/orchard</jid>"
.parse()
.unwrap();
2019-09-08 16:09:49 +02:00
let response = JidPrepResponse::try_from(elem).unwrap();
assert_eq!(
response.jid,
FullJid::new("romeo@montague.lit/orchard").unwrap()
);
2019-09-08 16:09:49 +02:00
}
}