dm_database_driver_log/
encoding.rs1use encoding::all::GB18030;
4use encoding::{DecoderTrap, Encoding};
5
6#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
8pub enum FileEncodingHint {
9 #[default]
11 Auto,
12 Utf8,
14 Gb18030,
16}
17
18pub(crate) fn decode(bytes: &[u8], hint: FileEncodingHint) -> String {
23 match hint {
24 FileEncodingHint::Utf8 => String::from_utf8_lossy(bytes).into_owned(),
25 FileEncodingHint::Gb18030 => decode_gb18030(bytes),
26 FileEncodingHint::Auto => match std::str::from_utf8(bytes) {
27 Ok(text) => text.to_owned(),
28 Err(_) => decode_gb18030(bytes),
29 },
30 }
31}
32
33fn decode_gb18030(bytes: &[u8]) -> String {
34 GB18030
35 .decode(bytes, DecoderTrap::Strict)
36 .unwrap_or_else(|_| String::from_utf8_lossy(bytes).into_owned())
37}