about summary refs log tree commit diff stats
path: root/sys/nixpkgs/pkgs/lf-make-map/src/mapping/mod.rs
blob: d05d34175623f5510490a7c65ceb99c0c85a5a59 (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
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
use std::{
    fmt::Display,
    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: Vec<MapKey>,
}

#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Hash, Eq)]
pub struct MapKey {
    value: String,
}

impl MapKey {
    pub fn new(value: String) -> Self {
        Self { value }
    }
    pub fn display(values: &[Self]) -> String {
        values.iter().map(|value| value.value.clone()).collect()
    }
}

impl Display for MapKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.value)
    }
}

impl Display for Mapping {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.raw_path.display())
    }
}

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_keys(raw_path.to_str().expect("Should be a valid &str"));

        Self {
            raw_path: raw_path.to_owned(),
            keys: key.len(),
            key,
        }
    }

    fn path_to_keys(path: &str) -> Vec<MapKey> {
        let key: Vec<MapKey> = path.split('/').map(Self::part_path_to_key).collect();
        debug!("Will insert: '{}' -> '{}'", path, MapKey::display(&key));
        key
    }

    fn part_path_to_key(part: &str) -> MapKey {
        let value = if part.contains('_') {
            part.split('_').filter_map(|ch| ch.chars().nth(0)).collect()
        } else if part.contains('-') {
            part.split('-').filter_map(|ch| ch.chars().nth(0)).collect()
        } else {
            part.chars()
                .nth(0)
                .expect("Must have a first element")
                .to_string()
        };
        MapKey::new(value)
    }
}

// pub fn gen_hot_key(path: &Path, base_path: &Path, amount_of_chars: usize) -> String {
//     let mut return_val = String::from("g");
//     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());
    }
}