about summary refs log tree commit diff stats
path: root/pkgs/by-name/lf/lf-make-map/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/by-name/lf/lf-make-map/src/main.rs')
-rw-r--r--pkgs/by-name/lf/lf-make-map/src/main.rs60
1 files changed, 27 insertions, 33 deletions
diff --git a/pkgs/by-name/lf/lf-make-map/src/main.rs b/pkgs/by-name/lf/lf-make-map/src/main.rs
index d3cc9f6f..d5d934e1 100644
--- a/pkgs/by-name/lf/lf-make-map/src/main.rs
+++ b/pkgs/by-name/lf/lf-make-map/src/main.rs
@@ -14,10 +14,10 @@ use anyhow::{Context, Result};
 use clap::Parser;
 use cli::{Args, Command};
 use log::trace;
-use mapping::map_tree::MappingTree;
+use mapping::map_key::MapKey;
 use walkdir::{DirEntry, WalkDir};
 
-use crate::mapping::MapKey;
+use crate::mapping::MappingsTrie;
 
 mod cli;
 mod mapping;
@@ -34,7 +34,7 @@ fn main() -> anyhow::Result<()> {
         .timestamp(stderrlog::Timestamp::Off)
         .init()?;
 
-    let mut mappings = MappingTree::new();
+    let mut mappings = MappingsTrie::new();
 
     let relevant_directories = match &args.command {
         Command::Visualize { options } => &options.relevant_directories,
@@ -42,68 +42,62 @@ fn main() -> anyhow::Result<()> {
     };
 
     for dir in relevant_directories {
-        trace!("Processing '{}'..", dir.display());
-        let path = strip_path(&dir, &args.home_name)?;
+        trace!("START Processing '{}'..", dir.display());
+        let path = strip_path(dir, &args.home_name)?;
 
         mappings
             .include(path_to_str(path)?)
             .with_context(|| format!("Failed to include path: '{}'", path.display()))?;
+        trace!("END Finished processing {}.", dir.display());
+    }
+
+    trace!("Generated mappings for the relevant directories. Starting expanding to max depth.");
+    if log::log_enabled!(log::Level::Trace) {
+        eprintln!("{mappings}");
     }
 
     let home = path_to_str(&args.home_name)?.to_owned();
 
     let mut current_depth = 1;
     while current_depth != args.depth {
-        for (key, value) in mappings.iter(false) {
-            trace!(
-                "Adding to child ('{}' -> '{}')",
-                MapKey::display(&key),
-                value
-            );
-
-            let mut local_mappings = MappingTree::new();
-            for dir in WalkDir::new(extend(&home, &value)?)
+        for (keys, child) in mappings.0.iter().filter(|(_, child)| child.expendable) {
+            trace!("Adding to child '{}' ('{}')", MapKey::display(&keys), child);
+
+            let mut local_mappings = MappingsTrie::new();
+            for dir in WalkDir::new(extend(&home, &child.path)?)
                 .min_depth(1)
                 .max_depth(1)
                 .into_iter()
                 .filter_entry(|e| is_dir(e) && !is_hidden(e))
             {
-                let directory = dir
-                    .with_context(|| format!("Failed to read dir ('{}')", home.clone() + &value))?;
-                let path_to_strip = &PathBuf::from(extend(&home, &value)?);
-                let path = strip_path(&directory.path(), &path_to_strip)?;
+                let directory = dir.with_context(|| {
+                    format!("Failed to read dir ('{}')", home.clone() + &child.path)
+                })?;
+                let path_to_strip = &PathBuf::from(extend(&home, &child.path)?);
+                let path = strip_path(directory.path(), path_to_strip)?;
                 trace!(
-                    "Including: '{}' (after stripping '{}' from '{}' -> '{}' + '/' + '{}')",
+                    "Including: '{}' (after stripping '{}' from '{}')",
                     path.display(),
-                    directory.path().display(),
                     path_to_strip.display(),
-                    home,
-                    value
+                    directory.path().display(),
                 );
 
                 let gen_key = MapKey::new_ones_from_path(path_to_str(path)?, 1);
                 local_mappings
                     .insert(
                         &gen_key,
-                        path_to_str(strip_path(&directory.path(), &PathBuf::from(&home))?)?,
+                        path_to_str(strip_path(directory.path(), &PathBuf::from(&home))?)?,
                     )
                     .with_context(|| format!("Failed to include path: '{}'", path.display()))?;
             }
 
-            trace!("{}", local_mappings);
-
-            trace!(
-                "'{}' -> '{:#?}'",
-                MapKey::display(&key),
-                local_mappings.root_node()
-            );
-            mappings.interleave(&key, local_mappings.root_node().to_owned())?;
+            mappings.add_trie(&keys, local_mappings)?;
         }
         current_depth += 1;
     }
 
     match args.command {
-        Command::Visualize { .. } => println!("{}", mappings),
+        Command::Visualize { .. } => println!("{}", mappings.0),
         Command::Generate { .. } => println!("{}", mappings.to_lf_mappings(args.home_name)),
     }
 
@@ -130,7 +124,7 @@ fn is_dir(entry: &DirEntry) -> bool {
 }
 
 fn strip_path<'a>(path: &'a Path, to_strip: &Path) -> Result<&'a Path> {
-    path.strip_prefix(&to_strip).with_context(|| {
+    path.strip_prefix(to_strip).with_context(|| {
         format!(
             "'{}' is not under the specified home path ('{}')!",
             path.display(),