about summary refs log tree commit diff stats
path: root/sys/nixpkgs/pkgs/lf-make-map/src/generator/mod.rs
blob: c03b33dc585cb20674502bd943473d0cf3988b37 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::path::PathBuf;

use anyhow::{Context, Result};
use log::trace;
use walkdir::{DirEntry, WalkDir};

use crate::mapping::{map_tree::MappingTree, MapKey, Mapping};

pub struct MappingsGenerator {
    mappings: MappingTree,
    paths_to_process: Vec<PathBuf>,
}

fn is_dir(entry: &DirEntry) -> bool {
    entry.file_type().is_dir()
}

impl MappingsGenerator {
    pub async fn new(
        directories_to_scan: Vec<PathBuf>,
        max_depth: usize,
        home_path: PathBuf,
    ) -> Result<Self> {
        let mut mappings = MappingTree::new();

        for dir in directories_to_scan {
            for dir2 in WalkDir::new(&dir)
                .max_depth(max_depth)
                .into_iter()
                .filter_entry(|e| is_dir(e))
            {
                let directory =
                    dir2.with_context(|| format!("Failed to read dir ('{}')", &dir.display()))?;

                trace!("Processed '{}'..", directory.path().display());

                let mapping = Mapping::new(&home_path, directory.path().to_path_buf());
                let mapping_key = mapping.key.clone();
                mappings.insert(&mapping_key, mapping).with_context(|| {
                    format!(
                        "Failed to insert '{}' for path: '{}'\nMapTree is now: \n{}",
                        MapKey::display(&mapping_key),
                        directory.path().display(),
                        mappings,
                    )
                })?;
            }
        }
        todo!()
    }
}