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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#![warn(missing_docs)]
#[cfg(windows)]
pub mod windows;
pub mod posix;
use std::io::Result;
pub trait Encoder {
fn to_string(self: &Self, data: &[u8]) -> Result<String>;
fn to_bytes(self: &Self, data: &str) -> Result<Vec<u8>>;
}
pub enum Encoding {
ANSI,
OEM,
}
#[cfg(windows)]
trait CodePage {
fn codepage(self: &Self) -> u32;
}
#[cfg(windows)]
impl CodePage for Encoding {
fn codepage(self: &Self) -> u32 {
extern crate winapi;
match self {
&Encoding::ANSI => winapi::CP_ACP,
&Encoding::OEM => winapi::CP_OEMCP,
}
}
}
#[cfg(windows)]
impl Encoder for Encoding {
fn to_string(self: &Self, data: &[u8]) -> Result<String> {
windows::EncoderCodePage(self.codepage()).to_string(data)
}
fn to_bytes(self: &Self, data: &str) -> Result<Vec<u8>> {
windows::EncoderCodePage(self.codepage()).to_bytes(data)
}
}
#[cfg(not(windows))]
impl Encoder for Encoding {
fn to_string(self: &Self, data: &[u8]) -> Result<String> {
posix::EncoderUtf8.to_string(data)
}
fn to_bytes(self: &Self, data: &str) -> Result<Vec<u8>> {
posix::EncoderUtf8.to_bytes(data)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn oem_to_string_test() {
to_string_test(Encoding::OEM);
}
#[test]
fn ansi_to_string_test() {
to_string_test(Encoding::ANSI);
}
#[test]
fn string_to_oem_test() {
from_string_test(Encoding::OEM);
}
#[test]
fn string_to_ansi_test() {
from_string_test(Encoding::ANSI);
}
fn to_string_test(encoding: Encoding) {
assert_eq!(encoding.to_string(b"Test").unwrap(), "Test");
}
fn from_string_test(encoding: Encoding) {
assert_eq!(encoding.to_bytes("Test").unwrap(), b"Test");
}
}