5
5
//! This library is intended for use in docs.rs and crater, but might be helpful to others.
6
6
//! See <https://docs.rs/about/metadata> for more information about the flags that can be set.
7
7
//!
8
+ //! This crate can only be used with nightly versions of `cargo` and `rustdoc`, because it
9
+ //! will always have the flag `-Z unstable-options`.
10
+ //!
8
11
//! Here is an example use of the crate:
9
12
//!
10
13
//! ```
@@ -95,7 +98,7 @@ pub enum MetadataError {
95
98
/// ```
96
99
///
97
100
/// You can define one or more fields in your `Cargo.toml`.
98
- #[ derive( Deserialize ) ]
101
+ #[ derive( Default , Deserialize ) ]
99
102
#[ serde( rename_all = "kebab-case" ) ]
100
103
pub struct Metadata {
101
104
/// List of features to pass on to `cargo`.
@@ -122,7 +125,8 @@ pub struct Metadata {
122
125
rustc_args : Option < Vec < String > > ,
123
126
124
127
/// List of command line arguments for `rustdoc`.
125
- rustdoc_args : Option < Vec < String > > ,
128
+ #[ serde( default ) ]
129
+ rustdoc_args : Vec < String > ,
126
130
}
127
131
128
132
/// The targets that should be built for a crate.
@@ -240,12 +244,14 @@ impl Metadata {
240
244
}
241
245
242
246
/// Return the environment variables that should be set when building this crate.
247
+ ///
248
+ /// This will always contain at least `RUSTDOCFLAGS="-Z unstable-options"`.
243
249
pub fn environment_variables ( & self ) -> HashMap < & ' static str , String > {
244
250
let joined = |v : & Option < Vec < _ > > | v. as_ref ( ) . map ( |args| args. join ( " " ) ) . unwrap_or_default ( ) ;
245
251
246
252
let mut map = HashMap :: new ( ) ;
247
253
map. insert ( "RUSTFLAGS" , joined ( & self . rustc_args ) ) ;
248
- map. insert ( "RUSTDOCFLAGS" , joined ( & self . rustdoc_args ) ) ;
254
+ map. insert ( "RUSTDOCFLAGS" , self . rustdoc_args . join ( " " ) ) ;
249
255
// For docs.rs detection from build scripts:
250
256
// https://github.com/rust-lang/docs.rs/issues/147
251
257
map. insert ( "DOCS_RS" , "1" . into ( ) ) ;
@@ -278,28 +284,16 @@ impl std::str::FromStr for Metadata {
278
284
. and_then ( |t| table ( t, "metadata" ) )
279
285
. and_then ( |t| table ( t, "docs" ) )
280
286
. and_then ( |t| table ( t, "rs" ) ) ;
281
- let metadata = if let Some ( table) = table {
287
+ let mut metadata = if let Some ( table) = table {
282
288
Value :: Table ( table) . try_into ( ) ?
283
289
} else {
284
290
Metadata :: default ( )
285
291
} ;
286
292
287
- Ok ( metadata)
288
- }
289
- }
293
+ metadata. rustdoc_args . push ( "-Z" . into ( ) ) ;
294
+ metadata. rustdoc_args . push ( "unstable-options" . into ( ) ) ;
290
295
291
- impl Default for Metadata {
292
- /// The metadata that is used if there is no `[package.metadata.docs.rs]` in `Cargo.toml`.
293
- fn default ( ) -> Metadata {
294
- Metadata {
295
- features : None ,
296
- all_features : false ,
297
- no_default_features : false ,
298
- default_target : None ,
299
- rustc_args : None ,
300
- rustdoc_args : None ,
301
- targets : None ,
302
- }
296
+ Ok ( metadata)
303
297
}
304
298
}
305
299
@@ -330,7 +324,6 @@ mod test_parsing {
330
324
assert ! ( metadata. all_features) ;
331
325
assert ! ( metadata. no_default_features) ;
332
326
assert ! ( metadata. default_target. is_some( ) ) ;
333
- assert ! ( metadata. rustdoc_args. is_some( ) ) ;
334
327
335
328
let features = metadata. features . unwrap ( ) ;
336
329
assert_eq ! ( features. len( ) , 2 ) ;
@@ -351,9 +344,11 @@ mod test_parsing {
351
344
assert_eq ! ( rustc_args. len( ) , 1 ) ;
352
345
assert_eq ! ( rustc_args[ 0 ] , "--example-rustc-arg" . to_owned( ) ) ;
353
346
354
- let rustdoc_args = metadata. rustdoc_args . unwrap ( ) ;
355
- assert_eq ! ( rustdoc_args. len( ) , 1 ) ;
347
+ let rustdoc_args = metadata. rustdoc_args ;
348
+ assert_eq ! ( rustdoc_args. len( ) , 3 ) ;
356
349
assert_eq ! ( rustdoc_args[ 0 ] , "--example-rustdoc-arg" . to_owned( ) ) ;
350
+ assert_eq ! ( rustdoc_args[ 1 ] , "-Z" . to_owned( ) ) ;
351
+ assert_eq ! ( rustdoc_args[ 2 ] , "unstable-options" . to_owned( ) ) ;
357
352
}
358
353
359
354
#[ test]
@@ -591,14 +586,14 @@ mod test_calculations {
591
586
592
587
// rustdocflags
593
588
let metadata = Metadata {
594
- rustdoc_args : Some ( vec ! [
589
+ rustdoc_args : vec ! [
595
590
"-Z" . into( ) ,
596
591
"unstable-options" . into( ) ,
597
592
"--static-root-path" . into( ) ,
598
593
"/" . into( ) ,
599
594
"--cap-lints" . into( ) ,
600
595
"warn" . into( ) ,
601
- ] ) ,
596
+ ] ,
602
597
..Metadata :: default ( )
603
598
} ;
604
599
assert_eq ! (
0 commit comments