use std::{fmt::Display, hash::Hash}; use log::debug; pub mod error; pub mod map_tree; #[derive(Clone, Debug, Eq)] pub struct MapKey { pub key: String, resolution: usize, /// Part of the path, used to derive the key part_path: String, } impl Hash for MapKey { fn hash(&self, state: &mut H) { self.key.hash(state) } } impl PartialEq for MapKey { fn eq(&self, other: &Self) -> bool { self.key == other.key } } impl MapKey { pub fn new_from_part_path(part_path: &str, resolution: usize) -> Self { let key = Self::part_path_to_key(&part_path, resolution); Self { key, resolution, part_path: part_path.to_owned(), } } pub fn increment(&mut self) { if self.resolution < self.part_path.len() { self.resolution += 1; self.key = Self::part_path_to_key(&self.part_path, self.resolution); } else { let last_char = self.key.chars().last().expect("A last value exists"); self.key.push(last_char); } } pub fn new_ones_from_path(path: &str, number_of_chars: usize) -> Vec { let key: Vec = path .split('/') .map(|part| Self::new_from_part_path(part, number_of_chars)) .collect(); debug!( "Generated full MapKeys: '{}' -> '{}'", path, MapKey::display(&key) ); key } pub fn display(values: &[Self]) -> String { values.iter().map(|value| value.key.clone()).collect() } fn part_path_to_key(part: &str, number_of_chars: usize) -> String { let value = if part.contains('_') { part.split('_') .map(|ch| ch.chars().take(number_of_chars).collect::>()) .flatten() .collect() } else if part.contains('-') { part.split('-') .map(|ch| ch.chars().take(number_of_chars).collect::>()) .flatten() .collect() } else { part.chars().take(number_of_chars).collect::() }; value } } impl Display for MapKey { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(&self.key) } } // 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()); } }