@@ -10,64 +10,79 @@ use thiserror::Error;
1010pub enum Error {
1111 #[ error( "Failed to run `adb emu avd name`: {0}" ) ]
1212 EmuFailed ( #[ source] super :: RunCheckedError ) ,
13+ #[ error( transparent) ]
14+ GetPropFailed ( super :: get_prop:: Error ) ,
1315 #[ error( "Failed to run `adb shell dumpsys bluetooth_manager`: {0}" ) ]
1416 DumpsysFailed ( #[ source] super :: RunCheckedError ) ,
1517 #[ error( "Name regex didn't match anything." ) ]
1618 NotMatched ,
17- #[ error( transparent) ]
18- Io ( #[ from] std:: io:: Error ) ,
19+ #[ error( "Failed to run {command}: {error}" ) ]
20+ CommandFailed {
21+ command : String ,
22+ error : std:: io:: Error ,
23+ } ,
1924}
2025
2126impl Reportable for Error {
2227 fn report ( & self ) -> Report {
2328 let msg = "Failed to get device name" ;
2429 match self {
2530 Self :: EmuFailed ( err) => err. report ( "Failed to run `adb emu avd name`" ) ,
31+ Self :: GetPropFailed ( err) => err. report ( ) ,
2632 Self :: DumpsysFailed ( err) => {
2733 err. report ( "Failed to run `adb shell dumpsys bluetooth_manager`" )
2834 }
2935 Self :: NotMatched => Report :: error ( msg, self ) ,
30- Self :: Io ( err) => Report :: error ( "IO error" , err) ,
36+ Self :: CommandFailed { command, error } => {
37+ Report :: error ( format ! ( "Failed to run {command}" ) , error)
38+ }
3139 }
3240 }
3341}
3442
3543pub fn device_name ( env : & Env , serial_no : & str ) -> Result < String , Error > {
3644 if serial_no. starts_with ( "emulator" ) {
45+ let cmd = adb ( env, [ "-s" , serial_no, "emu" , "avd" , "name" ] )
46+ . stderr_capture ( )
47+ . stdout_capture ( ) ;
3748 let name = super :: check_authorized (
38- adb ( env, [ "-s" , serial_no] )
39- . before_spawn ( move |cmd| {
40- cmd. args ( [ "emu" , "avd" , "name" ] ) ;
41- Ok ( ( ) )
42- } )
43- . stderr_capture ( )
44- . stdout_capture ( )
45- . start ( ) ?
46- . wait ( ) ?,
49+ cmd. start ( )
50+ . map_err ( |error| Error :: CommandFailed {
51+ command : format ! ( "{cmd:?}" ) ,
52+ error,
53+ } ) ?
54+ . wait ( )
55+ . map_err ( |error| Error :: CommandFailed {
56+ command : format ! ( "{cmd:?}" ) ,
57+ error,
58+ } ) ?,
4759 )
4860 . map ( |stdout| stdout. split ( '\n' ) . next ( ) . unwrap ( ) . trim ( ) . to_string ( ) )
4961 . map_err ( Error :: EmuFailed ) ?;
5062 if name. is_empty ( ) {
51- super :: get_prop:: get_prop ( env, serial_no, "ro.boot.qemu.avd_name" ) . map_err ( |e| {
52- Error :: EmuFailed ( super :: RunCheckedError :: CommandFailed ( std:: io:: Error :: new (
53- std:: io:: ErrorKind :: Other ,
54- e,
55- ) ) )
56- } )
63+ super :: get_prop:: get_prop ( env, serial_no, "ro.boot.qemu.avd_name" )
64+ . map_err ( Error :: GetPropFailed )
5765 } else {
5866 Ok ( name)
5967 }
6068 } else {
69+ let cmd = adb (
70+ env,
71+ [ "-s" , serial_no, "shell" , "dumpsys" , "bluetooth_manager" ] ,
72+ )
73+ . stderr_capture ( )
74+ . stdout_capture ( ) ;
6175 super :: check_authorized (
62- adb ( env, [ "-s" , serial_no] )
63- . before_spawn ( move |cmd| {
64- cmd. args ( [ "shell" , "dumpsys" , "bluetooth_manager" ] ) ;
65- Ok ( ( ) )
66- } )
67- . stderr_capture ( )
68- . stdout_capture ( )
69- . start ( ) ?
70- . wait ( ) ?,
76+ cmd. start ( )
77+ . map_err ( |error| Error :: CommandFailed {
78+ command : format ! ( "{cmd:?}" ) ,
79+ error,
80+ } ) ?
81+ . wait ( )
82+ . map_err ( |error| Error :: CommandFailed {
83+ command : format ! ( "{cmd:?}" ) ,
84+ error,
85+ } ) ?,
7186 )
7287 . map_err ( Error :: DumpsysFailed )
7388 . and_then ( |stdout| {
0 commit comments