From 32e373be03d78449a3ee76929ff19514ee6a43f4 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 23 Nov 2017 16:19:04 +0000 Subject: [PATCH] helpers: Add a plain text codec. --- src/helpers.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index e12000ac..a457081d 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -7,6 +7,24 @@ use base64; use error::Error; +/// Codec for plain text content. +pub struct PlainText; + +impl PlainText { + pub fn decode(s: &str) -> Result, Error> { + Ok(match s { + "" => None, + text => Some(text.to_owned()), + }) + } + + pub fn encode(string: &Option) -> Option { + string.as_ref().map(|text| { + text.to_owned() + }) + } +} + /// Codec wrapping base64 encode/decode pub struct Base64; @@ -15,7 +33,7 @@ impl Base64 { Ok(base64::decode(s)?) } - pub fn encode(b: &Vec) -> String { - base64::encode(b) + pub fn encode(b: &Vec) -> Option { + Some(base64::encode(b)) } }