jid_prep.rs

 1// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 2//
 3// This Source Code Form is subject to the terms of the Mozilla Public
 4// License, v. 2.0. If a copy of the MPL was not distributed with this
 5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
 6
 7use crate::iq::{IqGetPayload, IqResultPayload};
 8use crate::util::helpers::{JidCodec, Text};
 9use jid::Jid;
10
11generate_element!(
12    /// Request from a client to stringprep/PRECIS a string into a JID.
13    JidPrepQuery, "jid", JID_PREP,
14    text: (
15        /// The potential JID.
16        data: Text<String>
17    )
18);
19
20impl IqGetPayload for JidPrepQuery {}
21
22impl JidPrepQuery {
23    /// Create a new JID Prep query.
24    pub fn new<J: Into<String>>(jid: J) -> JidPrepQuery {
25        JidPrepQuery { data: jid.into() }
26    }
27}
28
29generate_element!(
30    /// Response from the server with the stringprep’d/PRECIS’d JID.
31    JidPrepResponse, "jid", JID_PREP,
32    text: (
33        /// The JID.
34        jid: JidCodec<Jid>
35    )
36);
37
38impl IqResultPayload for JidPrepResponse {}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use crate::Element;
44    use jid::FullJid;
45    use std::convert::TryFrom;
46
47    #[cfg(target_pointer_width = "32")]
48    #[test]
49    fn test_size() {
50        assert_size!(JidPrepQuery, 12);
51        assert_size!(JidPrepResponse, 40);
52    }
53
54    #[cfg(target_pointer_width = "64")]
55    #[test]
56    fn test_size() {
57        assert_size!(JidPrepQuery, 24);
58        assert_size!(JidPrepResponse, 80);
59    }
60
61    #[test]
62    fn simple() {
63        let elem: Element = "<jid xmlns='urn:xmpp:jidprep:0'>ROMeo@montague.lit/orchard</jid>"
64            .parse()
65            .unwrap();
66        let query = JidPrepQuery::try_from(elem).unwrap();
67        assert_eq!(query.data, "ROMeo@montague.lit/orchard");
68
69        let elem: Element = "<jid xmlns='urn:xmpp:jidprep:0'>romeo@montague.lit/orchard</jid>"
70            .parse()
71            .unwrap();
72        let response = JidPrepResponse::try_from(elem).unwrap();
73        assert_eq!(
74            response.jid,
75            FullJid::new("romeo", "montague.lit", "orchard")
76        );
77    }
78}