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 base64;
8use error::Error;
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(|text| {
23 text.to_owned()
24 })
25 }
26}
27
28/// Codec for trimmed plain text content.
29pub struct TrimmedPlainText;
30
31impl TrimmedPlainText {
32 pub fn decode(s: &str) -> Result<String, Error> {
33 Ok(match s.trim() {
34 "" => return Err(Error::ParseError("URI missing in uri.")),
35 text => text.to_owned(),
36 })
37 }
38
39 pub fn encode(string: &String) -> String {
40 string.to_owned()
41 }
42}
43
44/// Codec wrapping base64 encode/decode
45pub struct Base64;
46
47impl Base64 {
48 pub fn decode(s: &str) -> Result<Vec<u8>, Error> {
49 Ok(base64::decode(s)?)
50 }
51
52 pub fn encode(b: &Vec<u8>) -> Option<String> {
53 Some(base64::encode(b))
54 }
55}