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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
use crate::{file_info::FileInfo, output_type::OutputType}; use std::path::PathBuf; pub fn to_string_of_length(path_buf: &PathBuf, target_length: usize) -> String { let cow = path_buf.as_os_str().to_string_lossy(); let chars = cow.chars(); let mut chars = chars.collect::<Vec<char>>(); let current_chars_count = chars.len(); let difference = (current_chars_count as isize) - target_length as isize; match difference { 0 => chars.iter().collect::<String>(), x if x < 0 => (0..(-x)).map({ |_| ' ' }).chain(chars).collect::<String>(), _ => { chars[0_usize + difference as usize] = '.'; chars[1_usize + difference as usize] = '.'; (&chars[(difference as usize)..current_chars_count]) .iter() .collect::<String>() } } } pub fn to_string_by_output_type(duplicates: Vec<Vec<FileInfo>>, output_type: OutputType) -> String { match output_type { OutputType::Plain => to_vertical_splitted_string(duplicates), OutputType::Json => to_json(duplicates), } } fn to_vertical_splitted_string(vecs: Vec<Vec<FileInfo>>) -> String { let mut result = String::new(); let vecs_length = vecs.len(); for (i, inner_vec) in vecs.into_iter().enumerate() { for file_info in inner_vec { result.push_str(file_info.path.to_str().unwrap()); result.push_str("\n"); } if i < vecs_length - 1 { result.push_str("\n"); } } result } fn to_json(vecs: Vec<Vec<FileInfo>>) -> String { serde_json::to_string_pretty(&vecs).unwrap_or("".to_string()) } #[cfg(test)] mod to_string_of_length_tests { use super::*; #[test] fn converts_long_path_buf_to_string() { let path_buf = PathBuf::from("../duplicate-finder-test-folder/transforms-2/res/drawable/design_ic_visibility_off.png"); let actual = to_string_of_length(&path_buf, 50); assert_eq!("..orms-2/res/drawable/design_ic_visibility_off.png", actual); } #[test] fn converts_short_path_buf_to_string_and_pads_to_the_right() { let path_buf = PathBuf::from("design_ic_visibility_off.png"); let actual = to_string_of_length(&path_buf, 50); assert_eq!(" design_ic_visibility_off.png", actual); } #[test] fn converts_middle_path_buf_to_string() { let path_buf = PathBuf::from("/some/long/folder/path/esign_ic_visibility_off.png"); let actual = to_string_of_length(&path_buf, 50); assert_eq!("/some/long/folder/path/esign_ic_visibility_off.png", actual); } #[test] fn converts_path_buf_with_not_ascii_chars_to_string() { let path_buf = PathBuf::from("/ssss_ssss_ssss_ssss_ssss_/_Ψ_/_æ_/_ᛤ_/_ᚙ_/_Щ_.png"); let actual = to_string_of_length(&path_buf, 24); assert_eq!("..Ψ_/_æ_/_ᛤ_/_ᚙ_/_Щ_.png", actual); } } #[cfg(test)] mod to_vertical_splitted_string_tests { use super::*; use std::sync::Arc; #[test] fn converts_specific_vector_to_vertical_splitted_string() { let vec_vec: Vec<Vec<FileInfo>> = vec![ vec![ create_file_info("/usr/some_file", 1), create_file_info("/usr/same_file", 1), ], vec![ create_file_info("/usr/another_file", 2), create_file_info("/usr/another_same_file", 2), ], vec![ create_file_info("/local/third_file", 3), create_file_info("/local/third_file_copy", 3), ], ]; let actual = to_vertical_splitted_string(vec_vec); assert_eq!("/usr/some_file\n/usr/same_file\n\n/usr/another_file\n/usr/another_same_file\n\n/local/third_file\n/local/third_file_copy\n", actual); } #[test] fn converts_empty_specific_vector_to_empty_string() { let vec_vec: Vec<Vec<FileInfo>> = vec![]; let actual = to_vertical_splitted_string(vec_vec); assert_eq!("", actual); } fn create_file_info(path: &str, hash: u128) -> FileInfo { FileInfo { path: Arc::new(PathBuf::from(path)), hash, } } } #[cfg(test)] mod to_string_by_output_type_tests { use super::*; use std::sync::Arc; #[test] fn converts_to_json_string() { let vec_vec: Vec<Vec<FileInfo>> = vec![vec![ FileInfo { path: Arc::new(PathBuf::from("/usr/some_file")), hash: 223344, }, FileInfo { path: Arc::new(PathBuf::from("/usr/other.txt")), hash: 333444, }, ]]; let actual = to_string_by_output_type(vec_vec, OutputType::Json); assert_eq!("[\n [\n {\n \"path\": \"/usr/some_file\",\n \"hash\": 223344\n },\n {\n \"path\": \"/usr/other.txt\",\n \"hash\": 333444\n }\n ]\n]", actual); } #[test] fn converts_to_plain_text_string() { let vec_vec: Vec<Vec<FileInfo>> = vec![vec![ FileInfo { path: Arc::new(PathBuf::from("/usr/some_file")), hash: 223344, }, FileInfo { path: Arc::new(PathBuf::from("/usr/other.txt")), hash: 333444, }, ]]; let actual = to_string_by_output_type(vec_vec, OutputType::Plain); assert_eq!("/usr/some_file\n/usr/other.txt\n", actual); } }