helpers.rs

  1// Copyright (c) 2017 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::util::error::Error;
  8use jid::Jid;
  9use std::str::FromStr;
 10
 11/// Codec for text content.
 12pub struct Text;
 13
 14impl Text {
 15    pub fn decode(s: &str) -> Result<String, Error> {
 16        Ok(s.to_owned())
 17    }
 18
 19    pub fn encode(string: &str) -> Option<String> {
 20        Some(string.to_owned())
 21    }
 22}
 23
 24/// Codec for plain text content.
 25pub struct PlainText;
 26
 27impl PlainText {
 28    pub fn decode(s: &str) -> Result<Option<String>, Error> {
 29        Ok(match s {
 30            "" => None,
 31            text => Some(text.to_owned()),
 32        })
 33    }
 34
 35    pub fn encode(string: &Option<String>) -> Option<String> {
 36        string.as_ref().map(ToOwned::to_owned)
 37    }
 38}
 39
 40/// Codec for trimmed plain text content.
 41pub struct TrimmedPlainText;
 42
 43impl TrimmedPlainText {
 44    pub fn decode(s: &str) -> Result<String, Error> {
 45        Ok(match s.trim() {
 46            "" => return Err(Error::ParseError("URI missing in uri.")),
 47            text => text.to_owned(),
 48        })
 49    }
 50
 51    pub fn encode(string: &str) -> Option<String> {
 52        Some(string.to_owned())
 53    }
 54}
 55
 56/// Codec wrapping base64 encode/decode.
 57pub struct Base64;
 58
 59impl Base64 {
 60    pub fn decode(s: &str) -> Result<Vec<u8>, Error> {
 61        Ok(base64::decode(s)?)
 62    }
 63
 64    pub fn encode(b: &[u8]) -> Option<String> {
 65        Some(base64::encode(b))
 66    }
 67}
 68
 69/// Codec wrapping base64 encode/decode, while ignoring whitespace characters.
 70pub struct WhitespaceAwareBase64;
 71
 72impl WhitespaceAwareBase64 {
 73    pub fn decode(s: &str) -> Result<Vec<u8>, Error> {
 74        let s: String = s.chars().filter(|ch| *ch != ' ' && *ch != '\n' && *ch != '\t').collect();
 75        Ok(base64::decode(&s)?)
 76    }
 77
 78    pub fn encode(b: &[u8]) -> Option<String> {
 79        Some(base64::encode(b))
 80    }
 81}
 82
 83/// Codec for colon-separated bytes of uppercase hexadecimal.
 84pub struct ColonSeparatedHex;
 85
 86impl ColonSeparatedHex {
 87    pub fn decode(s: &str) -> Result<Vec<u8>, Error> {
 88        let mut bytes = vec![];
 89        for i in 0..(1 + s.len()) / 3 {
 90            let byte = u8::from_str_radix(&s[3 * i..3 * i + 2], 16)?;
 91            if 3 * i + 2 < s.len() {
 92                assert_eq!(&s[3 * i + 2..3 * i + 3], ":");
 93            }
 94            bytes.push(byte);
 95        }
 96        Ok(bytes)
 97    }
 98
 99    pub fn encode(b: &[u8]) -> Option<String> {
100        let mut bytes = vec![];
101        for byte in b {
102            bytes.push(format!("{:02X}", byte));
103        }
104        Some(bytes.join(":"))
105    }
106}
107
108/// Codec for a JID.
109pub struct JidCodec;
110
111impl JidCodec {
112    pub fn decode(s: &str) -> Result<Jid, Error> {
113        Ok(Jid::from_str(s)?)
114    }
115
116    pub fn encode(jid: &Jid) -> Option<String> {
117        Some(jid.to_string())
118    }
119}