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::{Text, JidCodec};
 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 {
26            data: jid.into(),
27        }
28    }
29}
30
31generate_element!(
32    /// Response from the server with the stringprep’d/PRECIS’d JID.
33    JidPrepResponse, "jid", JID_PREP,
34    text: (
35        /// The JID.
36        jid: JidCodec<Jid>
37    )
38);
39
40impl IqResultPayload for JidPrepResponse {}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use crate::Element;
46    use std::convert::TryFrom;
47    use std::str::FromStr;
48
49    #[cfg(target_pointer_width = "32")]
50    #[test]
51    fn test_size() {
52        assert_size!(JidPrepQuery, 12);
53        assert_size!(JidPrepResponse, 40);
54    }
55
56    #[cfg(target_pointer_width = "64")]
57    #[test]
58    fn test_size() {
59        assert_size!(JidPrepQuery, 24);
60        assert_size!(JidPrepResponse, 80);
61    }
62
63    #[test]
64    fn simple() {
65        let elem: Element = "<jid xmlns='urn:xmpp:jidprep:0'>ROMeo@montague.lit/orchard</jid>".parse().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>".parse().unwrap();
70        let response = JidPrepResponse::try_from(elem).unwrap();
71        assert_eq!(response.jid, Jid::from_str("romeo@montague.lit/orchard").unwrap());
72    }
73}