From a9a68cb1d749051558ef6978e3804d32e1bf3a6c Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 8 Sep 2019 16:22:12 +0200 Subject: [PATCH] jid_prep: Add constructor, documentation, and switch from Option to String. --- src/jid_prep.rs | 23 ++++++++++++++++------- src/util/helpers.rs | 13 +++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/jid_prep.rs b/src/jid_prep.rs index 9ecff61a..d151f6c0 100644 --- a/src/jid_prep.rs +++ b/src/jid_prep.rs @@ -5,25 +5,34 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. use crate::iq::{IqGetPayload, IqResultPayload}; -use crate::util::helpers::{PlainText, JidCodec}; +use crate::util::helpers::{Text, JidCodec}; use jid::Jid; generate_element!( - /// TODO + /// Request from a client to stringprep/PRECIS a string into a JID. JidPrepQuery, "jid", JID_PREP, text: ( - /// TODO - data: PlainText> + /// The potential JID. + data: Text ) ); impl IqGetPayload for JidPrepQuery {} +impl JidPrepQuery { + /// Create a new JID Prep query. + pub fn new>(jid: J) -> JidPrepQuery { + JidPrepQuery { + data: jid.into(), + } + } +} + generate_element!( - /// TODO + /// Response from the server with the stringprep’d/PRECIS’d JID. JidPrepResponse, "jid", JID_PREP, text: ( - /// TODO + /// The JID. jid: JidCodec ) ); @@ -47,7 +56,7 @@ mod tests { fn simple() { let elem: Element = "ROMeo@montague.lit/orchard".parse().unwrap(); let query = JidPrepQuery::try_from(elem).unwrap(); - assert_eq!(query.data.unwrap(), "ROMeo@montague.lit/orchard"); + assert_eq!(query.data, "ROMeo@montague.lit/orchard"); let elem: Element = "romeo@montague.lit/orchard".parse().unwrap(); let response = JidPrepResponse::try_from(elem).unwrap(); diff --git a/src/util/helpers.rs b/src/util/helpers.rs index a78fa7f6..7bf3feb1 100644 --- a/src/util/helpers.rs +++ b/src/util/helpers.rs @@ -8,6 +8,19 @@ use crate::util::error::Error; use jid::Jid; use std::str::FromStr; +/// Codec for text content. +pub struct Text; + +impl Text { + pub fn decode(s: &str) -> Result { + Ok(s.to_owned()) + } + + pub fn encode(string: &str) -> Option { + Some(string.to_owned()) + } +} + /// Codec for plain text content. pub struct PlainText;