@@ -7,6 +7,7 @@ mod sysroot;
77use std:: {
88 error:: Error ,
99 fs:: File ,
10+ fs:: read_dir,
1011 io:: BufReader ,
1112 path:: { Path , PathBuf } ,
1213 process:: Command ,
@@ -406,18 +407,54 @@ fn find_rust_project_json(path: &Path) -> Option<PathBuf> {
406407 None
407408}
408409
409- fn find_cargo_toml ( path : & Path ) -> Result < PathBuf > {
410- if path. ends_with ( "Cargo.toml" ) {
411- return Ok ( path. to_path_buf ( ) ) ;
412- }
410+ fn find_cargo_toml_down_the_fs ( path : & Path ) -> Option < PathBuf > {
413411 let mut curr = Some ( path) ;
414412 while let Some ( path) = curr {
415413 let candidate = path. join ( "Cargo.toml" ) ;
416414 if candidate. exists ( ) {
417- return Ok ( candidate) ;
415+ return Some ( candidate) ;
418416 }
419417 curr = path. parent ( ) ;
420418 }
419+
420+ None
421+ }
422+
423+ fn find_cargo_toml_up_the_fs ( path : & Path ) -> Option < PathBuf > {
424+ log:: info!( "find_cargo_toml_up_the_fs()" ) ;
425+ let entities = match read_dir ( path) {
426+ Ok ( entities) => entities,
427+ Err ( e) => {
428+ log:: info!( "err {}" , e) ;
429+ return None
430+ }
431+ } ;
432+
433+ log:: info!( "entities: {:?}" , entities) ;
434+ for entity in entities. filter_map ( Result :: ok) {
435+ let candidate = entity. path ( ) . join ( "Cargo.toml" ) ;
436+ log:: info!( "candidate: {:?}, exists: {}" , candidate, candidate. exists( ) ) ;
437+ if candidate. exists ( ) {
438+ return Some ( candidate) ;
439+ }
440+ }
441+
442+ None
443+ }
444+
445+ fn find_cargo_toml ( path : & Path ) -> Result < PathBuf > {
446+ if path. ends_with ( "Cargo.toml" ) {
447+ return Ok ( path. to_path_buf ( ) ) ;
448+ }
449+
450+ if let Some ( p) = find_cargo_toml_down_the_fs ( path) {
451+ return Ok ( p)
452+ }
453+
454+ if let Some ( p) = find_cargo_toml_up_the_fs ( path) {
455+ return Ok ( p)
456+ }
457+
421458 Err ( CargoTomlNotFoundError ( path. to_path_buf ( ) ) . into ( ) )
422459}
423460
0 commit comments