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 12:59:36 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-05-05 12:59:36 +0200
commit023b7cd0ada0cb153769bdcc931ef04512d125c4 (patch)
tree584315a010a9eccdd182e48801496ea4e226bfcb /sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs
parentbuild(treewide): Update (diff)
downloadnixos-config-023b7cd0ada0cb153769bdcc931ef04512d125c4.tar.gz
nixos-config-023b7cd0ada0cb153769bdcc931ef04512d125c4.zip
feat(pkgs/lf-make-map): Init
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.rs122
1 files changed, 122 insertions, 0 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
new file mode 100644
index 00000000..7de1ca5d
--- /dev/null
+++ b/sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs
@@ -0,0 +1,122 @@
+use std::path::{Path, PathBuf};
+
+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: String,
+}
+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"));
+
+        Self {
+            raw_path: raw_path.to_owned(),
+            keys: key.len(),
+            key,
+        }
+    }
+
+    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);
+        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"),
+            );
+        } 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::*;
+    #[test]
+    fn gen_hot_key_test() {
+        let gen1 = gen_hot_key(
+            Path::new("/home/dt/repos/java_script"),
+            Path::new("/home/dt/repos"),
+            1,
+        );
+        assert_eq!(gen1, "grjs".to_owned());
+    }
+    #[test]
+    fn gen_hot_key_test_for_same_names() {
+        let gen1 = gen_hot_key(Path::new("/home/dt/repos/"), Path::new("/home/dt/repos"), 1);
+        assert_eq!(gen1, "gr.".to_owned());
+    }
+    #[test]
+    fn gen_hot_key_test_for_non_underscore_name() {
+        let gen1 = gen_hot_key(
+            Path::new("/home/dt/repos/rust"),
+            Path::new("/home/dt/repos"),
+            1,
+        );
+        assert_eq!(gen1, "grr".to_owned());
+    }
+}