1- use anyhow:: Result ;
1+ use anyhow:: { bail , Result } ;
22use std:: path:: PathBuf ;
33use std:: process:: Command ;
44
5- #[ derive( Clone , Debug ) ]
5+ use crate :: devices:: adb:: Adb ;
6+
7+ #[ derive( Debug ) ]
68pub struct Doctor {
79 groups : Vec < Group > ,
810}
@@ -35,7 +37,11 @@ impl Default for Doctor {
3537 Group {
3638 name: "android" ,
3739 checks: vec![
38- Check :: new( "adb" , Some ( VersionCheck :: new( "--version" , 0 , 4 ) ) ) ,
40+ Check :: with_path(
41+ "adb" ,
42+ Adb :: which( ) ,
43+ Some ( VersionCheck :: new( "--version" , 0 , 4 ) ) ,
44+ ) ,
3945 Check :: new( "javac" , Some ( VersionCheck :: new( "--version" , 0 , 1 ) ) ) ,
4046 Check :: new( "java" , Some ( VersionCheck :: new( "--version" , 0 , 1 ) ) ) ,
4147 Check :: new( "kotlin" , Some ( VersionCheck :: new( "-version" , 0 , 2 ) ) ) ,
@@ -77,7 +83,7 @@ impl std::fmt::Display for Doctor {
7783 }
7884}
7985
80- #[ derive( Clone , Debug ) ]
86+ #[ derive( Debug ) ]
8187struct Group {
8288 name : & ' static str ,
8389 checks : Vec < Check > ,
@@ -105,15 +111,32 @@ impl std::fmt::Display for Group {
105111 }
106112}
107113
108- #[ derive( Clone , Copy , Debug ) ]
114+ #[ derive( Debug ) ]
109115struct Check {
110116 name : & ' static str ,
117+ location : Option < Result < PathBuf > > ,
111118 version : Option < VersionCheck > ,
112119}
113120
114121impl Check {
115122 pub const fn new ( name : & ' static str , version : Option < VersionCheck > ) -> Self {
116- Self { name, version }
123+ Self {
124+ name,
125+ location : None ,
126+ version,
127+ }
128+ }
129+
130+ pub const fn with_path (
131+ name : & ' static str ,
132+ path : Result < PathBuf > ,
133+ version : Option < VersionCheck > ,
134+ ) -> Self {
135+ Self {
136+ name,
137+ location : Some ( path) ,
138+ version,
139+ }
117140 }
118141}
119142
@@ -131,17 +154,22 @@ impl VersionCheck {
131154}
132155
133156impl Check {
134- fn name ( self ) -> & ' static str {
157+ fn name ( & self ) -> & ' static str {
135158 self . name
136159 }
137160
138- fn path ( self ) -> Result < PathBuf > {
139- Ok ( which:: which ( self . name ) ?)
161+ fn path ( & self ) -> Result < PathBuf > {
162+ Ok ( match & self . location {
163+ Some ( Ok ( path) ) => path. clone ( ) ,
164+ // Cannot clone the error:
165+ Some ( Err ( e) ) => bail ! ( "{:?}" , e) ,
166+ None => which:: which ( self . name ) ?,
167+ } )
140168 }
141169
142- fn version ( self ) -> Result < Option < String > > {
170+ fn version ( & self ) -> Result < Option < String > > {
143171 if let Some ( version) = self . version {
144- let output = Command :: new ( self . name )
172+ let output = Command :: new ( self . path ( ) ? )
145173 . args ( version. arg . split ( ' ' ) )
146174 . output ( ) ?;
147175 anyhow:: ensure!( output. status. success( ) , "failed to run {}" , self . name) ;
0 commit comments