about summary refs log tree commit diff stats
path: root/sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs')
-rw-r--r--sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs109
1 files changed, 59 insertions, 50 deletions
diff --git a/sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs b/sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs
index d05d3417..8bf6bbe0 100644
--- a/sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs
+++ b/sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs
@@ -1,81 +1,90 @@
-use std::{
-    fmt::Display,
-    path::{Path, PathBuf},
-};
+use std::{fmt::Display, hash::Hash};
 
 use log::debug;
 
 pub mod error;
 pub mod map_tree;
 
-#[derive(Debug, Clone)]
-pub struct Mapping {
-    pub raw_path: PathBuf,
-
-    pub keys: usize,
-
-    pub key: Vec<MapKey>,
-}
-
-#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Hash, Eq)]
+#[derive(Clone, Debug, Eq)]
 pub struct MapKey {
-    value: String,
-}
+    pub key: String,
 
-impl MapKey {
-    pub fn new(value: String) -> Self {
-        Self { value }
-    }
-    pub fn display(values: &[Self]) -> String {
-        values.iter().map(|value| value.value.clone()).collect()
-    }
+    resolution: usize,
+
+    /// Part of the path, used to derive the key
+    part_path: String,
 }
 
-impl Display for MapKey {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        f.write_str(&self.value)
+impl Hash for MapKey {
+    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
+        self.key.hash(state)
     }
 }
 
-impl Display for Mapping {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(f, "{}", self.raw_path.display())
+impl PartialEq for MapKey {
+    fn eq(&self, other: &Self) -> bool {
+        self.key == other.key
     }
 }
 
-impl Mapping {
-    pub fn new(home_path: &Path, initial_path: PathBuf) -> Mapping {
-        let raw_path = initial_path
-            .strip_prefix(home_path)
-            .expect("Must always be under the `home_path`");
-
-        let key = Self::path_to_keys(raw_path.to_str().expect("Should be a valid &str"));
-
+impl MapKey {
+    pub fn new_from_part_path(part_path: &str, resolution: usize) -> Self {
+        let key = Self::part_path_to_key(&part_path, resolution);
         Self {
-            raw_path: raw_path.to_owned(),
-            keys: key.len(),
             key,
+            resolution,
+            part_path: part_path.to_owned(),
         }
     }
 
-    fn path_to_keys(path: &str) -> Vec<MapKey> {
-        let key: Vec<MapKey> = path.split('/').map(Self::part_path_to_key).collect();
-        debug!("Will insert: '{}' -> '{}'", path, MapKey::display(&key));
+    pub fn increment(&mut self) {
+        if self.resolution < self.part_path.len() {
+            self.resolution += 1;
+            self.key = Self::part_path_to_key(&self.part_path, self.resolution);
+        } else {
+            let last_char = self.key.chars().last().expect("A last value exists");
+            self.key.push(last_char);
+        }
+    }
+
+    pub fn new_ones_from_path(path: &str, number_of_chars: usize) -> Vec<MapKey> {
+        let key: Vec<MapKey> = path
+            .split('/')
+            .map(|part| Self::new_from_part_path(part, number_of_chars))
+            .collect();
+
+        debug!(
+            "Generated full MapKeys: '{}' -> '{}'",
+            path,
+            MapKey::display(&key)
+        );
         key
     }
 
-    fn part_path_to_key(part: &str) -> MapKey {
+    pub fn display(values: &[Self]) -> String {
+        values.iter().map(|value| value.key.clone()).collect()
+    }
+    fn part_path_to_key(part: &str, number_of_chars: usize) -> String {
         let value = if part.contains('_') {
-            part.split('_').filter_map(|ch| ch.chars().nth(0)).collect()
+            part.split('_')
+                .map(|ch| ch.chars().take(number_of_chars).collect::<Vec<char>>())
+                .flatten()
+                .collect()
         } else if part.contains('-') {
-            part.split('-').filter_map(|ch| ch.chars().nth(0)).collect()
+            part.split('-')
+                .map(|ch| ch.chars().take(number_of_chars).collect::<Vec<char>>())
+                .flatten()
+                .collect()
         } else {
-            part.chars()
-                .nth(0)
-                .expect("Must have a first element")
-                .to_string()
+            part.chars().take(number_of_chars).collect::<String>()
         };
-        MapKey::new(value)
+        value
+    }
+}
+
+impl Display for MapKey {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        f.write_str(&self.key)
     }
 }