@@ -135,7 +135,7 @@ impl Cfg {
135
135
136
136
/// Renders the configuration for human display, as a short HTML description.
137
137
pub ( crate ) fn render_short_html ( & self ) -> String {
138
- let mut msg = Html ( self , true ) . to_string ( ) ;
138
+ let mut msg = Display ( self , Format :: ShortHtml ) . to_string ( ) ;
139
139
if self . should_capitalize_first_letter ( ) {
140
140
if let Some ( i) = msg. find ( |c : char | c. is_ascii_alphanumeric ( ) ) {
141
141
msg[ i..i + 1 ] . make_ascii_uppercase ( ) ;
@@ -148,14 +148,29 @@ impl Cfg {
148
148
pub ( crate ) fn render_long_html ( & self ) -> String {
149
149
let on = if self . should_use_with_in_description ( ) { "with" } else { "on" } ;
150
150
151
- let mut msg = format ! ( "This is supported {} <strong>{}</strong>" , on, Html ( self , false ) ) ;
151
+ let mut msg = format ! (
152
+ "This is supported {} <strong>{}</strong>" ,
153
+ on,
154
+ Display ( self , Format :: LongHtml )
155
+ ) ;
152
156
if self . should_append_only_to_description ( ) {
153
157
msg. push_str ( " only" ) ;
154
158
}
155
159
msg. push ( '.' ) ;
156
160
msg
157
161
}
158
162
163
+ /// Renders the configuration for long display, as a long plain text description.
164
+ pub ( crate ) fn render_long_plain ( & self ) -> String {
165
+ let on = if self . should_use_with_in_description ( ) { "with" } else { "on" } ;
166
+
167
+ let mut msg = format ! ( "This is supported {} {}" , on, Display ( self , Format :: LongPlain ) ) ;
168
+ if self . should_append_only_to_description ( ) {
169
+ msg. push_str ( " only" ) ;
170
+ }
171
+ msg
172
+ }
173
+
159
174
fn should_capitalize_first_letter ( & self ) -> bool {
160
175
match * self {
161
176
Cfg :: False | Cfg :: True | Cfg :: Not ( ..) => true ,
@@ -286,9 +301,31 @@ impl ops::BitOr for Cfg {
286
301
}
287
302
}
288
303
289
- /// Pretty-print wrapper for a `Cfg`. Also indicates whether the "short-form" rendering should be
290
- /// used.
291
- struct Html < ' a > ( & ' a Cfg , bool ) ;
304
+ #[ derive( Clone , Copy ) ]
305
+ enum Format {
306
+ LongHtml ,
307
+ LongPlain ,
308
+ ShortHtml ,
309
+ }
310
+
311
+ impl Format {
312
+ fn is_long ( self ) -> bool {
313
+ match self {
314
+ Format :: LongHtml | Format :: LongPlain => true ,
315
+ Format :: ShortHtml => false ,
316
+ }
317
+ }
318
+
319
+ fn is_html ( self ) -> bool {
320
+ match self {
321
+ Format :: LongHtml | Format :: ShortHtml => true ,
322
+ Format :: LongPlain => false ,
323
+ }
324
+ }
325
+ }
326
+
327
+ /// Pretty-print wrapper for a `Cfg`. Also indicates what form of rendering should be used.
328
+ struct Display < ' a > ( & ' a Cfg , Format ) ;
292
329
293
330
fn write_with_opt_paren < T : fmt:: Display > (
294
331
fmt : & mut fmt:: Formatter < ' _ > ,
@@ -305,7 +342,7 @@ fn write_with_opt_paren<T: fmt::Display>(
305
342
Ok ( ( ) )
306
343
}
307
344
308
- impl < ' a > fmt:: Display for Html < ' a > {
345
+ impl < ' a > fmt:: Display for Display < ' a > {
309
346
fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
310
347
match * self . 0 {
311
348
Cfg :: Not ( ref child) => match * * child {
@@ -314,18 +351,18 @@ impl<'a> fmt::Display for Html<'a> {
314
351
if sub_cfgs. iter ( ) . all ( Cfg :: is_simple) { " nor " } else { ", nor " } ;
315
352
for ( i, sub_cfg) in sub_cfgs. iter ( ) . enumerate ( ) {
316
353
fmt. write_str ( if i == 0 { "neither " } else { separator } ) ?;
317
- write_with_opt_paren ( fmt, !sub_cfg. is_all ( ) , Html ( sub_cfg, self . 1 ) ) ?;
354
+ write_with_opt_paren ( fmt, !sub_cfg. is_all ( ) , Display ( sub_cfg, self . 1 ) ) ?;
318
355
}
319
356
Ok ( ( ) )
320
357
}
321
- ref simple @ Cfg :: Cfg ( ..) => write ! ( fmt, "non-{}" , Html ( simple, self . 1 ) ) ,
322
- ref c => write ! ( fmt, "not ({})" , Html ( c, self . 1 ) ) ,
358
+ ref simple @ Cfg :: Cfg ( ..) => write ! ( fmt, "non-{}" , Display ( simple, self . 1 ) ) ,
359
+ ref c => write ! ( fmt, "not ({})" , Display ( c, self . 1 ) ) ,
323
360
} ,
324
361
325
362
Cfg :: Any ( ref sub_cfgs) => {
326
363
let separator = if sub_cfgs. iter ( ) . all ( Cfg :: is_simple) { " or " } else { ", or " } ;
327
364
328
- let short_longhand = ! self . 1 && {
365
+ let short_longhand = self . 1 . is_long ( ) && {
329
366
let all_crate_features = sub_cfgs
330
367
. iter ( )
331
368
. all ( |sub_cfg| matches ! ( sub_cfg, Cfg :: Cfg ( sym:: feature, Some ( _) ) ) ) ;
@@ -349,16 +386,20 @@ impl<'a> fmt::Display for Html<'a> {
349
386
fmt. write_str ( separator) ?;
350
387
}
351
388
if let ( true , Cfg :: Cfg ( _, Some ( feat) ) ) = ( short_longhand, sub_cfg) {
352
- write ! ( fmt, "<code>{}</code>" , feat) ?;
389
+ if self . 1 . is_html ( ) {
390
+ write ! ( fmt, "<code>{}</code>" , feat) ?;
391
+ } else {
392
+ write ! ( fmt, "`{}`" , feat) ?;
393
+ }
353
394
} else {
354
- write_with_opt_paren ( fmt, !sub_cfg. is_all ( ) , Html ( sub_cfg, self . 1 ) ) ?;
395
+ write_with_opt_paren ( fmt, !sub_cfg. is_all ( ) , Display ( sub_cfg, self . 1 ) ) ?;
355
396
}
356
397
}
357
398
Ok ( ( ) )
358
399
}
359
400
360
401
Cfg :: All ( ref sub_cfgs) => {
361
- let short_longhand = ! self . 1 && {
402
+ let short_longhand = self . 1 . is_long ( ) && {
362
403
let all_crate_features = sub_cfgs
363
404
. iter ( )
364
405
. all ( |sub_cfg| matches ! ( sub_cfg, Cfg :: Cfg ( sym:: feature, Some ( _) ) ) ) ;
@@ -382,9 +423,13 @@ impl<'a> fmt::Display for Html<'a> {
382
423
fmt. write_str ( " and " ) ?;
383
424
}
384
425
if let ( true , Cfg :: Cfg ( _, Some ( feat) ) ) = ( short_longhand, sub_cfg) {
385
- write ! ( fmt, "<code>{}</code>" , feat) ?;
426
+ if self . 1 . is_html ( ) {
427
+ write ! ( fmt, "<code>{}</code>" , feat) ?;
428
+ } else {
429
+ write ! ( fmt, "`{}`" , feat) ?;
430
+ }
386
431
} else {
387
- write_with_opt_paren ( fmt, !sub_cfg. is_simple ( ) , Html ( sub_cfg, self . 1 ) ) ?;
432
+ write_with_opt_paren ( fmt, !sub_cfg. is_simple ( ) , Display ( sub_cfg, self . 1 ) ) ?;
388
433
}
389
434
}
390
435
Ok ( ( ) )
@@ -453,33 +498,39 @@ impl<'a> fmt::Display for Html<'a> {
453
498
} ,
454
499
( sym:: target_endian, Some ( endian) ) => return write ! ( fmt, "{}-endian" , endian) ,
455
500
( sym:: target_pointer_width, Some ( bits) ) => return write ! ( fmt, "{}-bit" , bits) ,
456
- ( sym:: target_feature, Some ( feat) ) => {
457
- if self . 1 {
458
- return write ! ( fmt, "<code>{}</code>" , feat) ;
459
- } else {
501
+ ( sym:: target_feature, Some ( feat) ) => match self . 1 {
502
+ Format :: LongHtml => {
460
503
return write ! ( fmt, "target feature <code>{}</code>" , feat) ;
461
504
}
462
- }
463
- ( sym :: feature , Some ( feat ) ) => {
464
- if self . 1 {
465
- return write ! ( fmt , "<code>{}</code>" , feat ) ;
466
- } else {
505
+ Format :: LongPlain => return write ! ( fmt , "target feature `{}`" , feat ) ,
506
+ Format :: ShortHtml => return write ! ( fmt , "<code>{}</code>" , feat ) ,
507
+ } ,
508
+ ( sym :: feature , Some ( feat ) ) => match self . 1 {
509
+ Format :: LongHtml => {
467
510
return write ! ( fmt, "crate feature <code>{}</code>" , feat) ;
468
511
}
469
- }
512
+ Format :: LongPlain => return write ! ( fmt, "crate feature `{}`" , feat) ,
513
+ Format :: ShortHtml => return write ! ( fmt, "<code>{}</code>" , feat) ,
514
+ } ,
470
515
_ => "" ,
471
516
} ;
472
517
if !human_readable. is_empty ( ) {
473
518
fmt. write_str ( human_readable)
474
519
} else if let Some ( v) = value {
475
- write ! (
476
- fmt,
477
- "<code>{}=\" {}\" </code>" ,
478
- Escape ( & name. as_str( ) ) ,
479
- Escape ( & v. as_str( ) )
480
- )
481
- } else {
520
+ if self . 1 . is_html ( ) {
521
+ write ! (
522
+ fmt,
523
+ r#"<code>{}="{}"</code>"# ,
524
+ Escape ( & name. as_str( ) ) ,
525
+ Escape ( & v. as_str( ) )
526
+ )
527
+ } else {
528
+ write ! ( fmt, r#"`{}="{}"`"# , name, v)
529
+ }
530
+ } else if self . 1 . is_html ( ) {
482
531
write ! ( fmt, "<code>{}</code>" , Escape ( & name. as_str( ) ) )
532
+ } else {
533
+ write ! ( fmt, "`{}`" , name)
483
534
}
484
535
}
485
536
}
0 commit comments