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
75 .chars()
76 .filter(|ch| *ch != ' ' && *ch != '\n' && *ch != '\t')
77 .collect();
78 Ok(base64::decode(&s)?)
79 }
80
81 pub fn encode(b: &[u8]) -> Option<String> {
82 Some(base64::encode(b))
83 }
84}
85
86/// Codec for colon-separated bytes of uppercase hexadecimal.
87pub struct ColonSeparatedHex;
88
89impl ColonSeparatedHex {
90 pub fn decode(s: &str) -> Result<Vec<u8>, Error> {
91 let mut bytes = vec![];
92 for i in 0..(1 + s.len()) / 3 {
93 let byte = u8::from_str_radix(&s[3 * i..3 * i + 2], 16)?;
94 if 3 * i + 2 < s.len() {
95 assert_eq!(&s[3 * i + 2..3 * i + 3], ":");
96 }
97 bytes.push(byte);
98 }
99 Ok(bytes)
100 }
101
102 pub fn encode(b: &[u8]) -> Option<String> {
103 let mut bytes = vec![];
104 for byte in b {
105 bytes.push(format!("{:02X}", byte));
106 }
107 Some(bytes.join(":"))
108 }
109}
110
111/// Codec for a JID.
112pub struct JidCodec;
113
114impl JidCodec {
115 pub fn decode(s: &str) -> Result<Jid, Error> {
116 Ok(Jid::from_str(s)?)
117 }
118
119 pub fn encode(jid: &Jid) -> Option<String> {
120 Some(jid.to_string())
121 }
122}