@@ -2,8 +2,35 @@ use std::ffi::OsStr;
22use std:: fs;
33use std:: path:: Path ;
44use std:: path:: PathBuf ;
5+ use std:: cmp:: Ordering ;
56
6- pub fn list_all_file_items ( path : & Path ) -> Vec < String > {
7+ pub struct FileItem {
8+ pub filename : String ,
9+ pub pos : usize ,
10+ pub is_dir : bool ,
11+ }
12+
13+ impl Ord for FileItem {
14+ fn cmp ( & self , other : & Self ) -> Ordering {
15+ self . filename . cmp ( & other. filename )
16+ }
17+ }
18+
19+ impl PartialEq for FileItem {
20+ fn eq ( & self , other : & Self ) -> bool {
21+ self . filename == other. filename && self . pos == other. pos
22+ }
23+ }
24+
25+ impl Eq for FileItem { }
26+
27+ impl PartialOrd for FileItem {
28+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
29+ Some ( self . cmp ( other) )
30+ }
31+ }
32+
33+ pub fn list_all_file_items ( path : & Path , pos : usize ) -> Vec < FileItem > {
734 let read_result = fs:: read_dir ( path) ;
835 let mut result = Vec :: new ( ) ;
936 if let Ok ( entries) = read_result {
@@ -12,7 +39,11 @@ pub fn list_all_file_items(path: &Path) -> Vec<String> {
1239 let current = entry. path ( ) ;
1340 let filename = current. file_name ( ) . unwrap_or ( OsStr :: new ( "" ) ) . to_str ( ) ;
1441 if let Some ( f) = filename {
15- result. push ( f. to_string ( ) ) ;
42+ result. push ( FileItem {
43+ filename : f. to_string ( ) ,
44+ pos,
45+ is_dir : current. is_dir ( ) ,
46+ } ) ;
1647 }
1748 }
1849 }
@@ -22,7 +53,7 @@ pub fn list_all_file_items(path: &Path) -> Vec<String> {
2253
2354const MAX_LINE_LENGTH : usize = 600 ;
2455
25- pub fn get_file_items ( current_line : & str , root_folder : & str ) -> Vec < ( String , usize ) > {
56+ pub fn get_file_items ( current_line : & str , root_folder : & str ) -> Vec < FileItem > {
2657 if current_line. len ( ) > MAX_LINE_LENGTH {
2758 return Vec :: new ( ) ;
2859 }
@@ -41,11 +72,7 @@ pub fn get_file_items(current_line: &str, root_folder: &str) -> Vec<(String, usi
4172
4273 for base in [ root_folder, "" ] . iter ( ) . map ( PathBuf :: from) {
4374 let path = base. join ( p) ;
44- file_items. extend (
45- list_all_file_items ( & path)
46- . into_iter ( )
47- . map ( |file_path| ( file_path, j) ) ,
48- ) ;
75+ file_items. extend ( list_all_file_items ( & path, j) ) ;
4976 }
5077 }
5178 }
@@ -61,21 +88,21 @@ mod tests {
6188
6289 #[ test]
6390 fn test_list_all_files ( ) {
64- let items = list_all_file_items ( Path :: new ( "./" ) ) ;
91+ let items = list_all_file_items ( Path :: new ( "./" ) , 0 ) ;
6592 for item in items. iter ( ) {
66- println ! ( "item = {}" , item) ;
93+ println ! ( "item = {}" , item. filename ) ;
6794 }
68- assert ! ( items. iter( ) . any( |s| s == "src" ) ) ;
69- assert ! ( items. iter( ) . any( |s| s == ".git" ) ) ;
70- assert ! ( items. iter( ) . any( |s| s == "README.md" ) ) ;
71- assert ! ( items. iter( ) . any( |s| s == ".gitignore" ) ) ;
72- assert ! ( items. iter( ) . any( |s| s == "Cargo.toml" ) ) ;
73- assert ! ( items. iter( ) . any( |s| s == "Cargo.lock" ) ) ;
95+ assert ! ( items. iter( ) . any( |s| s. filename == "src" ) ) ;
96+ assert ! ( items. iter( ) . any( |s| s. filename == ".git" ) ) ;
97+ assert ! ( items. iter( ) . any( |s| s. filename == "README.md" ) ) ;
98+ assert ! ( items. iter( ) . any( |s| s. filename == ".gitignore" ) ) ;
99+ assert ! ( items. iter( ) . any( |s| s. filename == "Cargo.toml" ) ) ;
100+ assert ! ( items. iter( ) . any( |s| s. filename == "Cargo.lock" ) ) ;
74101
75- let items = list_all_file_items ( Path :: new ( "./src" ) ) ;
76- assert ! ( items. iter( ) . any( |s| s == "main.rs" ) ) ;
102+ let items = list_all_file_items ( Path :: new ( "./src" ) , 0 ) ;
103+ assert ! ( items. iter( ) . any( |s| s. filename == "main.rs" ) ) ;
77104
78- let items = list_all_file_items ( Path :: new ( "doesnt_exist" ) ) ;
105+ let items = list_all_file_items ( Path :: new ( "doesnt_exist" ) , 0 ) ;
79106 assert_eq ! ( 0 , items. len( ) ) ;
80107 }
81108
@@ -90,7 +117,11 @@ mod tests {
90117 let root_folder = "./" ;
91118 let items = get_file_items ( line, root_folder) ;
92119
93- assert ! ( items. contains( & ( "file2.txt" . to_string( ) , 15 ) ) ) ;
120+ assert ! ( items. contains( & FileItem {
121+ filename: "file2.txt" . to_string( ) ,
122+ pos: 15 ,
123+ is_dir: false ,
124+ } ) ) ;
94125
95126 // Clean up the dummy directory structure
96127 fs:: remove_dir_all ( "./test_dir" ) . unwrap ( ) ;
0 commit comments