@@ -88,46 +88,28 @@ impl ProjectRoot {
8888 }
8989
9090 pub fn discover ( path : & Path ) -> io:: Result < Vec < ProjectRoot > > {
91- if let Some ( project_json) = find_rust_project_json ( path) {
91+ if let Some ( project_json) = find_in_parent_dirs ( path, "rust-project.json" ) {
9292 return Ok ( vec ! [ ProjectRoot :: ProjectJson ( project_json) ] ) ;
9393 }
9494 return find_cargo_toml ( path)
9595 . map ( |paths| paths. into_iter ( ) . map ( ProjectRoot :: CargoToml ) . collect ( ) ) ;
9696
97- fn find_rust_project_json ( path : & Path ) -> Option < PathBuf > {
98- if path. ends_with ( "rust-project.json" ) {
99- return Some ( path. to_path_buf ( ) ) ;
100- }
101-
102- let mut curr = Some ( path) ;
103- while let Some ( path) = curr {
104- let candidate = path. join ( "rust-project.json" ) ;
105- if candidate. exists ( ) {
106- return Some ( candidate) ;
107- }
108- curr = path. parent ( ) ;
109- }
110-
111- None
112- }
113-
11497 fn find_cargo_toml ( path : & Path ) -> io:: Result < Vec < PathBuf > > {
115- if path. ends_with ( "Cargo.toml" ) {
116- return Ok ( vec ! [ path. to_path_buf( ) ] ) ;
98+ match find_in_parent_dirs ( path, "Cargo.toml" ) {
99+ Some ( it) => Ok ( vec ! [ it] ) ,
100+ None => Ok ( find_cargo_toml_in_child_dir ( read_dir ( path) ?) ) ,
117101 }
102+ }
118103
119- if let Some ( p) = find_cargo_toml_in_parent_dir ( path) {
120- return Ok ( vec ! [ p] ) ;
104+ fn find_in_parent_dirs ( path : & Path , target_file_name : & str ) -> Option < PathBuf > {
105+ if path. ends_with ( target_file_name) {
106+ return Some ( path. to_owned ( ) ) ;
121107 }
122108
123- let entities = read_dir ( path) ?;
124- Ok ( find_cargo_toml_in_child_dir ( entities) )
125- }
126-
127- fn find_cargo_toml_in_parent_dir ( path : & Path ) -> Option < PathBuf > {
128109 let mut curr = Some ( path) ;
110+
129111 while let Some ( path) = curr {
130- let candidate = path. join ( "Cargo.toml" ) ;
112+ let candidate = path. join ( target_file_name ) ;
131113 if candidate. exists ( ) {
132114 return Some ( candidate) ;
133115 }
@@ -139,14 +121,11 @@ impl ProjectRoot {
139121
140122 fn find_cargo_toml_in_child_dir ( entities : ReadDir ) -> Vec < PathBuf > {
141123 // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects
142- let mut valid_canditates = vec ! [ ] ;
143- for entity in entities. filter_map ( Result :: ok) {
144- let candidate = entity. path ( ) . join ( "Cargo.toml" ) ;
145- if candidate. exists ( ) {
146- valid_canditates. push ( candidate)
147- }
148- }
149- valid_canditates
124+ entities
125+ . filter_map ( Result :: ok)
126+ . map ( |it| it. path ( ) . join ( "Cargo.toml" ) )
127+ . filter ( |it| it. exists ( ) )
128+ . collect ( )
150129 }
151130 }
152131}
0 commit comments