1
+ use std:: cmp:: Ordering ;
1
2
#[ cfg( not( target_os = "windows" ) ) ]
2
3
use std:: fs;
3
4
@@ -176,28 +177,44 @@ const KNOWN_DEVICES: &[UsbDevice] = &[
176
177
] ;
177
178
178
179
fn select_serial_port (
179
- ports : Vec < SerialPortInfo > ,
180
+ mut ports : Vec < SerialPortInfo > ,
180
181
config : & Config ,
181
182
) -> Result < ( SerialPortInfo , bool ) , Error > {
182
- let device_matches = | info| {
183
+ fn device_matches ( config : & Config , info : & UsbPortInfo ) -> bool {
183
184
config
184
185
. usb_device
185
186
. iter ( )
186
187
. chain ( KNOWN_DEVICES . iter ( ) )
187
188
. any ( |dev| dev. matches ( info) )
188
- } ;
189
+ }
189
190
190
191
if ports. len ( ) > 1 {
191
192
// Multiple serial ports detected
192
193
info ! ( "Detected {} serial ports" , ports. len( ) ) ;
193
194
info ! ( "Ports which match a known common dev board are highlighted" ) ;
194
195
info ! ( "Please select a port" ) ;
195
196
197
+ ports. sort_by ( |a, b| {
198
+ let a_matches = match & a. port_type {
199
+ SerialPortType :: UsbPort ( info) => device_matches ( config, info) ,
200
+ _ => false ,
201
+ } ;
202
+ let b_matches = match & b. port_type {
203
+ SerialPortType :: UsbPort ( info) => device_matches ( config, info) ,
204
+ _ => false ,
205
+ } ;
206
+ match ( a_matches, b_matches) {
207
+ ( true , true ) | ( false , false ) => Ordering :: Equal ,
208
+ ( true , false ) => Ordering :: Less ,
209
+ ( false , true ) => Ordering :: Greater ,
210
+ }
211
+ } ) ;
212
+
196
213
let port_names = ports
197
214
. iter ( )
198
215
. map ( |port_info| match & port_info. port_type {
199
216
SerialPortType :: UsbPort ( info) => {
200
- let formatted = if device_matches ( info) {
217
+ let formatted = if device_matches ( config , info) {
201
218
port_info. port_name . as_str ( ) . bold ( )
202
219
} else {
203
220
port_info. port_name . as_str ( ) . reset ( )
@@ -222,7 +239,7 @@ fn select_serial_port(
222
239
match ports. get ( index) {
223
240
Some ( port_info) => {
224
241
let matches = if let SerialPortType :: UsbPort ( usb_info) = & port_info. port_type {
225
- device_matches ( usb_info)
242
+ device_matches ( config , usb_info)
226
243
} else {
227
244
false
228
245
} ;
@@ -248,7 +265,7 @@ fn select_serial_port(
248
265
_ => unreachable ! ( ) ,
249
266
} ;
250
267
251
- if device_matches ( port_info) {
268
+ if device_matches ( config , port_info) {
252
269
Ok ( ( port. to_owned ( ) , true ) )
253
270
} else if confirm_port ( & port_name, port_info) ? {
254
271
Ok ( ( port. to_owned ( ) , false ) )
0 commit comments