use std::{borrow::Cow, cmp::Ordering, collections::HashMap, iter::repeat};
use crate::{
- encoding::{get_encoding, Error as EncodingError},
+ encoding::{get_encoding, Error as EncodingError, default_encoding},
endian::Endian,
format::{Error as FormatError, Spec, UncheckedSpec},
identifier::{Error as IdError, Identifier},
#[error("{0}")]
EncodingError(EncodingError),
+ #[error("Using default encoding {0}.")]
+ UsingDefaultEncoding(String),
+
#[error("Variable record at offset {offset:#x} specifies width {width} not in valid range [-1,255).")]
InvalidVariableWidth { offset: u64, width: i32 },
Err(err) => {
warn(Error::EncodingError(err));
// Warn that we're using the default encoding.
-
+ default_encoding()
}
};
+use encoding_rs::{Encoding, UTF_8};
+
include!(concat!(env!("OUT_DIR"), "/encodings.rs"));
pub fn codepage_from_encoding(encoding: &str) -> Option<u32> {
}
use thiserror::Error as ThisError;
+
+use crate::locale_charset::locale_charset;
#[derive(ThisError, Debug)]
pub enum Error {
#[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.")]
Ebcdic,
}
-/// Returns the character set used by the locale configured in the operating
-/// system. This should implement roughly the same behavior as the function
-/// with the same name in Gnulib. Until then, we'll just use a default.
-pub fn locale_charset() -> &'static str {
- "UTF-8"
+
+pub fn default_encoding() -> &'static Encoding {
+ lazy_static! {
+ static ref DEFAULT_ENCODING: &'static Encoding = {
+ Encoding::for_label(locale_charset()).unwrap_or(&UTF_8)
+ };
+ }
+ DEFAULT_ENCODING
}
pub fn get_encoding(encoding: Option<&str>, character_code: Option<i32>) -> Result<&str, Error> {