1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use quoted_printable::{encode_to_str};
use base64::{encode};

#[derive(PartialEq)]
pub enum Encoding {
  BEncoding,
  QEncoding,
}

impl Encoding {
  pub fn name(&self) -> &str {
    match *self {
      Encoding::BEncoding => "B",
      Encoding::QEncoding => "Q",
    }
  }
}

pub struct WordEncoder {
  charset: &'static str,
  encoding: Encoding,
}

impl WordEncoder {

  pub fn new(charset: &'static str, encoding: Encoding) -> WordEncoder {
    WordEncoder {
      charset: charset,
      encoding: encoding,
    }
  }

  pub fn encode_word(&self, data: String) -> String {
    let to_charset = self.charset;
    let mut encoded_str = match self.encoding {
      Encoding::BEncoding => encode(data.as_bytes()),
      Encoding::QEncoding => encode_to_str(data).replace("\r\n=",""),
    };
    encoded_str = encoded_str.replace(" ", "_");
    let mut last_token = "";
    let len = encoded_str.len();
    if &encoded_str[len-2..len] != "?=" {
      last_token = "?=";
    }
    return format!("=?{}?{}?{}{}", to_charset, self.encoding.name(), encoded_str, last_token);
  }

}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn it_works() {
    let e = WordEncoder::new("UTF-8", Encoding::QEncoding);
    assert_eq!(e.encode_word("サポート".to_string()), "=?UTF-8?Q?=E3=82=B5=E3=83=9D=E3=83=BC=E3=83=88?=");
    assert_eq!(e.encode_word("abcあいうえお".to_string()), "=?UTF-8?Q?abc=E3=81=82=E3=81=84=E3=81=86=E3=81=88=E3=81=8A?=");
    assert_eq!(e.encode_word("abc한국어".to_string()), "=?UTF-8?Q?abc=ED=95=9C=EA=B5=AD=EC=96=B4?=");
    assert_eq!(e.encode_word("abc日本語".to_string()), "=?UTF-8?Q?abc=E6=97=A5=E6=9C=AC=E8=AA=9E?=");
    assert_eq!(e.encode_word("abc中文".to_string()), "=?UTF-8?Q?abc=E4=B8=AD=E6=96=87?=");
    assert_eq!(e.encode_word("abc def".to_string()), "=?UTF-8?Q?abc_def?=");
    assert_eq!(e.encode_word("あいうえおABC中国".to_string()), "=?UTF-8?Q?=E3=81=82=E3=81=84=E3=81=86=E3=81=88=E3=81=8AABC=E4=B8=AD=E5=9B=BD?=");
  }
}