work on character encoding
[pspp] / rust / src / encoding.rs
index d22f6927477218f59f564dd6a93a8bfd79d77d3c..3d585a6b635f94fa4f69284a96fcbeb085a778bf 100644 (file)
@@ -9,9 +9,14 @@ pub fn codepage_from_encoding(encoding: &str) -> Option<u32> {
 use thiserror::Error as ThisError;
 #[derive(ThisError, Debug)]
 pub enum Error {
-    #[error("This system file does not indicate its own character encoding.  Using default encoding {0}.  For best results, specify an encoding explicitly.  Use SYSFILE INFO with ENCODING=\"DETECT\" to analyze the possible encodings.")]
-    NoEncoding(String),
-    
+    #[error("This system file does not indicate its own character encoding.  xFor best results, specify an encoding explicitly.  Use SYSFILE INFO with ENCODING=\"DETECT\" to analyze the possible encodings.")]
+    NoEncoding,
+
+    #[error("This system file encodes text strings with unknown code page {0}.")]
+    UnknownCodepage(u32),
+
+    #[error("This system file is encoded in EBCDIC, which is not supported.")]
+    Ebcdic,
 }
 
 /// Returns the character set used by the locale configured in the operating
@@ -21,25 +26,26 @@ pub fn locale_charset() -> &'static str {
     "UTF-8"
 }
 
-/*
-pub fn encoding_from_hints(encoding: Option<&str>, codepage: Option<u32>) -> Result<&str, ()> {
-    let label = if encoding.is_some() {
-        encoding
-    } else if let Some(codepage) = codepage {
+pub fn get_encoding(encoding: Option<&str>, character_code: Option<u32>) -> Result<&str, Error> {
+    if let Some(encoding) = encoding {
+        Ok(encoding)
+    } else if let Some(codepage) = character_code {
         match codepage {
-            1 => Some("EBCDIC-US"),
+            1 => Err(Error::Ebcdic),
             2 | 3 => {
                 // These ostensibly mean "7-bit ASCII" and "8-bit ASCII"[sic]
                 // respectively.  However, many files have character code 2 but
                 // data which are clearly not ASCII.  Therefore, ignore these
                 // values.
-                None
-            },
-            4 => Some("MS_KANJI"),
-            _ => CODEPAGE_NUMBER_TO_NAME.get(&codepage).copied()
+                Err(Error::NoEncoding)
+            }
+            4 => Ok("MS_KANJI"),
+            _ => CODEPAGE_NUMBER_TO_NAME
+                .get(&codepage)
+                .copied()
+                .ok_or(Error::UnknownCodepage(codepage)),
         }
     } else {
-        None
-    };
+        Err(Error::NoEncoding)
+    }
 }
-*/