work
[pspp] / rust / src / encoding.rs
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> {