deencode/
cp1254engine.rs

1//! Deencoding engine for Codepage 1254
2//!
3//! Codepage 1252 is a superset of ISO 8859-9, so the underlying implementations
4//! are for the former.
5//!
6//! Encoding is performed with
7//! [`encoding_rs`](https://crates.io/crates/encoding_rs), which does not
8//! support insertion of U+FFFD � REPLACEMENT CHARACTER at decoding. Decoding is
9//! performed with [`mail-parser`](https://crates.io/crates/mail-parser), which
10//! does not allow encoding.
11
12use crate::engine::Engine;
13
14use encoding_rs::*;
15use mail_parser::*;
16
17pub struct CP1254Engine {}
18
19impl Engine for CP1254Engine
20{
21    fn get_name(&self) -> String { "ISO 8859-9 / Codepage 1254".to_string() }
22
23    fn encode(&self, string: &str) -> Option<Vec<u8>>
24    {
25        let (output, _, error) = WINDOWS_1254.encode(string);
26        if error
27        {
28            None
29        }
30        else
31        {
32            Some(output.into_owned())
33        }
34    }
35
36    fn decode(&self, bytes: &[u8]) -> String
37    {
38        decoders::charsets::single_byte::decoder_cp1254(bytes)
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn encode() {
48        let engine = CP1254Engine{};
49
50        let encoded = engine.encode("Hello").unwrap();
51        assert_eq!(encoded, &[0x48, 0x65, 0x6c, 0x6c, 0x6f]);
52
53        let encoded = engine.encode("é").unwrap();
54        assert_eq!(encoded, &[0xe9]);
55
56        let encoded = engine.encode("€").unwrap();
57        assert_eq!(encoded, &[0x80]);
58
59        assert!(engine.encode("😀").is_none());
60    }
61
62    #[test]
63    fn decode()
64    {
65        let engine = CP1254Engine{};
66
67        let decoded = engine.decode(&[0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]);
68        assert_eq!(decoded, "world!");
69
70        let decoded = engine.decode(&[0xe8]);
71        assert_eq!(decoded, "è");
72
73        let decoded = engine.decode(&[0x81]);
74        assert_eq!(decoded, "�");
75    }
76}