1 use encoding_rs::{Encoding, UTF_8};
3 include!(concat!(env!("OUT_DIR"), "/encodings.rs"));
5 pub fn codepage_from_encoding(encoding: &str) -> Option<u32> {
6 CODEPAGE_NAME_TO_NUMBER
7 .get(encoding.to_ascii_lowercase().as_str())
11 use thiserror::Error as ThisError;
13 use crate::locale_charset::locale_charset;
14 #[derive(ThisError, Debug)]
16 #[error("This system file does not indicate its own character encoding. For best results, specify an encoding explicitly. Use SYSFILE INFO with ENCODING=\"DETECT\" to analyze the possible encodings.")]
19 #[error("This system file encodes text strings with unknown code page {0}.")]
22 #[error("This system file is encoded in EBCDIC, which is not supported.")]
27 pub fn default_encoding() -> &'static Encoding {
29 static ref DEFAULT_ENCODING: &'static Encoding = {
30 Encoding::for_label(locale_charset()).unwrap_or(&UTF_8)
36 pub fn get_encoding(encoding: Option<&str>, character_code: Option<i32>) -> Result<&str, Error> {
37 if let Some(encoding) = encoding {
39 } else if let Some(codepage) = character_code {
41 1 => Err(Error::Ebcdic),
43 // These ostensibly mean "7-bit ASCII" and "8-bit ASCII"[sic]
44 // respectively. However, many files have character code 2 but
45 // data which are clearly not ASCII. Therefore, ignore these
47 Err(Error::NoEncoding)
50 _ => CODEPAGE_NUMBER_TO_NAME
53 .ok_or(Error::UnknownCodepage(codepage)),
56 Err(Error::NoEncoding)