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 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 wrapping base64 encode/decode
29pub struct Base64;
30
31impl Base64 {
32    pub fn decode(s: &str) -> Result<Vec<u8>, Error> {
33        Ok(base64::decode(s)?)
34    }
35
36    pub fn encode(b: &Vec<u8>) -> Option<String> {
37        Some(base64::encode(b))
38    }
39}