@@ -25,35 +25,23 @@ pub use crate::{
2525} ;
2626
2727#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
28- pub struct CargoTomlNoneFoundError ( pub PathBuf ) ;
29-
30- #[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
31- pub struct CargoTomlMultipleValidFoundError ( pub Vec < PathBuf > ) ;
32-
33- #[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
34- pub struct CargoTomlSearchFileSystemError ( pub PathBuf , pub String ) ;
35-
36- impl std:: fmt:: Display for CargoTomlNoneFoundError {
37- fn fmt ( & self , fmt : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
38- write ! ( fmt, "can't find Cargo.toml at {}" , self . 0 . display( ) )
39- }
40- }
41-
42- impl std:: fmt:: Display for CargoTomlMultipleValidFoundError {
43- fn fmt ( & self , fmt : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
44- write ! ( fmt, "found multiple valid Cargo.toml files {:?}" , self . 0 )
45- }
28+ pub struct CargoTomlNotFoundError {
29+ pub searched_at : PathBuf ,
30+ pub reason : String ,
4631}
4732
48- impl std:: fmt:: Display for CargoTomlSearchFileSystemError {
33+ impl std:: fmt:: Display for CargoTomlNotFoundError {
4934 fn fmt ( & self , fmt : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
50- write ! ( fmt, "a filesystem error occurred while searching for Cargo.toml: {}" , self . 1 )
35+ write ! (
36+ fmt,
37+ "can't find Cargo.toml at {}, due to {}" ,
38+ self . searched_at. display( ) ,
39+ self . reason
40+ )
5141 }
5242}
5343
54- impl Error for CargoTomlNoneFoundError { }
55- impl Error for CargoTomlMultipleValidFoundError { }
56- impl Error for CargoTomlSearchFileSystemError { }
44+ impl Error for CargoTomlNotFoundError { }
5745
5846#[ derive( Debug , Clone ) ]
5947pub enum ProjectWorkspace {
@@ -452,8 +440,6 @@ fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> {
452440}
453441
454442fn find_cargo_toml ( path : & Path ) -> Result < PathBuf > {
455- let path_as_buf = path. to_path_buf ( ) ;
456-
457443 if path. ends_with ( "Cargo.toml" ) {
458444 return Ok ( path. to_path_buf ( ) ) ;
459445 }
@@ -464,14 +450,31 @@ fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
464450
465451 let entities = match read_dir ( path) {
466452 Ok ( entities) => entities,
467- Err ( e) => return Err ( CargoTomlSearchFileSystemError ( path_as_buf, e. to_string ( ) ) . into ( ) ) ,
453+ Err ( e) => {
454+ return Err ( CargoTomlNotFoundError {
455+ searched_at : path. to_path_buf ( ) ,
456+ reason : format ! ( "file system error: {}" , e) ,
457+ }
458+ . into ( ) ) ;
459+ }
468460 } ;
469461
470462 let mut valid_canditates = find_cargo_toml_in_child_dir ( entities) ;
471463 match valid_canditates. len ( ) {
472464 1 => Ok ( valid_canditates. remove ( 0 ) ) ,
473- 0 => Err ( CargoTomlNoneFoundError ( path_as_buf) . into ( ) ) ,
474- _ => Err ( CargoTomlMultipleValidFoundError ( valid_canditates) . into ( ) ) ,
465+ 0 => Err ( CargoTomlNotFoundError {
466+ searched_at : path. to_path_buf ( ) ,
467+ reason : "no Cargo.toml file found" . to_string ( ) ,
468+ }
469+ . into ( ) ) ,
470+ _ => Err ( CargoTomlNotFoundError {
471+ searched_at : path. to_path_buf ( ) ,
472+ reason : format ! (
473+ "multiple equally valid Cargo.toml files found: {:?}" ,
474+ valid_canditates
475+ ) ,
476+ }
477+ . into ( ) ) ,
475478 }
476479}
477480
0 commit comments