@@ -6,7 +6,6 @@ use std::{
6
6
} ;
7
7
8
8
use hyper:: Uri ;
9
- use tracing:: { error, warn} ;
10
9
11
10
/// Environment variable used to tell the layer that it should dynamically detect the local
12
11
/// port used by the given debugger. Value passed through this variable should parse into
@@ -26,7 +25,7 @@ pub const MIRRORD_DETECT_DEBUGGER_PORT_ENV: &str = "MIRRORD_DETECT_DEBUGGER_PORT
26
25
pub const MIRRORD_IGNORE_DEBUGGER_PORTS_ENV : & str = "MIRRORD_IGNORE_DEBUGGER_PORTS" ;
27
26
28
27
/// The default port used by node's --inspect debugger from the
29
- /// [node doceumentation ](https://nodejs.org/en/learn/getting-started/debugging#enable-inspector)
28
+ /// [node documentation ](https://nodejs.org/en/learn/getting-started/debugging#enable-inspector)
30
29
pub const NODE_INSPECTOR_DEFAULT_PORT : u16 = 9229 ;
31
30
32
31
/// Type of debugger which is used to run the user's processes.
@@ -269,9 +268,8 @@ impl DebuggerType {
269
268
} . iter ( ) . filter_map ( |addr| match addr. ip ( ) {
270
269
IpAddr :: V4 ( Ipv4Addr :: LOCALHOST ) | IpAddr :: V6 ( Ipv6Addr :: LOCALHOST ) => Some ( addr. port ( ) ) ,
271
270
other => {
272
- warn ! (
273
- "Debugger uses a remote socket address {:?}! This case is not yet handled properly." ,
274
- other,
271
+ tracing:: debug!(
272
+ "Debugger uses a remote socket address {other}! This case is not yet handled properly." ,
275
273
) ;
276
274
None
277
275
}
@@ -300,58 +298,64 @@ pub enum DebuggerPorts {
300
298
impl FromStr for DebuggerPorts {
301
299
type Err = std:: convert:: Infallible ;
302
300
301
+ /// Parses [`DebuggerPorts`] from a string.
302
+ /// The string should look like one of:
303
+ /// 1. `<port>`
304
+ /// 2. `<port1>-<port2>`
305
+ /// 3. Comma-separated sequence of previous two variants
303
306
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
304
- // string looks like 'port1,port2,port3-portx,porty-portz'
305
- let mut vec = vec ! [ ] ;
306
- s. split ( ',' )
307
- . for_each ( |s| {
308
- let chunks = s
307
+ let vec = s
308
+ . split ( ',' )
309
+ . filter_map ( |entry| {
310
+ let chunks = entry
309
311
. split ( '-' )
310
312
. map ( u16:: from_str)
311
313
. collect :: < Result < Vec < _ > , _ > > ( )
312
- . inspect_err ( |e| error ! (
313
- "Failed to decode debugger port range from {} env variable: {}" ,
314
- MIRRORD_IGNORE_DEBUGGER_PORTS_ENV ,
315
- e
316
- ) )
317
- . ok ( ) . unwrap_or_default ( ) ;
318
- match * chunks. as_slice ( ) {
319
- [ p] => vec. push ( Self :: Single ( p) ) ,
320
- [ p1, p2] if p1 <= p2 => vec. push ( Self :: FixedRange ( p1..=p2) ) ,
314
+ . ok ( ) ;
315
+
316
+ match chunks. as_deref ( ) {
317
+ Some ( & [ p] ) => Some ( Self :: Single ( p) ) ,
318
+ Some ( & [ p1, p2] ) if p1 <= p2 => Some ( Self :: FixedRange ( p1..=p2) ) ,
321
319
_ => {
322
- error ! (
323
- "Failed to decode debugger ports from {} env variable: expected a port or range of ports" ,
320
+ tracing:: debug!(
321
+ full_variable = s,
322
+ entry,
323
+ "Failed to decode debugger ports entry from {} env variable" ,
324
324
MIRRORD_IGNORE_DEBUGGER_PORTS_ENV ,
325
325
) ;
326
- } ,
327
- } ;
328
- } ) ;
329
- if !vec. is_empty ( ) {
330
- Ok ( Self :: Combination ( vec) )
331
- } else {
332
- Ok ( Self :: None )
333
- }
326
+ None
327
+ }
328
+ }
329
+ } )
330
+ . collect :: < Vec < _ > > ( ) ;
331
+
332
+ let result = match vec. len ( ) {
333
+ 0 => Self :: None ,
334
+ 1 => vec. into_iter ( ) . next ( ) . unwrap ( ) ,
335
+ _ => Self :: Combination ( vec) ,
336
+ } ;
337
+
338
+ Ok ( result)
334
339
}
335
340
}
336
341
337
342
impl fmt:: Display for DebuggerPorts {
343
+ /// Writes [`DebuggerPorts`] into the given [`fmt::Formatter`],
344
+ /// using format recognized by [`FromStr`] implementation.
338
345
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
339
- write ! (
340
- f,
341
- "{}" ,
342
- match self {
343
- DebuggerPorts :: Single ( port) => port. to_string( ) ,
344
- DebuggerPorts :: FixedRange ( range_inclusive) =>
345
- format!( "{}-{}" , range_inclusive. start( ) , range_inclusive. end( ) ) ,
346
- DebuggerPorts :: Combination ( vec) => {
347
- vec. iter( )
348
- . map( ToString :: to_string)
349
- . collect:: <Vec <_>>( )
350
- . join( "," )
351
- }
352
- DebuggerPorts :: None => String :: default ( ) ,
346
+ match self {
347
+ Self :: Single ( port) => port. fmt ( f) ,
348
+ Self :: FixedRange ( range) => write ! ( f, "{}-{}" , range. start( ) , range. end( ) ) ,
349
+ Self :: Combination ( vec) => {
350
+ let value = vec
351
+ . iter ( )
352
+ . map ( ToString :: to_string)
353
+ . collect :: < Vec < _ > > ( )
354
+ . join ( "," ) ;
355
+ f. write_str ( & value)
353
356
}
354
- )
357
+ Self :: None => Ok ( ( ) ) ,
358
+ }
355
359
}
356
360
}
357
361
@@ -366,10 +370,11 @@ impl DebuggerPorts {
366
370
. ok ( )
367
371
. and_then ( |s| {
368
372
DebuggerType :: from_str ( & s)
369
- . inspect_err ( |e| {
370
- error ! (
371
- "Failed to decode debugger type from {} env variable: {}" ,
372
- MIRRORD_DETECT_DEBUGGER_PORT_ENV , e
373
+ . inspect_err ( |error| {
374
+ tracing:: debug!(
375
+ error,
376
+ "Failed to decode debugger type from {} env variable" ,
377
+ MIRRORD_DETECT_DEBUGGER_PORT_ENV ,
373
378
)
374
379
} )
375
380
. ok ( )
0 commit comments