@@ -22,46 +22,57 @@ impl Device {
2222 pub fn platform ( & self ) -> & String {
2323 & self . platform
2424 }
25- }
2625
27- impl Display for Device {
28- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
29- write ! ( f, "[{}] {} ({})" , self . platform, self . name, self . id)
26+ fn from_str_with_platforms ( line : & str , platforms : & [ Platform ] ) -> Option < Device > {
27+ let ( id, platform) = Self :: parse_info ( line, platforms) ?;
28+ let name = Self :: parse_name ( & id) . ok ( ) ?;
29+ let device = Self { name, id, platform } ;
30+ Some ( device)
3031 }
31- }
3232
33- pub fn get_devices ( platforms : & [ Platform ] ) -> Result < Vec < Device > , Error > {
34- let output = Command :: new ( "adb" ) . args ( [ "devices" , "-l" ] ) . output ( ) ?;
35- let output = String :: from_utf8 ( output. stdout ) ?;
36- let header_line_ix = output
37- . lines ( )
38- . position ( |l| l. contains ( "List of devices attached" ) )
39- . ok_or ( Error :: DevicesFetching ) ?;
40-
41- let devices = output
42- . lines ( )
43- . skip ( header_line_ix + 1 )
44- . filter ( |l| !l. is_empty ( ) )
45- . filter_map ( |l| parse_device ( l, platforms) )
46- . collect ( ) ;
47- Ok ( devices)
48- }
33+ fn parse_name ( id : & str ) -> Result < String , Error > {
34+ let output = Command :: new ( "adb" )
35+ . args ( [ "-s" , id, "shell" , "settings get global device_name" ] )
36+ . output ( ) ?;
37+ let name = String :: from_utf8 ( output. stdout ) ?;
38+ match name. len ( ) {
39+ 0 => Err ( Error :: NoDeviceName ) ,
40+ _ => Ok ( String :: from ( name. trim_end ( ) ) ) ,
41+ }
42+ }
43+
44+ fn parse_info ( line : & str , platforms : & [ Platform ] ) -> Option < ( String , Platform ) > {
45+ let regex = Regex :: new ( r"(\w+)\s+.*model:(\w+)\sdevice:(\w+)" ) . ok ( ) ?;
46+ let caps = regex. captures ( line) ?;
47+ let id = String :: from ( caps. get ( 1 ) ?. as_str ( ) ) ;
48+ let model = caps. get ( 2 ) ?. as_str ( ) ;
49+ let device_name = caps. get ( 3 ) ?. as_str ( ) ;
50+ let platform = get_platform ( model, platforms) . or ( get_platform ( device_name, platforms) ) ?;
51+ Some ( ( id, platform) )
52+ }
4953
50- fn parse_device ( line : & str , platforms : & [ Platform ] ) -> Option < Device > {
51- let ( id, platform) = parse_device_info ( line, platforms) ?;
52- let name = get_device_name ( & id) . ok ( ) ?;
53- let device = Device { name, id, platform } ;
54- Some ( device)
54+ pub fn get_devices ( platforms : & [ Platform ] ) -> Result < Vec < Device > , Error > {
55+ let output = Command :: new ( "adb" ) . args ( [ "devices" , "-l" ] ) . output ( ) ?;
56+ let output = String :: from_utf8 ( output. stdout ) ?;
57+ let header_line_ix = output
58+ . lines ( )
59+ . position ( |l| l. contains ( "List of devices attached" ) )
60+ . ok_or ( Error :: DevicesFetching ) ?;
61+
62+ let devices = output
63+ . lines ( )
64+ . skip ( header_line_ix + 1 )
65+ . filter ( |l| !l. is_empty ( ) )
66+ . filter_map ( |l| Self :: from_str_with_platforms ( l, platforms) )
67+ . collect ( ) ;
68+ Ok ( devices)
69+ }
5570}
5671
57- fn parse_device_info ( line : & str , platforms : & [ Platform ] ) -> Option < ( String , Platform ) > {
58- let regex = Regex :: new ( r"(\w+)\s+.*model:(\w+)\sdevice:(\w+)" ) . ok ( ) ?;
59- let caps = regex. captures ( line) ?;
60- let id = String :: from ( caps. get ( 1 ) ?. as_str ( ) ) ;
61- let model = caps. get ( 2 ) ?. as_str ( ) ;
62- let device_name = caps. get ( 3 ) ?. as_str ( ) ;
63- let platform = get_platform ( model, platforms) . or ( get_platform ( device_name, platforms) ) ?;
64- Some ( ( id, platform) )
72+ impl Display for Device {
73+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
74+ write ! ( f, "[{}] {} ({})" , self . platform, self . name, self . id)
75+ }
6576}
6677
6778fn get_platform ( identifier : & str , platforms : & [ Platform ] ) -> Option < Platform > {
@@ -70,17 +81,6 @@ fn get_platform(identifier: &str, platforms: &[Platform]) -> Option<Platform> {
7081 Some ( platform. clone ( ) )
7182}
7283
73- fn get_device_name ( id : & str ) -> Result < String , Error > {
74- let output = Command :: new ( "adb" )
75- . args ( [ "-s" , id, "shell" , "settings get global device_name" ] )
76- . output ( ) ?;
77- let name = String :: from_utf8 ( output. stdout ) ?;
78- match name. len ( ) {
79- 0 => Err ( Error :: NoDeviceName ) ,
80- _ => Ok ( String :: from ( name. trim_end ( ) ) ) ,
81- }
82- }
83-
8484#[ cfg( test) ]
8585mod tests {
8686 use super :: * ;
@@ -122,7 +122,7 @@ mod tests {
122122 let data = "PA7L50MGF8290021W device usb:34873344X product:A7H10 model:Pico_Neo_3 \
123123 device:PICOA7H10 transport_id:2";
124124 let platforms = get_platforms ( ) ;
125- let info = parse_device_info ( data, & platforms) ;
125+ let info = Device :: parse_info ( data, & platforms) ;
126126 assert ! ( info. is_some( ) ) ;
127127 let ( id, platform) = info. unwrap ( ) ;
128128 assert_eq ! ( id, String :: from( "PA7L50MGF8290021W" ) ) ;
@@ -134,7 +134,7 @@ mod tests {
134134 let data = "PA8150MGGB230744G device usb:34603008X product:Phoenix_ovs model:A8110 \
135135 device:PICOA8110 transport_id:7";
136136 let platforms = get_platforms ( ) ;
137- let info = parse_device_info ( data, & platforms) ;
137+ let info = Device :: parse_info ( data, & platforms) ;
138138 assert ! ( info. is_some( ) ) ;
139139 let ( id, platform) = info. unwrap ( ) ;
140140 assert_eq ! ( id, String :: from( "PA8150MGGB230744G" ) ) ;
@@ -146,7 +146,7 @@ mod tests {
146146 let data = "1WMHHA63PR1501 device usb:34603008X product:hollywood model:Quest_2 \
147147 device:hollywood transport_id:9";
148148 let platforms = get_platforms ( ) ;
149- let info = parse_device_info ( data, & platforms) ;
149+ let info = Device :: parse_info ( data, & platforms) ;
150150 assert ! ( info. is_some( ) ) ;
151151 let ( id, platform) = info. unwrap ( ) ;
152152 assert_eq ! ( id, String :: from( "1WMHHA63PR1501" ) ) ;
@@ -158,7 +158,7 @@ mod tests {
158158 let data = "ce031713396bc92803 device usb:1048576X product:dreamltexx model:SM_G950F \
159159 device:dreamlte transport_id:4";
160160 let platforms = get_platforms ( ) ;
161- let info = parse_device_info ( data, & platforms) ;
161+ let info = Device :: parse_info ( data, & platforms) ;
162162 assert ! ( info. is_some( ) ) ;
163163 let ( id, platform) = info. unwrap ( ) ;
164164 assert_eq ! ( id, String :: from( "ce031713396bc92803" ) ) ;
0 commit comments