55//! This library is intended for use in docs.rs and crater, but might be helpful to others.
66//! See <https://docs.rs/about/metadata> for more information about the flags that can be set.
77//!
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+ //!
811//! Here is an example use of the crate:
912//!
1013//! ```
@@ -95,7 +98,7 @@ pub enum MetadataError {
9598/// ```
9699///
97100/// You can define one or more fields in your `Cargo.toml`.
98- #[ derive( Deserialize ) ]
101+ #[ derive( Default , Deserialize ) ]
99102#[ serde( rename_all = "kebab-case" ) ]
100103pub struct Metadata {
101104 /// List of features to pass on to `cargo`.
@@ -122,7 +125,8 @@ pub struct Metadata {
122125 rustc_args : Option < Vec < String > > ,
123126
124127 /// List of command line arguments for `rustdoc`.
125- rustdoc_args : Option < Vec < String > > ,
128+ #[ serde( default ) ]
129+ rustdoc_args : Vec < String > ,
126130}
127131
128132/// The targets that should be built for a crate.
@@ -240,12 +244,14 @@ impl Metadata {
240244 }
241245
242246 /// Return the environment variables that should be set when building this crate.
247+ ///
248+ /// This will always contain at least `RUSTDOCFLAGS="-Z unstable-options"`.
243249 pub fn environment_variables ( & self ) -> HashMap < & ' static str , String > {
244250 let joined = |v : & Option < Vec < _ > > | v. as_ref ( ) . map ( |args| args. join ( " " ) ) . unwrap_or_default ( ) ;
245251
246252 let mut map = HashMap :: new ( ) ;
247253 map. insert ( "RUSTFLAGS" , joined ( & self . rustc_args ) ) ;
248- map. insert ( "RUSTDOCFLAGS" , joined ( & self . rustdoc_args ) ) ;
254+ map. insert ( "RUSTDOCFLAGS" , self . rustdoc_args . join ( " " ) ) ;
249255 // For docs.rs detection from build scripts:
250256 // https://github.com/rust-lang/docs.rs/issues/147
251257 map. insert ( "DOCS_RS" , "1" . into ( ) ) ;
@@ -278,28 +284,16 @@ impl std::str::FromStr for Metadata {
278284 . and_then ( |t| table ( t, "metadata" ) )
279285 . and_then ( |t| table ( t, "docs" ) )
280286 . and_then ( |t| table ( t, "rs" ) ) ;
281- let metadata = if let Some ( table) = table {
287+ let mut metadata = if let Some ( table) = table {
282288 Value :: Table ( table) . try_into ( ) ?
283289 } else {
284290 Metadata :: default ( )
285291 } ;
286292
287- Ok ( metadata)
288- }
289- }
293+ metadata. rustdoc_args . push ( "-Z" . into ( ) ) ;
294+ metadata. rustdoc_args . push ( "unstable-options" . into ( ) ) ;
290295
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)
303297 }
304298}
305299
@@ -330,7 +324,6 @@ mod test_parsing {
330324 assert ! ( metadata. all_features) ;
331325 assert ! ( metadata. no_default_features) ;
332326 assert ! ( metadata. default_target. is_some( ) ) ;
333- assert ! ( metadata. rustdoc_args. is_some( ) ) ;
334327
335328 let features = metadata. features . unwrap ( ) ;
336329 assert_eq ! ( features. len( ) , 2 ) ;
@@ -351,9 +344,11 @@ mod test_parsing {
351344 assert_eq ! ( rustc_args. len( ) , 1 ) ;
352345 assert_eq ! ( rustc_args[ 0 ] , "--example-rustc-arg" . to_owned( ) ) ;
353346
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 ) ;
356349 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( ) ) ;
357352 }
358353
359354 #[ test]
@@ -591,14 +586,14 @@ mod test_calculations {
591586
592587 // rustdocflags
593588 let metadata = Metadata {
594- rustdoc_args : Some ( vec ! [
589+ rustdoc_args : vec ! [
595590 "-Z" . into( ) ,
596591 "unstable-options" . into( ) ,
597592 "--static-root-path" . into( ) ,
598593 "/" . into( ) ,
599594 "--cap-lints" . into( ) ,
600595 "warn" . into( ) ,
601- ] ) ,
596+ ] ,
602597 ..Metadata :: default ( )
603598 } ;
604599 assert_eq ! (
0 commit comments