variables parsed
[pspp] / rust / src / dictionary.rs
index 59e1e3a853dcc59191d75d61d650dce27b37be5f..042a294452a2e572bd4dc95ed8b890b77269a839 100644 (file)
@@ -78,6 +78,15 @@ impl VarWidth {
             VarWidth::String(width) => *width.min(&32) as u32,
         }
     }
+
+    pub fn from_raw(raw: impl Into<i32>) -> Result<Self, ()> {
+        let raw: i32 = raw.into();
+        match raw {
+            0 => Ok(Self::Numeric),
+            1..=255 => Ok(Self::String(raw as u16)),
+            _ => Err(()),
+        }
+    }
 }
 
 impl From<VarWidth> for VarType {
@@ -120,6 +129,7 @@ pub struct Dictionary {
     pub encoding: &'static Encoding,
 }
 
+#[derive(Debug)]
 pub struct DuplicateVariableName;
 
 impl Dictionary {
@@ -140,9 +150,10 @@ impl Dictionary {
         }
     }
 
-    pub fn add_var(&mut self, variable: Variable) -> Result<(), DuplicateVariableName> {
-        if self.variables.insert(ByIdentifier::new(variable)) {
-            Ok(())
+    pub fn add_var(&mut self, variable: Variable) -> Result<usize, DuplicateVariableName> {
+        let (index, inserted) = self.variables.insert_full(ByIdentifier::new(variable));
+        if inserted {
+            Ok(index)
         } else {
             Err(DuplicateVariableName)
         }