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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
// nixos-config - My current NixOS configuration
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of my nixos-config.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use clap::Parser;
use cli::{Args, Command};
use log::trace;
use mapping::map_key::MapKey;
use walkdir::{DirEntry, WalkDir};
use crate::mapping::MappingsTrie;
mod cli;
mod mapping;
fn main() -> anyhow::Result<()> {
let args = Args::parse();
stderrlog::new()
.module(module_path!())
.quiet(args.quiet)
.show_module_names(false)
.color(stderrlog::ColorChoice::Auto)
.verbosity(args.verbosity as usize)
.timestamp(stderrlog::Timestamp::Off)
.init()?;
let mut mappings = MappingsTrie::new();
let relevant_directories = match &args.command {
Command::Visualize { options } => &options.relevant_directories,
Command::Generate { options } => &options.relevant_directories,
Command::Interactive { options } => &options.relevant_directories,
};
for dir in relevant_directories {
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 (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() + &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 '{}')",
path.display(),
path_to_strip.display(),
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))?)?,
)
.with_context(|| format!("Failed to include path: '{}'", path.display()))?;
}
mappings.add_trie(&keys, local_mappings)?;
}
current_depth += 1;
}
match args.command {
Command::Visualize { .. } => println!("{}", mappings.0),
Command::Generate { .. } => println!("{}", mappings.to_lf_mappings(args.home_name)),
Command::Interactive { .. } => mappings.interactive_start(args.home_name)?,
}
Ok(())
}
fn extend(base: &str, value: &str) -> Result<String> {
let base_path = PathBuf::from(base);
let value_path = PathBuf::from(value);
Ok(path_to_str(&base_path.join(&value_path))?.to_owned())
}
fn is_hidden(entry: &DirEntry) -> bool {
entry
.file_name()
.to_str()
.map(|s| s.starts_with("."))
.unwrap_or(false)
}
fn is_dir(entry: &DirEntry) -> bool {
entry.file_type().is_dir()
}
fn strip_path<'a>(path: &'a Path, to_strip: &Path) -> Result<&'a Path> {
path.strip_prefix(to_strip).with_context(|| {
format!(
"'{}' is not under the specified home path ('{}')!",
path.display(),
to_strip.display()
)
})
}
fn path_to_str(path: &Path) -> Result<&str> {
path.to_str().with_context(|| {
format!(
"\
Can't derive a keymapping from path: '{}' \
because it can't be turned to a string
",
path.display()
)
})
}
|