Skip to main content

lib/
fn_doc_id.rs

1use std::collections::HashMap;
2use std::path::PathBuf;
3
4pub fn generate_ids(paths: &[PathBuf]) -> HashMap<PathBuf, String> {
5    let mut map = HashMap::new();
6    let mut counters: HashMap<String, usize> = HashMap::new();
7
8    // Klonujemy i sortujemy ścieżki, żeby ID były nadawane powtarzalnie
9    let mut sorted_paths = paths.to_vec();
10    sorted_paths.sort();
11
12    for path in sorted_paths {
13        // Ignorujemy foldery, przypisujemy ID tylko plikom
14        if path.is_dir() {
15            continue;
16        }
17        // DODAJEMY .to_string() NA KOŃCU, ABY ZROBIĆ NIEZALEŻNĄ KOPIĘ
18        let file_name = path
19            .file_name()
20            .unwrap_or_default()
21            .to_string_lossy()
22            .to_string();
23
24        // Tutaj .replace() i tak zwraca już własnego Stringa, więc jest bezpiecznie
25        let path_str = path.to_string_lossy().replace('\\', "/");
26
27        // 1. Twarde reguły dla znanych plików
28        if file_name == "Cargo.toml" {
29            map.insert(path.clone(), "TomlCargo".to_string());
30            continue;
31        }
32        if file_name == "Makefile.toml" {
33            map.insert(path.clone(), "TomlMakefile".to_string());
34            continue;
35        }
36        if file_name == "build.rs" {
37            map.insert(path.clone(), "RustBuild".to_string());
38            continue;
39        }
40        if path_str.contains("src/ui/index.slint") {
41            map.insert(path.clone(), "SlintIndex".to_string());
42            continue;
43        }
44
45        // 2. Dynamiczne ID na podstawie ścieżki
46        let prefix = if path_str.contains("src/lib") {
47            if file_name == "mod.rs" {
48                "RustLibMod".to_string()
49            } else {
50                "RustLibPub".to_string()
51            }
52        } else if path_str.contains("src/bin") || path_str.contains("src/main.rs") {
53            "RustBin".to_string()
54        } else if path_str.contains("src/ui") {
55            "Slint".to_string()
56        } else {
57            let ext = path.extension().unwrap_or_default().to_string_lossy();
58            format!("File{}", capitalize(&ext))
59        };
60
61        // Licznik dla danej kategorii
62        let count = counters.entry(prefix.clone()).or_insert(1);
63
64        let id = if file_name == "mod.rs" && prefix == "RustLibMod" {
65            format!("{}_00", prefix) // mod.rs zawsze jako 00
66        } else {
67            format!("{}_{:02}", prefix, count)
68        };
69
70        map.insert(path, id);
71        if !(file_name == "mod.rs" && prefix == "RustLibMod") {
72            *count += 1;
73        }
74    }
75
76    map
77}
78
79fn capitalize(s: &str) -> String {
80    let mut c = s.chars();
81    match c.next() {
82        None => String::new(),
83        Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
84    }
85}