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