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:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-05-05 14:02:09 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-05-05 14:02:09 +0200
commit47e3b82d0f8c9c4abcaf8588764fa934446dbdc8 (patch)
tree807f4874f1b945b82c00ec5cd666b4173bd09aab /sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs
parentfeat(pkgs/lf-make-map): Init (diff)
downloadnixos-config-47e3b82d0f8c9c4abcaf8588764fa934446dbdc8.zip
feat(pkgs/lf-make-map): Change the key to custom type and add visuals
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.rs145
1 files changed, 84 insertions, 61 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 7de1ca5d..d05d3417 100644
--- a/sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs
+++ b/sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs
@@ -1,4 +1,7 @@
-use std::path::{Path, PathBuf};
+use std::{
+    fmt::Display,
+    path::{Path, PathBuf},
+};
 
 use log::debug;
 
@@ -11,15 +14,42 @@ pub struct Mapping {
 
     pub keys: usize,
 
-    pub key: String,
+    pub key: Vec<MapKey>,
 }
+
+#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Hash, Eq)]
+pub struct MapKey {
+    value: 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()
+    }
+}
+
+impl Display for MapKey {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        f.write_str(&self.value)
+    }
+}
+
+impl Display for Mapping {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{}", self.raw_path.display())
+    }
+}
+
 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_key(raw_path.to_str().expect("Should be a valid &str"));
+        let key = Self::path_to_keys(raw_path.to_str().expect("Should be a valid &str"));
 
         Self {
             raw_path: raw_path.to_owned(),
@@ -28,71 +58,64 @@ impl Mapping {
         }
     }
 
-    fn path_to_key(path: &str) -> String {
-        let key: String = path
-            .split('/')
-            .map(|part| part.chars().nth(0).expect("Must have a first element"))
-            .collect();
-        debug!("'{}' -> '{}'", path, key);
+    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));
         key
     }
-}
 
-pub fn gen_hot_key(path: &Path, base_path: &Path, amount_of_chars: usize) -> String {
-    let path_filename_as_str = path
-        .file_name()
-        .expect("All paths here should have a file name")
-        .to_str()
-        .expect("The OSstr should be convertible");
-
-    let mut return_val = String::from("g");
-    if path != base_path {
-        return_val.push(
-            base_path
-                .file_name()
-                .expect("All paths here should have a file name")
-                .to_str()
-                .expect("The OSstr should be convertible")
-                .chars()
-                .nth(0)
-                .expect("All names should have a first char"),
-        );
-    }
-    if path_filename_as_str.contains("_") {
-        path_filename_as_str.split("_").for_each(|a| {
-            return_val.push(
-                a.chars()
-                    .nth(0)
-                    .expect("All names should have a first char"),
-            )
-        });
-    } else {
-        if path == base_path {
-            return_val.push(
-                path_filename_as_str
-                    .chars()
-                    .nth(0)
-                    .expect("All names should have a first char"),
-            );
+    fn part_path_to_key(part: &str) -> MapKey {
+        let value = if part.contains('_') {
+            part.split('_').filter_map(|ch| ch.chars().nth(0)).collect()
+        } else if part.contains('-') {
+            part.split('-').filter_map(|ch| ch.chars().nth(0)).collect()
         } else {
-            for a in 0..amount_of_chars {
-                return_val.push(if let Some(b) = path_filename_as_str.chars().nth(a) {
-                    b
-                } else {
-                    path_filename_as_str
-                        .chars()
-                        .nth(0)
-                        .expect("All names should have a first char")
-                });
-            }
-        }
-    }
-    if path == base_path {
-        return_val.push('.');
+            part.chars()
+                .nth(0)
+                .expect("Must have a first element")
+                .to_string()
+        };
+        MapKey::new(value)
     }
-    return_val
 }
 
+// pub fn gen_hot_key(path: &Path, base_path: &Path, amount_of_chars: usize) -> String {
+//     let mut return_val = String::from("g");
+//     if path_filename_as_str.contains("_") {
+//         path_filename_as_str.split("_").for_each(|a| {
+//             return_val.push(
+//                 a.chars()
+//                     .nth(0)
+//                     .expect("All names should have a first char"),
+//             )
+//         });
+//     } else {
+//         if path == base_path {
+//             return_val.push(
+//                 path_filename_as_str
+//                     .chars()
+//                     .nth(0)
+//                     .expect("All names should have a first char"),
+//             );
+//         } else {
+//             for a in 0..amount_of_chars {
+//                 return_val.push(if let Some(b) = path_filename_as_str.chars().nth(a) {
+//                     b
+//                 } else {
+//                     path_filename_as_str
+//                         .chars()
+//                         .nth(0)
+//                         .expect("All names should have a first char")
+//                 });
+//             }
+//         }
+//     }
+//     if path == base_path {
+//         return_val.push('.');
+//     }
+//     return_val
+// }
+
 #[cfg(test)]
 mod tests {
     use super::*;