1use std::borrow::Cow;
2use encoding::all::{GB18030, GBK, UTF_8, KOI8_R};
3use encoding::{DecoderTrap, EncoderTrap, Encoding};
4use encoding_rs::EUC_KR;
5use textcode::gb2312;
6
7
8pub fn code_to_utf8(mut code: &str, data: Vec<u8>) -> String {
9 if data.is_empty() {
10 return "".to_string();
11 }
12 if code.is_empty() {
13 code = "utf8";
14 }
15 match code.to_lowercase().as_str() {
16 "utf8" | "utf-8" => {
17 match from_utf8(data.clone()) {
18 Ok(e) => e,
19 Err(_) => {
20 code_to_utf8("gbk", data.clone())
21 }
22 }
23 }
24 "gbk" => {
25 match gbk_to_utf8(data.clone()) {
26 Ok(e) => e,
27 Err(_) => {
28 code_to_utf8("gb18030", data.clone())
29 }
30 }
31 }
32 "gb18030" => match gb18030_to_utf8(data.clone()) {
33 Ok(e) => e,
34 Err(_) => {
35 code_to_utf8("gb2312", data.clone())
36 }
37 },
38 "gb2312" => gb2312_to_utf8(data.clone()),
39 "big5" => big5_to_utf8(data.clone()),
40 "iso-8859-1" => iso_8859_1_to_utf8(data.clone()),
41 "iso-8859-2" => iso_8859_2_to_utf8(data.clone()),
42 "iso-8859-3" => iso_8859_3_to_utf8(data.clone()),
43 "iso-8859-4" => iso_8859_4_to_utf8(data.clone()),
44 "iso-8859-5" => iso_8859_5_to_utf8(data.clone()),
45 "iso-8859-6" => iso_8859_6_to_utf8(data.clone()),
46 "iso-8859-7" => iso_8859_7_to_utf8(data.clone()),
47 "iso-8859-8" => iso_8859_8_to_utf8(data.clone()),
48 "iso-8859-9" => iso_8859_9_to_utf8(data.clone()),
49 "iso-8859-10" => iso_8859_10_to_utf8(data.clone()),
50 "iso-8859-11" => iso_8859_11_to_utf8(data.clone()),
51 "iso-8859-13" => iso_8859_13_to_utf8(data.clone()),
52 "iso-8859-14" => iso_8859_14_to_utf8(data.clone()),
53 "iso-8859-15" => iso_8859_15_to_utf8(data.clone()),
54 "iso-8859-16" => iso_8859_16_to_utf8(data.clone()),
55 "iso-6937" => iso_6937_to_utf8(data.clone()),
56 "koi8-r" => koi8r_to_utf8(data.clone()),
57 "euc-kr" => euc_kr_to_utf8(data.clone()),
58 _ => from_utf8(data.clone()).unwrap_or("".to_string()),
59 }
60}
61
62
63pub fn gbk_to_utf8(data: Vec<u8>) -> Result<String, Cow<'static, str>> {
64 GBK.decode(&data, DecoderTrap::Strict)
65}
66
67pub fn to_gbk(str: &str) -> Vec<u8> {
68 GBK.encode(str, EncoderTrap::Strict).unwrap()
69}
70
71pub fn from_utf8(data: Vec<u8>) -> Result<String, Cow<'static, str>> {
72 UTF_8.decode(&data, DecoderTrap::Strict)
73}
74
75pub fn to_utf8(str: &str) -> Vec<u8> {
76 UTF_8.encode(str, EncoderTrap::Strict).unwrap()
77}
78
79pub fn gb18030_to_utf8(data: Vec<u8>) -> Result<String, Cow<'static, str>> {
80 GB18030.decode(&data, DecoderTrap::Strict)
81}
82
83pub fn to_gb18030(str: &str) -> Vec<u8> {
84 GB18030.encode(str, EncoderTrap::Strict).unwrap()
85}
86
87pub fn gb2312_to_utf8(data: Vec<u8>) -> String {
88 gb2312::decode_to_string(&data)
89}
90
91pub fn to_gb2312(str: &str) -> Vec<u8> {
92 gb2312::encode_to_vec(str)
93}
94
95pub fn iso_8859_1_to_utf8(data: Vec<u8>) -> String {
97 textcode::iso8859_1::decode_to_string(&data)
98}
99
100pub fn iso_8859_2_to_utf8(data: Vec<u8>) -> String {
101 textcode::iso8859_2::decode_to_string(&data)
102}
103
104pub fn iso_8859_3_to_utf8(data: Vec<u8>) -> String {
105 textcode::iso8859_3::decode_to_string(&data)
106}
107
108pub fn iso_8859_4_to_utf8(data: Vec<u8>) -> String {
109 textcode::iso8859_4::decode_to_string(&data)
110}
111
112pub fn iso_8859_5_to_utf8(data: Vec<u8>) -> String {
113 textcode::iso8859_5::decode_to_string(&data)
114}
115
116pub fn iso_8859_6_to_utf8(data: Vec<u8>) -> String {
117 textcode::iso8859_6::decode_to_string(&data)
118}
119
120pub fn iso_8859_7_to_utf8(data: Vec<u8>) -> String {
121 textcode::iso8859_7::decode_to_string(&data)
122}
123
124pub fn iso_8859_8_to_utf8(data: Vec<u8>) -> String {
125 textcode::iso8859_8::decode_to_string(&data)
126}
127
128pub fn iso_8859_9_to_utf8(data: Vec<u8>) -> String {
129 textcode::iso8859_9::decode_to_string(&data)
130}
131
132pub fn iso_8859_10_to_utf8(data: Vec<u8>) -> String {
133 textcode::iso8859_10::decode_to_string(&data)
134}
135
136pub fn iso_8859_11_to_utf8(data: Vec<u8>) -> String {
137 textcode::iso8859_11::decode_to_string(&data)
138}
139
140pub fn iso_8859_13_to_utf8(data: Vec<u8>) -> String {
141 textcode::iso8859_13::decode_to_string(&data)
142}
143
144pub fn iso_8859_14_to_utf8(data: Vec<u8>) -> String {
145 textcode::iso8859_14::decode_to_string(&data)
146}
147
148pub fn iso_8859_15_to_utf8(data: Vec<u8>) -> String {
149 textcode::iso8859_15::decode_to_string(&data)
150}
151
152pub fn iso_8859_16_to_utf8(data: Vec<u8>) -> String {
153 textcode::iso8859_16::decode_to_string(&data)
154}
155
156pub fn iso_6937_to_utf8(data: Vec<u8>) -> String {
157 textcode::iso6937::decode_to_string(&data)
158}
159
160pub fn big5_to_utf8(data: Vec<u8>) -> String {
161 mail_parser::decoders::charsets::multi_byte::decoder_big5(&data)
162}
163
164pub fn koi8r_to_utf8(data: Vec<u8>) -> String {
165 KOI8_R.decode(&data, DecoderTrap::Strict).unwrap()
166}
167pub fn euc_kr_to_utf8(data: Vec<u8>) -> String {
168 let (decoded_str, _, _) = EUC_KR.decode(data.as_slice());
169 match decoded_str {
170 Cow::Borrowed(s) => s.to_string(),
171 Cow::Owned(s) => s,
172 }
173}
174use urlencoding::{decode, encode};
175
176pub fn urlencoding_decode(str: &str) -> String {
178 decode(str).unwrap_or(Cow::from(str)).to_string()
179}
180
181pub fn urlencoding_encode(str: &str) -> String {
183 encode(str).to_string()
184}