work
authorBen Pfaff <blp@cs.stanford.edu>
Sat, 4 Nov 2023 23:31:19 +0000 (16:31 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sat, 4 Nov 2023 23:31:19 +0000 (16:31 -0700)
rust/src/cooked.rs
rust/src/encoding.rs

index ef4b79ccf46cc5ab5e9c219e67dbb4f259c6ffdd..1749ecc301a0bab8e741ab613c22155276267b66 100644 (file)
@@ -1,7 +1,7 @@
 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},
@@ -25,6 +25,9 @@ pub enum Error {
     #[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 },
 
@@ -226,7 +229,7 @@ pub fn decode<T>(headers: Vec<raw::Record>, warn: &impl Fn(Error)) -> Result<Vec
         Err(err) => {
             warn(Error::EncodingError(err));
             // Warn that we're using the default encoding.
-            
+            default_encoding()
         }
     };
 
index 3509b73954930fee0b64ba041fc1f65484949010..d135b8e9e6fe140cbc62e851317143979b5b40a0 100644 (file)
@@ -1,3 +1,5 @@
+use encoding_rs::{Encoding, UTF_8};
+
 include!(concat!(env!("OUT_DIR"), "/encodings.rs"));
 
 pub fn codepage_from_encoding(encoding: &str) -> Option<u32> {
@@ -7,6 +9,8 @@ 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.")]
@@ -19,11 +23,14 @@ pub enum Error {
     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> {