3
3
// FIXME: Once the portability lint RFC is implemented (see tracking issue #41619),
4
4
// switch to use those structures instead.
5
5
6
- use std:: fmt:: { self , Write } ;
7
- use std:: { mem, ops} ;
6
+ use std:: { fmt, mem, ops} ;
8
7
8
+ use itertools:: Either ;
9
9
use rustc_ast:: { LitKind , MetaItem , MetaItemInner , MetaItemKind , MetaItemLit } ;
10
10
use rustc_data_structures:: fx:: FxHashSet ;
11
11
use rustc_session:: parse:: ParseSess ;
12
12
use rustc_span:: Span ;
13
13
use rustc_span:: symbol:: { Symbol , sym} ;
14
14
15
- use crate :: display:: Joined as _;
15
+ use crate :: display:: { Joined as _, MaybeDisplay , Wrapped } ;
16
16
use crate :: html:: escape:: Escape ;
17
17
18
18
#[ cfg( test) ]
@@ -376,27 +376,20 @@ impl Format {
376
376
Format :: LongPlain => false ,
377
377
}
378
378
}
379
+
380
+ fn escape ( self , s : & str ) -> impl fmt:: Display {
381
+ if self . is_html ( ) { Either :: Left ( Escape ( s) ) } else { Either :: Right ( s) }
382
+ }
379
383
}
380
384
381
385
/// Pretty-print wrapper for a `Cfg`. Also indicates what form of rendering should be used.
382
386
struct Display < ' a > ( & ' a Cfg , Format ) ;
383
387
384
- fn write_with_opt_paren < T : fmt:: Display > (
385
- fmt : & mut fmt:: Formatter < ' _ > ,
386
- has_paren : bool ,
387
- obj : T ,
388
- ) -> fmt:: Result {
389
- if has_paren {
390
- fmt. write_char ( '(' ) ?;
391
- }
392
- obj. fmt ( fmt) ?;
393
- if has_paren {
394
- fmt. write_char ( ')' ) ?;
388
+ impl Display < ' _ > {
389
+ fn code_wrappers ( & self ) -> Wrapped < & ' static str > {
390
+ if self . 1 . is_html ( ) { Wrapped :: with ( "<code>" , "</code>" ) } else { Wrapped :: with ( "`" , "`" ) }
395
391
}
396
- Ok ( ( ) )
397
- }
398
392
399
- impl Display < ' _ > {
400
393
fn display_sub_cfgs (
401
394
& self ,
402
395
fmt : & mut fmt:: Formatter < ' _ > ,
@@ -427,20 +420,17 @@ impl Display<'_> {
427
420
sub_cfgs
428
421
. iter ( )
429
422
. map ( |sub_cfg| {
430
- fmt:: from_fn ( move |fmt| {
431
- if let Cfg :: Cfg ( _, Some ( feat) ) = sub_cfg
432
- && short_longhand
433
- {
434
- if self . 1 . is_html ( ) {
435
- write ! ( fmt, "<code>{feat}</code>" ) ?;
436
- } else {
437
- write ! ( fmt, "`{feat}`" ) ?;
438
- }
439
- } else {
440
- write_with_opt_paren ( fmt, !sub_cfg. is_all ( ) , Display ( sub_cfg, self . 1 ) ) ?;
441
- }
442
- Ok ( ( ) )
443
- } )
423
+ if let Cfg :: Cfg ( _, Some ( feat) ) = sub_cfg
424
+ && short_longhand
425
+ {
426
+ Either :: Left ( self . code_wrappers ( ) . wrap ( feat) )
427
+ } else {
428
+ Either :: Right (
429
+ Wrapped :: with_parens ( )
430
+ . when ( !sub_cfg. is_all ( ) )
431
+ . wrap ( Display ( sub_cfg, self . 1 ) ) ,
432
+ )
433
+ }
444
434
} )
445
435
. joined ( separator, f)
446
436
} )
@@ -461,9 +451,9 @@ impl fmt::Display for Display<'_> {
461
451
sub_cfgs
462
452
. iter ( )
463
453
. map ( |sub_cfg| {
464
- fmt :: from_fn ( |fmt| {
465
- write_with_opt_paren ( fmt , !sub_cfg. is_all ( ) , Display ( sub_cfg , self . 1 ) )
466
- } )
454
+ Wrapped :: with_parens ( )
455
+ . when ( !sub_cfg. is_all ( ) )
456
+ . wrap ( Display ( sub_cfg , self . 1 ) )
467
457
} )
468
458
. joined ( separator, fmt)
469
459
}
@@ -568,21 +558,13 @@ impl fmt::Display for Display<'_> {
568
558
} ;
569
559
if !human_readable. is_empty ( ) {
570
560
fmt. write_str ( human_readable)
571
- } else if let Some ( v) = value {
572
- if self . 1 . is_html ( ) {
573
- write ! (
574
- fmt,
575
- r#"<code>{}="{}"</code>"# ,
576
- Escape ( name. as_str( ) ) ,
577
- Escape ( v. as_str( ) )
578
- )
579
- } else {
580
- write ! ( fmt, r#"`{name}="{v}"`"# )
581
- }
582
- } else if self . 1 . is_html ( ) {
583
- write ! ( fmt, "<code>{}</code>" , Escape ( name. as_str( ) ) )
584
561
} else {
585
- write ! ( fmt, "`{name}`" )
562
+ let value = value
563
+ . map ( |v| fmt:: from_fn ( move |f| write ! ( f, "={}" , self . 1 . escape( v. as_str( ) ) ) ) )
564
+ . maybe_display ( ) ;
565
+ self . code_wrappers ( )
566
+ . wrap ( format_args ! ( "{}{value}" , self . 1 . escape( name. as_str( ) ) ) )
567
+ . fmt ( fmt)
586
568
}
587
569
}
588
570
}
0 commit comments