21
21
//!
22
22
//! // Next, learn what arguments we need to pass to `cargo`.
23
23
//! let targets = metadata.targets();
24
- //! let mut cargo_args = metadata.cargo_args();
24
+ //! let mut cargo_args = metadata.cargo_args(&[], &[] );
25
25
//! cargo_args.push(targets.default_target.into());
26
26
//!
27
27
//! // Now, set up the `Command`
@@ -228,11 +228,7 @@ impl Metadata {
228
228
/// Note that this does not necessarily reproduce the HTML _output_ of docs.rs exactly.
229
229
/// For example, the links may point somewhere different than they would on docs.rs.
230
230
/// However, rustdoc will see exactly the same code as it would on docs.rs, even counting `cfg`s.
231
- pub fn cargo_args (
232
- & self ,
233
- additional_args : & [ impl AsRef < str > ] ,
234
- rustdoc_args : & [ impl AsRef < str > ] ,
235
- ) -> Vec < String > {
231
+ pub fn cargo_args ( & self , additional_args : & [ String ] , rustdoc_args : & [ String ] ) -> Vec < String > {
236
232
let mut cargo_args: Vec < String > = vec ! [ "rustdoc" . into( ) , "--lib" . into( ) ] ;
237
233
238
234
if let Some ( features) = & self . features {
@@ -258,16 +254,14 @@ impl Metadata {
258
254
cargo_args. push ( format ! ( "build.rustflags={}" , rustflags) ) ;
259
255
}
260
256
261
- cargo_args. extend ( additional_args. iter ( ) . map ( |s| s. as_ref ( ) . to_owned ( ) ) ) ;
257
+ cargo_args. extend ( additional_args. iter ( ) . map ( |s| s. to_owned ( ) ) ) ;
262
258
cargo_args. push ( "--" . into ( ) ) ;
263
259
cargo_args. extend_from_slice ( & self . rustdoc_args ) ;
264
- cargo_args. extend ( rustdoc_args. iter ( ) . map ( |s| s. as_ref ( ) . to_owned ( ) ) ) ;
260
+ cargo_args. extend ( rustdoc_args. iter ( ) . map ( |s| s. to_owned ( ) ) ) ;
265
261
cargo_args
266
262
}
267
263
268
264
/// Return the environment variables that should be set when building this crate.
269
- ///
270
- /// This will always contain at least `RUSTDOCFLAGS="-Z unstable-options"`.
271
265
pub fn environment_variables ( & self ) -> HashMap < & ' static str , String > {
272
266
let mut map = HashMap :: new ( ) ;
273
267
// For docs.rs detection from build scripts:
@@ -357,7 +351,7 @@ mod test_parsing {
357
351
assert_eq ! ( targets[ 0 ] , "x86_64-apple-darwin" ) ;
358
352
assert_eq ! ( targets[ 1 ] , "x86_64-pc-windows-msvc" ) ;
359
353
360
- let rustc_args = metadata. rustc_args . unwrap ( ) ;
354
+ let rustc_args = metadata. rustc_args ;
361
355
assert_eq ! ( rustc_args. len( ) , 1 ) ;
362
356
assert_eq ! ( rustc_args[ 0 ] , "--example-rustc-arg" . to_owned( ) ) ;
363
357
@@ -527,17 +521,17 @@ mod test_calculations {
527
521
use super :: * ;
528
522
529
523
fn default_cargo_args ( ) -> Vec < String > {
530
- vec ! [ "doc " . into( ) , "--lib" . into( ) , "--no-deps " . into( ) ]
524
+ vec ! [ "rustdoc " . into( ) , "--lib" . into( ) , "--" . into( ) ]
531
525
}
532
526
533
527
#[ test]
534
528
fn test_defaults ( ) {
535
529
let metadata = Metadata :: default ( ) ;
536
- assert_eq ! ( metadata. cargo_args( ) , default_cargo_args( ) ) ;
530
+ assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , default_cargo_args( ) ) ;
537
531
let env = metadata. environment_variables ( ) ;
538
532
assert_eq ! ( env. get( "DOCS_RS" ) . map( String :: as_str) , Some ( "1" ) ) ;
539
- assert_eq ! ( env. get( "RUSTDOCFLAGS" ) . map ( String :: as_str ) , Some ( "" ) ) ;
540
- assert_eq ! ( env. get( "RUSTFLAGS" ) . map ( String :: as_str ) , Some ( "" ) ) ;
533
+ assert ! ( env. get( "RUSTDOCFLAGS" ) . is_none ( ) ) ;
534
+ assert ! ( env. get( "RUSTFLAGS" ) . is_none ( ) ) ;
541
535
}
542
536
543
537
#[ test]
@@ -548,17 +542,17 @@ mod test_calculations {
548
542
..Metadata :: default ( )
549
543
} ;
550
544
let mut expected_args = default_cargo_args ( ) ;
551
- expected_args. push ( "--all-features" . into ( ) ) ;
552
- assert_eq ! ( metadata. cargo_args( ) , expected_args) ;
545
+ expected_args. insert ( expected_args . len ( ) - 1 , "--all-features" . into ( ) ) ;
546
+ assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
553
547
554
548
// no default features
555
549
let metadata = Metadata {
556
550
no_default_features : true ,
557
551
..Metadata :: default ( )
558
552
} ;
559
553
let mut expected_args = default_cargo_args ( ) ;
560
- expected_args. push ( "--no-default-features" . into ( ) ) ;
561
- assert_eq ! ( metadata. cargo_args( ) , expected_args) ;
554
+ expected_args. insert ( expected_args . len ( ) - 1 , "--no-default-features" . into ( ) ) ;
555
+ assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
562
556
563
557
// allow passing both even though it's nonsense; cargo will give an error anyway
564
558
let metadata = Metadata {
@@ -567,39 +561,52 @@ mod test_calculations {
567
561
..Metadata :: default ( )
568
562
} ;
569
563
let mut expected_args = default_cargo_args ( ) ;
570
- expected_args. push ( "--all-features" . into ( ) ) ;
571
- expected_args. push ( "--no-default-features" . into ( ) ) ;
572
- assert_eq ! ( metadata. cargo_args( ) , expected_args) ;
564
+ let idx = expected_args. len ( ) - 1 ;
565
+ expected_args. insert ( idx, "--all-features" . into ( ) ) ;
566
+ expected_args. insert ( idx + 1 , "--no-default-features" . into ( ) ) ;
567
+ assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
573
568
574
569
// explicit empty vec
575
570
let metadata = Metadata {
576
571
features : Some ( vec ! [ ] ) ,
577
572
..Metadata :: default ( )
578
573
} ;
579
- let mut expected_args = default_cargo_args ( ) ;
580
- expected_args. push ( "--features" . into ( ) ) ;
581
- expected_args. push ( String :: new ( ) ) ;
582
- assert_eq ! ( metadata. cargo_args( ) , expected_args) ;
574
+ let expected_args = vec ! [
575
+ "rustdoc" . into( ) ,
576
+ "--lib" . into( ) ,
577
+ "--features" . into( ) ,
578
+ String :: new( ) ,
579
+ "--" . into( ) ,
580
+ ] ;
581
+ assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
583
582
584
583
// one feature
585
584
let metadata = Metadata {
586
585
features : Some ( vec ! [ "some_feature" . into( ) ] ) ,
587
586
..Metadata :: default ( )
588
587
} ;
589
- let mut expected_args = default_cargo_args ( ) ;
590
- expected_args. push ( "--features" . into ( ) ) ;
591
- expected_args. push ( "some_feature" . into ( ) ) ;
592
- assert_eq ! ( metadata. cargo_args( ) , expected_args) ;
588
+ let expected_args = vec ! [
589
+ String :: from( "rustdoc" ) ,
590
+ "--lib" . into( ) ,
591
+ "--features" . into( ) ,
592
+ "some_feature" . into( ) ,
593
+ "--" . into( ) ,
594
+ ] ;
595
+ assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
593
596
594
597
// multiple features
595
598
let metadata = Metadata {
596
599
features : Some ( vec ! [ "feature1" . into( ) , "feature2" . into( ) ] ) ,
597
600
..Metadata :: default ( )
598
601
} ;
599
- let mut expected_args = default_cargo_args ( ) ;
600
- expected_args. push ( "--features" . into ( ) ) ;
601
- expected_args. push ( "feature1 feature2" . into ( ) ) ;
602
- assert_eq ! ( metadata. cargo_args( ) , expected_args) ;
602
+ let expected_args = vec ! [
603
+ String :: from( "rustdoc" ) ,
604
+ "--lib" . into( ) ,
605
+ "--features" . into( ) ,
606
+ "feature1 feature2" . into( ) ,
607
+ "--" . into( ) ,
608
+ ] ;
609
+ assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
603
610
604
611
// rustdocflags
605
612
let metadata = Metadata {
@@ -613,32 +620,33 @@ mod test_calculations {
613
620
] ,
614
621
..Metadata :: default ( )
615
622
} ;
616
- assert_eq ! (
617
- metadata
618
- . environment_variables( )
619
- . get( "RUSTDOCFLAGS" )
620
- . map( String :: as_str) ,
621
- Some ( "-Z unstable-options --static-root-path / --cap-lints warn" )
622
- ) ;
623
+ let expected_args = vec ! [
624
+ String :: from( "rustdoc" ) ,
625
+ "--lib" . into( ) ,
626
+ "--" . into( ) ,
627
+ "-Z" . into( ) ,
628
+ "unstable-options" . into( ) ,
629
+ "--static-root-path" . into( ) ,
630
+ "/" . into( ) ,
631
+ "--cap-lints" . into( ) ,
632
+ "warn" . into( ) ,
633
+ ] ;
634
+ assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
623
635
624
636
// rustdocflags
625
637
let metadata = Metadata {
626
- rustc_args : Some ( vec ! [
627
- "-Z" . into( ) ,
628
- "unstable-options" . into( ) ,
629
- "--static-root-path" . into( ) ,
630
- "/" . into( ) ,
631
- "--cap-lints" . into( ) ,
632
- "warn" . into( ) ,
633
- ] ) ,
638
+ rustc_args : vec ! [ "--cfg" . into( ) , "x" . into( ) ] ,
634
639
..Metadata :: default ( )
635
640
} ;
636
- assert_eq ! (
637
- metadata
638
- . environment_variables( )
639
- . get( "RUSTFLAGS" )
640
- . map( String :: as_str) ,
641
- Some ( "-Z unstable-options --static-root-path / --cap-lints warn" )
642
- ) ;
641
+ let expected_args = vec ! [
642
+ String :: from( "rustdoc" ) ,
643
+ "--lib" . into( ) ,
644
+ "-Z" . into( ) ,
645
+ "unstable-options" . into( ) ,
646
+ "--config" . into( ) ,
647
+ "build.rustflags=[\" --cfg\" , \" x\" ]" . into( ) ,
648
+ "--" . into( ) ,
649
+ ] ;
650
+ assert_eq ! ( metadata. cargo_args( & [ ] , & [ ] ) , expected_args) ;
643
651
}
644
652
}
0 commit comments