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