work
[pspp] / rust / src / cooked.rs
index 095bfe507db1bc1297168fa7e4a8bfdefaa072f8..e17e9f3e7a87271bffcf5bc6b97d1b0956570bdf 100644 (file)
@@ -8,7 +8,7 @@ use crate::{
     endian::Endian,
     format::{Error as FormatError, Spec, UncheckedSpec},
     identifier::{Error as IdError, Identifier},
-    raw::{self, RawStr, RawString, VarType, RawDocumentLine},
+    raw::{self, RawStr, RawString, VarType, RawDocumentLine, VarDisplayRecord},
 };
 use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
 use encoding_rs::{DecoderResult, Encoding};
@@ -151,12 +151,6 @@ pub enum Error {
     #[error("Text string contains invalid bytes for {encoding} encoding: {text}")]
     MalformedString { encoding: String, text: String },
 
-    #[error("Invalid variable measurement level value {0}")]
-    InvalidMeasurement(u32),
-
-    #[error("Invalid variable display alignment value {0}")]
-    InvalidAlignment(u32),
-
     #[error("Details TBD")]
     TBD,
 }
@@ -372,9 +366,7 @@ pub fn decode(
     }
     // XXX weight
     if let Some(raw) = h.var_display {
-        if let Some(vdr) = VarDisplayRecord::try_decode(&mut decoder, raw, warn)? {
-            output.push(Record::VarDisplay(vdr))
-        }
+        output.push(Record::VarDisplay(raw.clone()));
     }
 
     // Decode records that use short names.
@@ -1169,93 +1161,6 @@ impl VariableAttributeRecord {
     }
 }
 
-#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
-pub enum Measure {
-    Nominal,
-    Ordinal,
-    Scale,
-}
-
-impl Measure {
-    fn try_decode(source: u32) -> Result<Option<Measure>, Error> {
-        match source {
-            0 => Ok(None),
-            1 => Ok(Some(Measure::Nominal)),
-            2 => Ok(Some(Measure::Ordinal)),
-            3 => Ok(Some(Measure::Scale)),
-            _ => Err(Error::InvalidMeasurement(source)),
-        }
-    }
-}
-
-#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
-pub enum Alignment {
-    Left,
-    Right,
-    Center,
-}
-
-impl Alignment {
-    fn try_decode(source: u32) -> Result<Option<Alignment>, Error> {
-        match source {
-            0 => Ok(None),
-            1 => Ok(Some(Alignment::Left)),
-            2 => Ok(Some(Alignment::Right)),
-            3 => Ok(Some(Alignment::Center)),
-            _ => Err(Error::InvalidAlignment(source)),
-        }
-    }
-}
-
-#[derive(Clone, Debug)]
-pub struct VarDisplay {
-    pub measure: Option<Measure>,
-    pub width: Option<u32>,
-    pub alignment: Option<Alignment>,
-}
-
-#[derive(Clone, Debug)]
-pub struct VarDisplayRecord(pub Vec<VarDisplay>);
-
-impl TryDecode for VarDisplayRecord {
-    type Input = raw::VarDisplayRecord;
-    fn try_decode(
-        decoder: &mut Decoder,
-        input: &Self::Input,
-        warn: impl Fn(Error),
-    ) -> Result<Option<Self>, Error> {
-        let n_vars = decoder.variables.len();
-        let n_per_var = if input.0.len() == 3 * n_vars {
-            3
-        } else if input.0.len() == 2 * n_vars {
-            2
-        } else {
-            return Err(Error::TBD);
-        };
-
-        let var_displays = input
-            .0
-            .chunks(n_per_var)
-            .map(|chunk| {
-                let (measure, width, alignment) = match n_per_var == 3 {
-                    true => (chunk[0], Some(chunk[1]), chunk[2]),
-                    false => (chunk[0], None, chunk[1]),
-                };
-                let measure = Measure::try_decode(measure).warn_on_error(&warn).flatten();
-                let alignment = Alignment::try_decode(alignment)
-                    .warn_on_error(&warn)
-                    .flatten();
-                VarDisplay {
-                    measure,
-                    width,
-                    alignment,
-                }
-            })
-            .collect();
-        Ok(Some(VarDisplayRecord(var_displays)))
-    }
-}
-
 #[derive(Clone, Debug)]
 pub enum MultipleResponseType {
     MultipleDichotomy {