progress! and clippy!
[pspp] / rust / src / dictionary.rs
index ace97b03f7db5690d2a0ff565d4b0f2ced0ee242..59e1e3a853dcc59191d75d61d650dce27b37be5f 100644 (file)
@@ -1,7 +1,8 @@
 use std::{
+    cmp::Ordering,
     collections::{HashMap, HashSet},
     fmt::Debug,
-    ops::{Bound, RangeBounds}, cmp::Ordering,
+    ops::{Bound, RangeBounds},
 };
 
 use encoding_rs::Encoding;
@@ -12,7 +13,7 @@ use ordered_float::OrderedFloat;
 use crate::{
     format::Spec,
     identifier::{ByIdentifier, HasIdentifier, Identifier},
-    raw::{Alignment, CategoryLabels, Measure, MissingValues, VarType, self, RawStr, Decoder},
+    raw::{self, Alignment, CategoryLabels, Decoder, Measure, MissingValues, RawStr, VarType},
 };
 
 pub type DictIndex = usize;
@@ -119,6 +120,8 @@ pub struct Dictionary {
     pub encoding: &'static Encoding,
 }
 
+pub struct DuplicateVariableName;
+
 impl Dictionary {
     pub fn new(encoding: &'static Encoding) -> Self {
         Self {
@@ -137,11 +140,11 @@ impl Dictionary {
         }
     }
 
-    pub fn add_var(&mut self, variable: Variable) -> Result<(), ()> {
+    pub fn add_var(&mut self, variable: Variable) -> Result<(), DuplicateVariableName> {
         if self.variables.insert(ByIdentifier::new(variable)) {
             Ok(())
         } else {
-            Err(())
+            Err(DuplicateVariableName)
         }
     }
 
@@ -149,6 +152,7 @@ impl Dictionary {
         if from_index != to_index {
             self.variables.move_index(from_index, to_index);
             self.update_dict_indexes(&|index| {
+                #[allow(clippy::collapsible_else_if)]
                 if index == from_index {
                     Some(to_index)
                 } else if from_index < to_index {
@@ -223,8 +227,8 @@ impl Dictionary {
         F: Fn(DictIndex) -> Option<DictIndex>,
     {
         update_dict_index_vec(&mut self.split_file, f);
-        self.weight = self.weight.map(|index| f(index)).flatten();
-        self.filter = self.filter.map(|index| f(index)).flatten();
+        self.weight = self.weight.and_then(f);
+        self.filter = self.filter.and_then(f);
         self.vectors = self
             .vectors
             .drain()
@@ -232,7 +236,7 @@ impl Dictionary {
                 vector_by_id
                     .0
                     .with_updated_dict_indexes(f)
-                    .map(|vector| ByIdentifier::new(vector))
+                    .map(ByIdentifier::new)
             })
             .collect();
         self.mrsets = self
@@ -242,7 +246,7 @@ impl Dictionary {
                 mrset_by_id
                     .0
                     .with_updated_dict_indexes(f)
-                    .map(|mrset| ByIdentifier::new(mrset))
+                    .map(ByIdentifier::new)
             })
             .collect();
         self.variable_sets = self
@@ -252,7 +256,7 @@ impl Dictionary {
                 var_set_by_id
                     .0
                     .with_updated_dict_indexes(f)
-                    .map(|var_set| ByIdentifier::new(var_set))
+                    .map(ByIdentifier::new)
             })
             .collect();
     }
@@ -350,7 +354,7 @@ impl Variable {
             alignment: Alignment::default_for_type(var_type),
             leave,
             short_names: Vec::new(),
-            attributes: HashSet::new()
+            attributes: HashSet::new(),
         }
     }
 }