33mod cargo_workspace;
44mod json_project;
55mod sysroot;
6- mod workspace_error;
76
87use std:: {
8+ error:: Error ,
99 fs:: File ,
1010 io:: BufReader ,
1111 path:: { Path , PathBuf } ,
@@ -21,9 +21,27 @@ pub use crate::{
2121 cargo_workspace:: { CargoFeatures , CargoWorkspace , Package , Target , TargetKind } ,
2222 json_project:: JsonProject ,
2323 sysroot:: Sysroot ,
24- workspace_error:: WorkspaceError ,
2524} ;
2625
26+ pub type Result < T > = :: std:: result:: Result < T , Box < dyn Error + Send + Sync > > ;
27+
28+ #[ derive( Clone , PartialEq , Eq , Hash ) ]
29+ pub struct CargoTomlNotFoundError ( pub PathBuf ) ;
30+
31+ impl std:: fmt:: Display for CargoTomlNotFoundError {
32+ fn fmt ( & self , fmt : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
33+ write ! ( fmt, "can't find Cargo.toml at {}" , self . 0 . display( ) )
34+ }
35+ }
36+
37+ impl std:: fmt:: Debug for CargoTomlNotFoundError {
38+ fn fmt ( & self , fmt : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
39+ write ! ( fmt, "can't find Cargo.toml at {}" , self . 0 . display( ) )
40+ }
41+ }
42+
43+ impl Error for CargoTomlNotFoundError { }
44+
2745#[ derive( Debug , Clone ) ]
2846pub enum ProjectWorkspace {
2947 /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`.
@@ -58,27 +76,20 @@ impl PackageRoot {
5876}
5977
6078impl ProjectWorkspace {
61- pub fn discover (
62- path : & Path ,
63- cargo_features : & CargoFeatures ,
64- ) -> Result < ProjectWorkspace , WorkspaceError > {
79+ pub fn discover ( path : & Path , cargo_features : & CargoFeatures ) -> Result < ProjectWorkspace > {
6580 ProjectWorkspace :: discover_with_sysroot ( path, true , cargo_features)
6681 }
6782
6883 pub fn discover_with_sysroot (
6984 path : & Path ,
7085 with_sysroot : bool ,
7186 cargo_features : & CargoFeatures ,
72- ) -> Result < ProjectWorkspace , WorkspaceError > {
87+ ) -> Result < ProjectWorkspace > {
7388 match find_rust_project_json ( path) {
7489 Some ( json_path) => {
75- let file =
76- File :: open ( json_path) . map_err ( |err| WorkspaceError :: OpenWorkspaceError ( err) ) ?;
90+ let file = File :: open ( json_path) ?;
7791 let reader = BufReader :: new ( file) ;
78- Ok ( ProjectWorkspace :: Json {
79- project : from_reader ( reader)
80- . map_err ( |err| WorkspaceError :: ReadWorkspaceError ( err) ) ?,
81- } )
92+ Ok ( ProjectWorkspace :: Json { project : from_reader ( reader) ? } )
8293 }
8394 None => {
8495 let cargo_toml = find_cargo_toml ( path) ?;
@@ -355,7 +366,7 @@ fn find_rust_project_json(path: &Path) -> Option<PathBuf> {
355366 None
356367}
357368
358- fn find_cargo_toml ( path : & Path ) -> Result < PathBuf , WorkspaceError > {
369+ fn find_cargo_toml ( path : & Path ) -> Result < PathBuf > {
359370 if path. ends_with ( "Cargo.toml" ) {
360371 return Ok ( path. to_path_buf ( ) ) ;
361372 }
@@ -367,7 +378,7 @@ fn find_cargo_toml(path: &Path) -> Result<PathBuf, WorkspaceError> {
367378 }
368379 curr = path. parent ( ) ;
369380 }
370- Err ( WorkspaceError :: CargoTomlNotFound ( path. to_path_buf ( ) ) )
381+ Err ( Box :: new ( CargoTomlNotFoundError ( path. to_path_buf ( ) ) ) )
371382}
372383
373384pub fn get_rustc_cfg_options ( ) -> CfgOptions {
@@ -381,16 +392,13 @@ pub fn get_rustc_cfg_options() -> CfgOptions {
381392 }
382393 }
383394
384- match ( || -> Result < _ , WorkspaceError > {
395+ match ( || -> Result < _ > {
385396 // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here.
386- let output = Command :: new ( "rustc" )
387- . args ( & [ "--print" , "cfg" , "-O" ] )
388- . output ( )
389- . map_err ( |err| WorkspaceError :: RustcError ( err) ) ?;
397+ let output = Command :: new ( "rustc" ) . args ( & [ "--print" , "cfg" , "-O" ] ) . output ( ) ?;
390398 if !output. status . success ( ) {
391- Err ( WorkspaceError :: RustcCfgError ) ?;
399+ Err ( "failed to get rustc cfgs" ) ?;
392400 }
393- Ok ( String :: from_utf8 ( output. stdout ) . map_err ( |err| WorkspaceError :: RustcOutputError ( err ) ) ?)
401+ Ok ( String :: from_utf8 ( output. stdout ) ?)
394402 } ) ( ) {
395403 Ok ( rustc_cfgs) => {
396404 for line in rustc_cfgs. lines ( ) {
0 commit comments