use crate::alloc_prelude::*;
use crate::error::{Result, XmlError};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
Utf8,
Utf8Bom,
Utf16Le,
Utf16Be,
Iso8859_1,
Windows1252,
Ascii,
}
fn decode_windows_1252_byte(b: u8) -> char {
match b {
0x80 => '€',
0x82 => '‚',
0x83 => 'ƒ',
0x84 => '„',
0x85 => '…',
0x86 => '†',
0x87 => '‡',
0x88 => 'ˆ',
0x89 => '‰',
0x8A => 'Š',
0x8B => '‹',
0x8C => 'Œ',
0x8E => 'Ž',
0x91 => '‘',
0x92 => '’',
0x93 => '“',
0x94 => '”',
0x95 => '•',
0x96 => '–',
0x97 => '—',
0x98 => '˜',
0x99 => '™',
0x9A => 'š',
0x9B => '›',
0x9C => 'œ',
0x9E => 'ž',
0x9F => 'Ÿ',
other => other as char,
}
}
pub fn decode_with_encoding(bytes: &[u8], encoding: &str) -> Result<(String, Format)> {
let enc_trimmed = encoding.trim().to_uppercase();
match enc_trimmed.as_str() {
"UTF-8" | "UTF8" => detect_encoding_and_strip_bom(bytes),
"ISO-8859-1" | "ISO_8859_1" | "LATIN1" | "LATIN-1" => {
let s: String = bytes.iter().map(|&b| b as char).collect();
Ok((s, Format::Iso8859_1))
}
"WINDOWS-1252" | "WINDOWS_1252" | "CP1252" => {
let s: String = bytes.iter().map(|&b| decode_windows_1252_byte(b)).collect();
Ok((s, Format::Windows1252))
}
"US-ASCII" | "ASCII" => {
for &b in bytes {
if b > 127 {
return Err(XmlError::Io("Byte out of 7-bit US-ASCII range".into()));
}
}
let s: String = bytes.iter().map(|&b| b as char).collect();
Ok((s, Format::Ascii))
}
_ => Err(XmlError::Io(format!(
"Unsupported character encoding: {encoding}"
))),
}
}
pub fn detect_encoding_and_strip_bom(bytes: &[u8]) -> Result<(String, Format)> {
let (content, enc) = babbel_core::encoding::detect_encoding_and_strip_bom(bytes)
.map_err(|e| XmlError::Io(e.to_string()))?;
let fmt = match enc {
babbel_core::encoding::Encoding::Utf8 => {
if bytes.starts_with(&[0xEF, 0xBB, 0xBF]) {
Format::Utf8Bom
} else {
Format::Utf8
}
}
babbel_core::encoding::Encoding::Utf16Le => Format::Utf16Le,
babbel_core::encoding::Encoding::Utf16Be => Format::Utf16Be,
_ => Format::Utf8,
};
Ok((content.into_owned(), fmt))
}